Skip to content

Commit 19e50d7

Browse files
authored
Fix for permission denied error (#68)
* [POSSIBLE_OBJECT_NAME_OVERFLOW] Enhance support for partitioned tables/indexes * Fix for permission denied error * Fix linter error
1 parent eebce6a commit 19e50d7

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

sql/possible_object_name_overflow.sql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,16 @@ with
3939
when 'S' then 'sequence'
4040
when 'v' then 'view'
4141
when 'm' then 'materialized view'
42+
when 'p' then 'partitioned table'
43+
when 'I' then 'partitioned index'
4244
end as object_type
4345
from
4446
pg_catalog.pg_class pc
4547
inner join nsp on nsp.oid = pc.relnamespace
4648
inner join t on t.max_identifier_length = length(pc.relname)
4749
where
48-
pc.relkind in ('r', 'i', 'S', 'v', 'm')
50+
pc.relkind in ('r', 'i', 'S', 'v', 'm', 'p', 'I')
51+
/* decided not to filter by the pc.relispartition field here */
4952

5053
union all
5154

sql/primary_keys_with_serial_types.sql

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,47 @@
1212
-- See also https://wiki.postgresql.org/wiki/Don't_Do_This#Don.27t_use_serial
1313
-- and https://stackoverflow.com/questions/55300370/postgresql-serial-vs-identity
1414
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+
1522
t as (
1623
select
1724
col.attrelid::regclass::text as table_name,
1825
col.attname::text as column_name,
1926
col.attnotnull as column_not_null,
20-
nsp.nspname as schema_name,
27+
s.seqrelid::regclass::text as sequence_name,
2128
case col.atttypid
2229
when 'int'::regtype then 'serial'
2330
when 'int8'::regtype then 'bigserial'
2431
when 'int2'::regtype then 'smallserial'
2532
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
3134
from
3235
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
3437
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
3638
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
3742
where
3843
col.atttypid = any('{int,int8,int2}'::regtype[]) and
3944
not col.attisdropped and
4045
c.contype = 'p' and /* primary keys */
41-
nsp.nspname = :schema_name_param::text
46+
dep.deptype = 'a' /* DEPENDENCY_AUTO */
4247
)
4348

4449
select
4550
table_name,
4651
column_name,
4752
column_not_null,
4853
column_type,
49-
case when schema_name = 'public'::text then replace(sequence_name, 'public.', '') else sequence_name end as sequence_name
54+
sequence_name
5055
from t
5156
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)'
5458
order by table_name, column_name;

0 commit comments

Comments
 (0)