WITH fk_columns AS (select target_schema as table_schema, target_table as table_name, a.attname as column_name
from (select
(select nspname from pg_namespace where oid=c.relnamespace) as target_schema,
c.relname as target_table,
c.oid as target_table_oid,
unnest(o.conkey) AS target_col
from pg_constraint o inner join pg_class c on c.oid = o.conrelid
where o.contype = 'f') t
inner join pg_attribute a on t.target_col = a.attnum and t.target_table_oid = a.attrelid and a.attisdropped = false),
columns AS (SELECT table_schema, table_name, column_name
FROM INFORMATION_SCHEMA.columns c
WHERE (table_schema, table_name) IN (SELECT table_schema, table_name
FROM INFORMATION_SCHEMA.tables WHERE table_type='BASE TABLE') AND
table_schema NOT IN (SELECT schema_name
FROM INFORMATION_SCHEMA.schemata
WHERE schema_name<>'public' AND
schema_owner='postgres' AND schema_name IS NOT NULL)
AND c.column_default LIKE 'nextval%')
SELECT format('ALTER TABLE %1$I.%2$I ALTER COLUMN %3$I DROP DEFAULT;', table_schema, table_name, column_name) AS statements
FROM Columns
WHERE EXISTS (SELECT 1
FROM fk_columns
WHERE columns.table_schema=fk_columns.table_schema AND columns.table_name=fk_columns.table_name AND columns.column_name=fk_columns.column_name)
ORDER BY table_schema, table_name; | Drop the reference to an external sequence generator that is directly associated with the column. |
WITH fk_columns AS (select target_schema as table_schema, target_table as table_name, a.attname as column_name
from (select
(select nspname from pg_namespace where oid=c.relnamespace) as target_schema,
c.relname as target_table,
c.oid as target_table_oid,
unnest(o.conkey) AS target_col
from pg_constraint o inner join pg_class c on c.oid = o.conrelid
where o.contype = 'f') t
inner join pg_attribute a on t.target_col = a.attnum and t.target_table_oid = a.attrelid and a.attisdropped = false),
columns AS (SELECT table_schema, table_name, column_name
FROM INFORMATION_SCHEMA.columns c
WHERE (table_schema, table_name) IN (SELECT table_schema, table_name
FROM INFORMATION_SCHEMA.tables WHERE table_type='BASE TABLE') AND
table_schema NOT IN (SELECT schema_name
FROM INFORMATION_SCHEMA.schemata
WHERE schema_name<>'public' AND
schema_owner='postgres' AND schema_name IS NOT NULL)
AND c.is_identity='YES')
SELECT format('ALTER TABLE %1$I.%2$I ALTER COLUMN %3$I DROP IDENTITY;', table_schema, table_name, column_name) AS statements
FROM Columns
WHERE EXISTS (SELECT 1
FROM fk_columns
WHERE columns.table_schema=fk_columns.table_schema AND columns.table_name=fk_columns.table_name AND columns.column_name=fk_columns.column_name)
ORDER BY table_schema, table_name; | Drop the specification that the foreign key column is also the identity column. |
WITH fk_columns AS (select target_schema as table_schema, target_table as table_name, a.attname as column_name
from (select
(select nspname from pg_namespace where oid=c.relnamespace) as target_schema,
c.relname as target_table,
c.oid as target_table_oid,
unnest(o.conkey) AS target_col
from pg_constraint o inner join pg_class c on c.oid = o.conrelid
where o.contype = 'f') t
inner join pg_attribute a on t.target_col = a.attnum and t.target_table_oid = a.attrelid and a.attisdropped = false),
columns AS (SELECT table_schema, table_name, column_name, domain_schema, domain_name
FROM INFORMATION_SCHEMA.columns c
LEFT JOIN information_schema.domains d USING (domain_schema, domain_name)
WHERE (table_schema, table_name) IN (SELECT table_schema, table_name
FROM INFORMATION_SCHEMA.tables WHERE table_type='BASE TABLE') AND
table_schema NOT IN (SELECT schema_name
FROM INFORMATION_SCHEMA.schemata
WHERE schema_name<>'public' AND
schema_owner='postgres' AND schema_name IS NOT NULL)
AND d.domain_default LIKE 'nextval%')
SELECT DISTINCT format('ALTER DOMAIN %1$I.%2$I DROP DEFAULT;', domain_schema, domain_name) AS statements
FROM Columns
WHERE EXISTS (SELECT 1
FROM fk_columns
WHERE columns.table_schema=fk_columns.table_schema AND columns.table_name=fk_columns.table_name AND columns.column_name=fk_columns.column_name)
ORDER BY statements; | Drop the reference to an external sequence generator that is associated with the domain. |