From 19e50d784cdb2b53ac4d334043d8318f568971d4 Mon Sep 17 00:00:00 2001 From: Ivan Vakhrushev Date: Fri, 20 Dec 2024 16:26:29 +0400 Subject: [PATCH] Fix for permission denied error (#68) * [POSSIBLE_OBJECT_NAME_OVERFLOW] Enhance support for partitioned tables/indexes * Fix for permission denied error * Fix linter error --- sql/possible_object_name_overflow.sql | 5 ++++- sql/primary_keys_with_serial_types.sql | 28 +++++++++++++++----------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/sql/possible_object_name_overflow.sql b/sql/possible_object_name_overflow.sql index 2e7cb27..19e3420 100644 --- a/sql/possible_object_name_overflow.sql +++ b/sql/possible_object_name_overflow.sql @@ -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 diff --git a/sql/primary_keys_with_serial_types.sql b/sql/primary_keys_with_serial_types.sql index e4ef088..718b2bf 100644 --- a/sql/primary_keys_with_serial_types.sql +++ b/sql/primary_keys_with_serial_types.sql @@ -12,33 +12,38 @@ -- 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 @@ -46,9 +51,8 @@ select 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;