1- select
2- tbl .schemaname as schema,
3- tbl .tablename as table,
4- tbl .quoted_name ,
5- tbl .is_table ,
6- json_agg(a) as columns
7- from
8- (
9- select
10- n .nspname as schemaname,
11- c .relname as tablename,
12- (
13- quote_ident(n .nspname ) || ' .' || quote_ident(c .relname )
14- ) as quoted_name,
15- true as is_table
16- from
17- pg_catalog .pg_class c
18- join pg_catalog .pg_namespace n on n .oid = c .relnamespace
19- where
20- c .relkind = ' r'
21- and n .nspname != ' pg_toast'
22- and n .nspname not like ' pg_temp_%'
23- and n .nspname not like ' pg_toast_temp_%'
24- and has_schema_privilege(n .oid , ' USAGE' ) = true
25- and has_table_privilege(
26- quote_ident(n .nspname ) || ' .' || quote_ident(c .relname ),
27- ' SELECT, INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER'
28- ) = true
29- union
30- all
1+ with
2+ available_tables as (
313 select
32- n .nspname as schemaname,
33- c .relname as tablename,
34- (
35- quote_ident(n .nspname ) || ' .' || quote_ident(c .relname )
36- ) as quoted_name,
37- false as is_table
4+ c .relname as table_name,
5+ c .oid as table_oid,
6+ c .relkind as class_kind,
7+ n .nspname as schema_name
388 from
399 pg_catalog .pg_class c
40- join pg_catalog .pg_namespace n on n .oid = c .relnamespace
10+ left join pg_catalog .pg_namespace n on n .oid = c .relnamespace
4111 where
42- c .relkind in (' v' , ' m' )
43- and n .nspname != ' pg_toast'
44- and n .nspname not like ' pg_temp_%'
45- and n .nspname not like ' pg_toast_temp_%'
46- and has_schema_privilege(n .oid , ' USAGE' ) = true
47- and has_table_privilege(
48- quote_ident(n .nspname ) || ' .' || quote_ident(c .relname ),
49- ' SELECT, INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER'
50- ) = true
51- ) as tbl
52- left join (
53- select
54- attrelid,
55- attname,
56- format_type(atttypid, atttypmod) as data_type,
57- attnum,
58- attisdropped
59- from
60- pg_attribute
61- ) as a on (
62- a .attrelid = tbl .quoted_name :: regclass
63- and a .attnum > 0
64- and not a .attisdropped
65- and has_column_privilege(
66- tbl .quoted_name ,
67- a .attname ,
68- ' SELECT, INSERT, UPDATE, REFERENCES'
69- )
12+ -- r: normal tables
13+ -- v: views
14+ -- m: materialized views
15+ -- f: foreign tables
16+ -- p: partitioned tables
17+ c .relkind in (' r' , ' v' , ' m' , ' f' , ' p' )
7018 )
71- group by
72- schemaname,
73- tablename,
74- quoted_name,
75- is_table;
19+ select
20+ atts .attname as name,
21+ ts .table_name ,
22+ ts .table_oid ,
23+ ts .class_kind ,
24+ ts .schema_name ,
25+ atts .attnum ,
26+ atts .atttypid as type_id,
27+ not atts .attnotnull as is_nullable,
28+ nullif(
29+ information_schema ._pg_char_max_length (atts .atttypid , atts .atttypmod ),
30+ - 1
31+ ) as varchar_length
32+ from
33+ pg_catalog .pg_attribute atts
34+ left join available_tables ts on atts .attrelid = ts .table_oid
35+ where
36+ -- system columns, such as `cmax` or `tableoid`, have negative `attnum`s
37+ atts .attnum >= 0 ;
0 commit comments