PostgreSQL \ dt command does not show * ALL * tables in search_path schemas

Why in PostgreSQL, when there are tables with the same name in different schemas, \dt

only include the table in the first schema specified in the search path, according to the example below?

I'm interested in:

  • the reason it would be desirable, and

  • how this is actually achieved given the underlying query \dt

    (see far below).

(By the way, I understand from this answer that \dt *.*

will list every table in every schema, but for the example below, which gives me 58 tables I don't need in addition to the two I do!)


Example

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)

      

Request below \dt

(shown when starting psql with the -E command, for example 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;
**************************

      

+3


source to share


2 answers


Here is a quote from the manual: http://www.postgresql.org/docs/current/static/functions-info.html

Table 9-61 shows the functions that determine whether a particular object is visible in the current schema search path. For example, a table said to be visible if its containing schema is in the search path and a lookup table with the same name does not appear earlier in the search path.



Key part: no table with the same name appears earlier in the search path

For the set search_path to first,second;

table is second.my_table

not displayed because it is lockedfirst.my_table

+2


source


To display only the table you want to use in all schemas:



\dt *.my_table
         List of relations
 Schema |   Name   | Type  | Owner 
--------+----------+-------+-------
 first  | my_table | table | cpn
 public | my_table | table | cpn
 second | my_table | table | cpn

      

0


source







All Articles