为什么在 PostgreSQL 中,当不同模式中存在同名表时,\dt
只包含搜索路径中列出的第一个模式中的表,如下例所示?
我感兴趣的是:
这将是可取的原因,以及
给定底层查询
\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)
查询底层\dt
(当 psql 使用 -E 命令启动时显示,例如 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;
**************************