|
12 | 12 | -- See also https://wiki.postgresql.org/wiki/Don't_Do_This#Don.27t_use_serial
|
13 | 13 | -- and https://stackoverflow.com/questions/55300370/postgresql-serial-vs-identity
|
14 | 14 | with
|
| 15 | + nsp as ( |
| 16 | + select nsp.oid |
| 17 | + from pg_catalog.pg_namespace nsp |
| 18 | + where |
| 19 | + nsp.nspname = :schema_name_param::text |
| 20 | + ), |
| 21 | + |
15 | 22 | t as (
|
16 | 23 | select
|
17 | 24 | col.attrelid::regclass::text as table_name,
|
18 | 25 | col.attname::text as column_name,
|
19 | 26 | col.attnotnull as column_not_null,
|
20 |
| - nsp.nspname as schema_name, |
| 27 | + s.seqrelid::regclass::text as sequence_name, |
21 | 28 | case col.atttypid
|
22 | 29 | when 'int'::regtype then 'serial'
|
23 | 30 | when 'int8'::regtype then 'bigserial'
|
24 | 31 | when 'int2'::regtype then 'smallserial'
|
25 | 32 | end as column_type,
|
26 |
| - pg_get_expr(ad.adbin, ad.adrelid) as column_default_value, |
27 |
| - case |
28 |
| - when has_schema_privilege(nsp.oid, 'create,usage'::text) then pg_get_serial_sequence(col.attrelid::regclass::text, col.attname) |
29 |
| - else null::text |
30 |
| - end as sequence_name |
| 33 | + pg_get_expr(ad.adbin, ad.adrelid) as column_default_value |
31 | 34 | from
|
32 | 35 | pg_catalog.pg_class t
|
33 |
| - inner join pg_catalog.pg_namespace nsp on nsp.oid = t.relnamespace |
| 36 | + inner join nsp on nsp.oid = t.relnamespace |
34 | 37 | inner join pg_catalog.pg_attribute col on col.attrelid = t.oid
|
35 |
| - inner join pg_catalog.pg_attrdef ad on ad.adrelid = col.attrelid and ad.adnum = col.attnum |
36 | 38 | inner join pg_catalog.pg_constraint c on c.conrelid = col.attrelid and col.attnum = any(c.conkey)
|
| 39 | + inner join pg_catalog.pg_attrdef ad on ad.adrelid = col.attrelid and ad.adnum = col.attnum |
| 40 | + inner join pg_catalog.pg_depend dep on dep.refobjid = col.attrelid and dep.refobjsubid = col.attnum |
| 41 | + inner join pg_catalog.pg_sequence s on s.seqrelid = dep.objid |
37 | 42 | where
|
38 | 43 | col.atttypid = any('{int,int8,int2}'::regtype[]) and
|
39 | 44 | not col.attisdropped and
|
40 | 45 | c.contype = 'p' and /* primary keys */
|
41 |
| - nsp.nspname = :schema_name_param::text |
| 46 | + dep.deptype = 'a' /* DEPENDENCY_AUTO */ |
42 | 47 | )
|
43 | 48 |
|
44 | 49 | select
|
45 | 50 | table_name,
|
46 | 51 | column_name,
|
47 | 52 | column_not_null,
|
48 | 53 | column_type,
|
49 |
| - case when schema_name = 'public'::text then replace(sequence_name, 'public.', '') else sequence_name end as sequence_name |
| 54 | + sequence_name |
50 | 55 | from t
|
51 | 56 | where
|
52 |
| - sequence_name is not null and |
53 |
| - column_default_value = 'nextval(''' || sequence_name::regclass || '''::regclass)' |
| 57 | + column_default_value = 'nextval(''' || sequence_name || '''::regclass)' |
54 | 58 | order by table_name, column_name;
|
0 commit comments