7
7
n .nspname as schema_name
8
8
from
9
9
pg_catalog .pg_class c
10
- left join pg_catalog .pg_namespace n on n .oid = c .relnamespace
10
+ join pg_catalog .pg_namespace n on n .oid = c .relnamespace
11
11
where
12
12
-- r: normal tables
13
13
-- v: views
14
14
-- m: materialized views
15
15
-- f: foreign tables
16
16
-- p: partitioned tables
17
17
c .relkind in (' r' , ' v' , ' m' , ' f' , ' p' )
18
+ ),
19
+ available_indexes as (
20
+ select
21
+ unnest (ix .indkey ) as attnum,
22
+ ix .indisprimary as is_primary,
23
+ ix .indisunique as is_unique,
24
+ ix .indrelid as table_oid
25
+ from
26
+ pg_catalog .pg_class c
27
+ join pg_catalog .pg_index ix on c .oid = ix .indexrelid
28
+ where
29
+ c .relkind = ' i'
18
30
)
19
31
select
20
32
atts .attname as name,
@@ -28,10 +40,22 @@ select
28
40
nullif(
29
41
information_schema ._pg_char_max_length (atts .atttypid , atts .atttypmod ),
30
42
- 1
31
- ) as varchar_length
43
+ ) as varchar_length,
44
+ pg_get_expr (def .adbin , def .adrelid ) as default_expr,
45
+ coalesce(ix .is_primary , false) as is_primary_key,
46
+ coalesce(ix .is_unique , false) as is_unique,
47
+ pg_catalog .col_description (ts .table_oid , atts .attnum ) as comment
32
48
from
33
49
pg_catalog .pg_attribute atts
34
- left join available_tables ts on atts .attrelid = ts .table_oid
50
+ join available_tables ts on atts .attrelid = ts .table_oid
51
+ left join available_indexes ix on atts .attrelid = ix .table_oid
52
+ and atts .attnum = ix .attnum
53
+ left join pg_catalog .pg_attrdef def on atts .attrelid = def .adrelid
54
+ and atts .attnum = def .adnum
35
55
where
36
56
-- system columns, such as `cmax` or `tableoid`, have negative `attnum`s
37
- atts .attnum >= 0 ;
57
+ atts .attnum >= 0
58
+ order by
59
+ schema_name desc ,
60
+ table_name,
61
+ attnum;
0 commit comments