Find foreign key constraints that reference to a candidate key that is a surrogate key, i.e., its values are generated by the system by using sequence generators. Do not use ON UPDATE CASCADE, ON UPDATE SET NULL, and ON UPDATE SET DEFAULT in case of foreign keys that reference to surrogate keys.
Notes
The query assumes that the surrogate key is implemented with the help of external or internal sequence generators. In the latter case it means that the key column is the identity column.
Type
Problem detection (Each row in the result could represent a flaw in the design)
Drop the foreign key constraint and recreate it without the ON UPDATE CASCADE/SET NULL/SET DEFAULT compensating action.
Data Source
INFORMATION_SCHEMA+system catalog
SQL Query
WITH surrogate_key AS (SELECT A.table_schema, A.table_name, B.column_name
FROM information_schema.table_constraints A
JOIN information_schema.constraint_column_usage B
ON A.table_schema = B.table_schema
AND A.table_name = B.table_name
AND A.constraint_name = B.constraint_name
AND A.constraint_type IN ('PRIMARY KEY', 'UNIQUE')
INNER JOIN information_schema.columns C
ON B.table_schema=C.table_schema
AND B.table_name = C.table_name
AND B.column_name=C.column_name
INNER JOIN information_schema.schemata S
ON A.table_schema=S.schema_name
WHERE (C.column_default LIKE '%nextval%' OR C.is_identity='YES') AND
(A.table_schema = 'public'
OR S.schema_owner<>'postgres') ),
fk as (select
o.conname,
(select nspname from pg_namespace where oid=f.relnamespace) as foreign_schema,
f.relname as foreign_table,
f.oid as foreign_table_oid,
unnest(o.confkey) AS foreign_col,
(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,
o.confupdtype
from pg_constraint o inner join pg_class c on c.oid = o.conrelid
inner join pg_class f on f.oid = o.confrelid
where o.confmatchtype<>'f' and o.contype = 'f' and o.confupdtype IN ('c','n','d')),
fk_columns as (
select fk.conname, fk.foreign_schema, fk.foreign_table, a_foreign.attname as foreign_col, fk.target_schema, fk.target_table, a_target.attname as target_col,
case when fk.confupdtype='c' then 'ON UPDATE CASCADE'
when fk.confupdtype='n' then 'ON UPDATE SET NULL'
when fk.confupdtype='d' then 'ON UPDATE SET DEFAULT'
end as compensatory_action
from fk inner join pg_attribute a_foreign on fk.foreign_col = a_foreign.attnum and fk.foreign_table_oid = a_foreign.attrelid and a_foreign.attisdropped = false
inner join pg_attribute a_target on fk.target_col = a_target.attnum and fk.target_table_oid = a_target.attrelid and a_target.attisdropped = false)
SELECT conname, foreign_schema, foreign_table, foreign_col, target_schema, target_table, target_col,compensatory_action
FROM fk_columns
WHERE (foreign_schema, foreign_table, foreign_col) IN (SELECT table_schema, table_name, column_name FROM surrogate_key)
ORDER BY target_schema, target_table, target_col;
SQL statements that help generate fixes for the identified problem.
SQL Query to Generate Fix
Description
WITH surrogate_key AS (SELECT A.table_schema, A.table_name, B.column_name
FROM information_schema.table_constraints A
JOIN information_schema.constraint_column_usage B
ON A.table_schema = B.table_schema
AND A.table_name = B.table_name
AND A.constraint_name = B.constraint_name
AND A.constraint_type IN ('PRIMARY KEY', 'UNIQUE')
INNER JOIN information_schema.columns C
ON B.table_schema=C.table_schema
AND B.table_name = C.table_name
AND B.column_name=C.column_name
INNER JOIN information_schema.schemata S
ON A.table_schema=S.schema_name
WHERE (C.column_default LIKE '%nextval%' OR C.is_identity='YES') AND
(A.table_schema = 'public'
OR S.schema_owner<>'postgres') ),
fk as (select
o.conname,
(select nspname from pg_namespace where oid=f.relnamespace) as foreign_schema,
f.relname as foreign_table,
f.oid as foreign_table_oid,
unnest(o.confkey) AS foreign_col,
(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
inner join pg_class f on f.oid = o.confrelid
where o.confmatchtype<>'f' and o.contype = 'f' and o.confupdtype in ('c','n','d')),
fk_columns as (
select fk.conname, fk.foreign_schema, fk.foreign_table, a_foreign.attname as foreign_col, fk.target_schema, fk.target_table, a_target.attname as target_col
from fk inner join pg_attribute a_foreign on fk.foreign_col = a_foreign.attnum and fk.foreign_table_oid = a_foreign.attrelid and a_foreign.attisdropped = false
inner join pg_attribute a_target on fk.target_col = a_target.attnum and fk.target_table_oid = a_target.attrelid and a_target.attisdropped = false)
SELECT format('ALTER TABLE %1$I.%2$I DROP CONSTRAINT %3$I;', target_schema, target_table, conname) AS statements
FROM fk_columns
WHERE (foreign_schema, foreign_table, foreign_col) IN (SELECT table_schema, table_name, column_name FROM surrogate_key)
ORDER BY target_schema, target_table, target_col;
Drop the foreign key constraint.
Collections
This query belongs to the following collections:
Name
Description
Find problems about integrity constraints
A selection of queries that return information about the state of integrity constraints in the datadabase. Contains all the types of queries - problem detection, software measure, and general overview
Find problems automatically
Queries, that results point to problems in the database. Each query in the collection produces an initial assessment. However, a human reviewer has the final say as to whether there is a problem or not .
Categories
This query is classified under the following categories:
Name
Description
Comfortability of data management
Queries of this category provide information about the means that have been used to make the use or management of database more comfortable and thus, more efficient.
Compensating actions
Queries of this category provide information about compensating actions of foreign key constraints.
Relationships between tables
Queries of this category provide information about how database tables are connected to each other and whether such connections have been explicitly defined and whether it has been done correctly.