AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 256894
Accepted
Peter Vandivier
Peter Vandivier
Asked: 2020-01-08 10:44:06 +0800 CST2020-01-08 10:44:06 +0800 CST 2020-01-08 10:44:06 +0800 CST

看不到两个同名表

  • 772

我public在一个数据库中有两个非模式。两个模式中都存在一个名为的表"foo",我可以对两者执行所有必要的 DDL 和 DML 命令。但是,当我执行时\d,我看不到它们。是什么赋予了?

复制品

create schema a;
create schema b;

create table a.foo (i int);
create table b.foo (i int);

set search_path = "$user", public, a, b;

在psql

postgres=# \d
            List of relations
 Schema |   Name    | Type  |   Owner
--------+-----------+-------+------------
 a      | foo       | table | pvandivier
(1 row)

我\set ECHO_HIDDEN on提取了运行的基础查询\d并将相关行从WHERE子句移到 中,SELECT以检查两个表对象之间的差异。

SELECT n.nspname as "Schema",
  c.relname as "Name",
  CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' WHEN 'p' THEN 'partitioned table' WHEN 'I' THEN 'partitioned index' END as "Type",
  pg_catalog.pg_get_userbyid(c.relowner) as "Owner",
  pg_catalog.pg_table_is_visible(c.oid),
  n.nspname,
  c.relkind
FROM pg_catalog.pg_class c
     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname IN ('a','b')
ORDER BY 1,2;

运行此查询显示...

 Schema | Name | Type  |   Owner    | pg_table_is_visible | nspname | relkind
--------+------+-------+------------+---------------------+---------+---------
 a      | foo  | table | pvandivier | t                   | a       | r
 b      | foo  | table | pvandivier | f                   | b       | r

相关的差异似乎是 的输出pg_table_is_visible()。但是为什么False在询问是否b.foo可见时它应该返回“”?

为什么要pg_table_is_visible()返回False我可以访问的表?

postgresql
  • 1 1 个回答
  • 236 Views

1 个回答

  • Voted
  1. Best Answer
    Peter Vandivier
    2020-01-08T10:44:06+08:002020-01-08T10:44:06+08:00

    这里的概念is_visible似乎是指“你看到的第 search_path一个”;而不是“你能看到吗”。您可以通过更改search_path.

    postgres=# set search_path = "$user", public, b, a;
    SET
    postgres=# \d
                List of relations
     Schema |   Name    | Type  |   Owner
    --------+-----------+-------+------------
     b      | foo       | table | pvandivier
    (1 row)
    
    postgres=#
    

    请注意,当在中首先列出b.foo时返回。上一个查询的输出也支持这一点,该查询在同一.bsearch_pathsearch_path

     Schema | Name | Type  |   Owner    | pg_table_is_visible | nspname | relkind
    --------+------+-------+------------+---------------------+---------+---------
     a      | foo  | table | pvandivier | f                   | a       | r
     b      | foo  | table | pvandivier | t                   | b       | r
    (2 rows)
    

    我们现在可以清楚地看到pg_table_is_visible()是Trueforb和Falsefor a。因此,“ is_visible”是当前可访问性的指标,而不是关系是否存在的指标。

    请注意,尝试确定不存在对象的可见性具有不同的行为集。

    postgres=# -- attempting to get the oid of a non-existent object
    postgres=# select 'c.foo'::regclass;
    ERROR:  schema "c" does not exist
    LINE 1: select 'c.foo'::regclass;
                   ^
    postgres=# select 'bar'::regclass::oid;
    ERROR:  relation "bar" does not exist
    LINE 1: select 'bar'::regclass::oid
                   ^
    postgres=# -- passing an oid for a non-existent object returns NULL
    postgres=# select pg_table_is_visible((random() * 1e7)::int::oid);
     pg_table_is_visible
    ---------------------
    
    (1 row)
    
    postgres=#
    

    您也可以在 src/backend/catalog/namespace.c 的源代码中阅读此内容。我对 C 语言不流利,但以下引用似乎支持这一推理。

    • pg_table_is_visible()引用RelationIsVisible
    Datum
    pg_table_is_visible(PG_FUNCTION_ARGS)
    {
        Oid         oid = PG_GETARG_OID(0);
    
        if (!SearchSysCacheExists1(RELOID, ObjectIdGetDatum(oid)))
            PG_RETURN_NULL();
    
        PG_RETURN_BOOL(RelationIsVisible(oid));
    }
    
    • RelationIsVisible在返回逻辑上方显示以下代码注释
    /*
     * If it is in the path, it might still not be visible; it could be
     * hidden by another relation of the same name earlier in the path. So
     * we must do a slow check for conflicting relations.
     */
    
    • 2

相关问题

  • 我可以在使用数据库后激活 PITR 吗?

  • 运行时间偏移延迟复制的最佳实践

  • 存储过程可以防止 SQL 注入吗?

  • PostgreSQL 中 UniProt 的生物序列

  • PostgreSQL 9.0 Replication 和 Slony-I 有什么区别?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve