@@ -16,10 +16,67 @@ SELECT
1616 ) AS size,
1717 pg_stat_get_live_tuples(c .oid ) AS live_rows_estimate,
1818 pg_stat_get_dead_tuples(c .oid ) AS dead_rows_estimate,
19- obj_description(c .oid ) AS comment
19+ obj_description(c .oid ) AS comment,
20+ coalesce(pk .primary_keys , ' []' ) as primary_keys,
21+ coalesce(
22+ jsonb_agg(relationships) filter (where relationships is not null ),
23+ ' []'
24+ ) as relationships
2025FROM
2126 pg_namespace nc
2227 JOIN pg_class c ON nc .oid = c .relnamespace
28+ left join (
29+ select
30+ table_id,
31+ jsonb_agg(_pk.* ) as primary_keys
32+ from (
33+ select
34+ n .nspname as schema,
35+ c .relname as table_name,
36+ a .attname as name,
37+ c .oid :: int8 as table_id
38+ from
39+ pg_index i,
40+ pg_class c,
41+ pg_attribute a,
42+ pg_namespace n
43+ where
44+ i .indrelid = c .oid
45+ and c .relnamespace = n .oid
46+ and a .attrelid = c .oid
47+ and a .attnum = any (i .indkey )
48+ and i .indisprimary
49+ ) as _pk
50+ group by table_id
51+ ) as pk
52+ on pk .table_id = c .oid
53+ left join (
54+ select
55+ c .oid :: int8 as id,
56+ c .conname as constraint_name,
57+ nsa .nspname as source_schema,
58+ csa .relname as source_table_name,
59+ sa .attname as source_column_name,
60+ nta .nspname as target_table_schema,
61+ cta .relname as target_table_name,
62+ ta .attname as target_column_name
63+ from
64+ pg_constraint c
65+ join (
66+ pg_attribute sa
67+ join pg_class csa on sa .attrelid = csa .oid
68+ join pg_namespace nsa on csa .relnamespace = nsa .oid
69+ ) on sa .attrelid = c .conrelid and sa .attnum = any (c .conkey )
70+ join (
71+ pg_attribute ta
72+ join pg_class cta on ta .attrelid = cta .oid
73+ join pg_namespace nta on cta .relnamespace = nta .oid
74+ ) on ta .attrelid = c .confrelid and ta .attnum = any (c .confkey )
75+ where
76+ c .contype = ' f'
77+ ) as relationships
78+ on (relationships .source_schema = nc .nspname and relationships .source_table_name = c .relname )
79+ or (relationships .target_table_schema = nc .nspname and relationships .target_table_name = c .relname )
2380WHERE
2481 c .relkind IN (' r' , ' p' )
2582 AND NOT pg_is_other_temp_schema(nc .oid )
3188 )
3289 OR has_any_column_privilege(c .oid , ' SELECT, INSERT, UPDATE, REFERENCES' )
3390 )
91+ group by
92+ c .oid ,
93+ c .relname ,
94+ c .relrowsecurity ,
95+ c .relforcerowsecurity ,
96+ c .relreplident ,
97+ nc .nspname ,
98+ pk .primary_keys
0 commit comments