@@ -16,10 +16,67 @@ SELECT
16
16
) AS size,
17
17
pg_stat_get_live_tuples(c .oid ) AS live_rows_estimate,
18
18
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
20
25
FROM
21
26
pg_namespace nc
22
27
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 )
23
80
WHERE
24
81
c .relkind IN (' r' , ' p' )
25
82
AND NOT pg_is_other_temp_schema(nc .oid )
31
88
)
32
89
OR has_any_column_privilege(c .oid , ' SELECT, INSERT, UPDATE, REFERENCES' )
33
90
)
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