PostgreSQL\_dt命令不会在search_path模式中显示*ALL*表

sai*_*utz 7 postgresql search-path

为什么在PostgreSQL中,当不同模式中存在同名的表时,\dt只包含搜索路径中列出的第一个模式中的表,如下例所示?

我对......感兴趣:

  1. 这是理想的原因,并且

  2. 在查询基础的情况下如何实现它\dt(见下文).

(顺便说一下,我从这个答案中意识到\dt *.*将列出每个模式中的每个表 - 但是对于下面的示例情况,这给了我58个系统表,除了我做的两个之外我不想要它!)


dt_test=# CREATE SCHEMA first;
CREATE SCHEMA
dt_test=# CREATE SCHEMA second;
CREATE SCHEMA
dt_test=# CREATE TABLE first.my_table(id integer);
CREATE TABLE
dt_test=# CREATE TABLE second.my_table(id integer);
CREATE TABLE
dt_test=# set search_path to first,second;
SET
dt_test=# \dt
         List of relations
 Schema |   Name   | Type  | Owner  
--------+----------+-------+--------
 first  | my_table | table | postgres
(1 row)

dt_test=# set search_path to second,first;
SET
dt_test=# \dt
         List of relations
 Schema |   Name   | Type  | Owner  
--------+----------+-------+--------
 second | my_table | table | postgres
(1 row)
Run Code Online (Sandbox Code Playgroud)

查询底层\dt(使用-E命令启动psql时显示,例如,psql -E dt_test)

********* QUERY **********
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' END as "Type",
  pg_catalog.pg_get_userbyid(c.relowner) as "Owner"
FROM pg_catalog.pg_class c
     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','')
      AND n.nspname <> 'pg_catalog'
      AND n.nspname <> 'information_schema'
      AND n.nspname !~ '^pg_toast'
  AND pg_catalog.pg_table_is_visible(c.oid)
ORDER BY 1,2;
**************************
Run Code Online (Sandbox Code Playgroud)

Igo*_*nko 5

这是手册中的引用:http : //www.postgresql.org/docs/current/static/functions-info.html

表 9-61显示了确定某个对象在当前模式搜索路径中是否可见的函数。例如,如果一个表的包含模式在搜索路径中并且没有同名表出现在搜索路径的前面,则称该表可见。

关键部分:在搜索路径前面没有出现同名表

For set search_path to first,second;tablesecond.my_table不可见,因为它被阻止了first.my_table

  • 因此,考虑 \dt 的另一种方式似乎是,如果您使用没有模式限定的表名,它会返回您将访问/操作的表的列表。 (2认同)