Skip to content

Commit

Permalink
Fix for permission denied error (#68)
Browse files Browse the repository at this point in the history
* [POSSIBLE_OBJECT_NAME_OVERFLOW] Enhance support for partitioned tables/indexes

* Fix for permission denied error

* Fix linter error
  • Loading branch information
mfvanek authored Dec 20, 2024
1 parent eebce6a commit 19e50d7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
5 changes: 4 additions & 1 deletion sql/possible_object_name_overflow.sql
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,16 @@ with
when 'S' then 'sequence'
when 'v' then 'view'
when 'm' then 'materialized view'
when 'p' then 'partitioned table'
when 'I' then 'partitioned index'
end as object_type
from
pg_catalog.pg_class pc
inner join nsp on nsp.oid = pc.relnamespace
inner join t on t.max_identifier_length = length(pc.relname)
where
pc.relkind in ('r', 'i', 'S', 'v', 'm')
pc.relkind in ('r', 'i', 'S', 'v', 'm', 'p', 'I')
/* decided not to filter by the pc.relispartition field here */

union all

Expand Down
28 changes: 16 additions & 12 deletions sql/primary_keys_with_serial_types.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,47 @@
-- See also https://wiki.postgresql.org/wiki/Don't_Do_This#Don.27t_use_serial
-- and https://stackoverflow.com/questions/55300370/postgresql-serial-vs-identity
with
nsp as (
select nsp.oid
from pg_catalog.pg_namespace nsp
where
nsp.nspname = :schema_name_param::text
),

t as (
select
col.attrelid::regclass::text as table_name,
col.attname::text as column_name,
col.attnotnull as column_not_null,
nsp.nspname as schema_name,
s.seqrelid::regclass::text as sequence_name,
case col.atttypid
when 'int'::regtype then 'serial'
when 'int8'::regtype then 'bigserial'
when 'int2'::regtype then 'smallserial'
end as column_type,
pg_get_expr(ad.adbin, ad.adrelid) as column_default_value,
case
when has_schema_privilege(nsp.oid, 'create,usage'::text) then pg_get_serial_sequence(col.attrelid::regclass::text, col.attname)
else null::text
end as sequence_name
pg_get_expr(ad.adbin, ad.adrelid) as column_default_value
from
pg_catalog.pg_class t
inner join pg_catalog.pg_namespace nsp on nsp.oid = t.relnamespace
inner join nsp on nsp.oid = t.relnamespace
inner join pg_catalog.pg_attribute col on col.attrelid = t.oid
inner join pg_catalog.pg_attrdef ad on ad.adrelid = col.attrelid and ad.adnum = col.attnum
inner join pg_catalog.pg_constraint c on c.conrelid = col.attrelid and col.attnum = any(c.conkey)
inner join pg_catalog.pg_attrdef ad on ad.adrelid = col.attrelid and ad.adnum = col.attnum
inner join pg_catalog.pg_depend dep on dep.refobjid = col.attrelid and dep.refobjsubid = col.attnum
inner join pg_catalog.pg_sequence s on s.seqrelid = dep.objid
where
col.atttypid = any('{int,int8,int2}'::regtype[]) and
not col.attisdropped and
c.contype = 'p' and /* primary keys */
nsp.nspname = :schema_name_param::text
dep.deptype = 'a' /* DEPENDENCY_AUTO */
)

select
table_name,
column_name,
column_not_null,
column_type,
case when schema_name = 'public'::text then replace(sequence_name, 'public.', '') else sequence_name end as sequence_name
sequence_name
from t
where
sequence_name is not null and
column_default_value = 'nextval(''' || sequence_name::regclass || '''::regclass)'
column_default_value = 'nextval(''' || sequence_name || '''::regclass)'
order by table_name, column_name;

0 comments on commit 19e50d7

Please sign in to comment.