The list of all the queries

ON UPDATE CASCADE is not needed (based on surrogate keys)

Query goal: 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 in case of foreign keys that reference to surrogate keys.
Notes about the query: 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.
Query type: Problem detection (Each row in the result could represent a flaw in the design)
Query reliability: High (Few or no false-positive results)
Query license: MIT License
Fixing suggestion: Drop the foreign key constraint and recreate it without the ON UPDATE CASCADE compensating action.
Data source: INFORMATION_SCHEMA+system catalog
SQL query: Click on query to copy it

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='c'),
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 conname,  foreign_schema, foreign_table, foreign_col, target_schema, target_table, target_col 
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 for generating SQL statements that help us to fix the problem

SQL queryDescription
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='c'),
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 where the query belongs to

Collection nameCollection description
Find problems about integrity constraintsA 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 automaticallyQueries, 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 where the query belongs to

Category nameCategory description
Comfortability of data managementQueries 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 actionsQueries of this category provide information about compensating actions of foreign key constraints.
Relationships between tablesQueries 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.

The list of all the queries