-
-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathtypes.sql
49 lines (49 loc) · 1.12 KB
/
types.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
select
t.oid::int8 as id,
t.typname as name,
n.nspname as schema,
format_type (t.oid, null) as format,
nullif(t.typbasetype, 0) as base_type_id,
not (t.typnotnull) as is_nullable,
coalesce(t_enums.enums, '[]') as enums,
coalesce(t_attributes.attributes, '[]') as attributes,
obj_description (t.oid, 'pg_type') as comment
from
pg_type t
left join pg_namespace n on n.oid = t.typnamespace
left join (
select
enumtypid,
jsonb_agg(enumlabel order by enumsortorder) as enums
from
pg_enum
group by
enumtypid
) as t_enums on t_enums.enumtypid = t.oid
left join (
select
oid,
jsonb_agg(
jsonb_build_object('name', a.attname, 'type_id', a.atttypid::int8)
order by a.attnum asc
) as attributes
from
pg_class c
join pg_attribute a on a.attrelid = c.oid
where
c.relkind = 'c' and not a.attisdropped
group by
c.oid
) as t_attributes on t_attributes.oid = t.typrelid
where
(
t.typrelid = 0
or (
select
c.relkind = 'c'
from
pg_class c
where
c.oid = t.typrelid
)
)