The list of all the queries

ON DELETE SET NULL is probably missing

Query goal: Find implementations of the adjacency list design pattern, where the corresponding foreign key columns are optional but the foreign key constraint does not have ON DELETE SET NULL compensating action. Implement adjacency list correctly.
Query type: Problem detection (Each row in the result could represent a flaw in the design)
Query reliability: Medium (Medium number of false-positive results)
Query license: MIT License
Fixing suggestion: Drop the foreign key constraint and recreate it with the ON DELETE SET NULL compensating action.
Data source: system catalog only
SQL query: Click on query to copy it

with 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,
CASE WHEN o.confdeltype='c' THEN 'CASCADE'
WHEN o.confdeltype='r' THEN 'RESTRICT'
WHEN o.confdeltype='n' THEN 'SET NULL'
WHEN o.confdeltype='d' THEN 'SET DEFAULT'
ELSE 'NO ACTION' END AS on_delete_behavior
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.contype = 'f' and o.confdeltype<>'n'),
fk_grouped as (
select fk.conname, fk.foreign_schema, foreign_table, array_agg(fk.foreign_table || '.' || a_foreign.attname order by  a_foreign.attnum) as foreign_col, 
fk.target_schema, fk.target_table, array_agg(fk.target_table  || '.' || a_target.attname  order by  a_target.attnum) as target_col,
array_agg(a_target.attnotnull order by  a_target.attnum) as target_not_null, fk.on_delete_behavior
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
group by fk.conname, fk.foreign_schema, fk.foreign_table,  fk.target_schema, fk.target_table, fk.on_delete_behavior)
select conname, foreign_schema as schema, foreign_table as table, foreign_col, target_col, on_delete_behavior
from fk_grouped
where foreign_schema=target_schema and foreign_table=target_table and target_not_null @> array['f']::boolean[]
order by target_schema, target_table, conname;

Collections where the query belongs to

Collection nameCollection description
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.
Hierarchical dataQueries of this catergory provide information about storing hierarchical data in the database.
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.
Validity and completenessQueries of this category provide information about whether database design represents the world (domain) correctly (validity) and whether database design captures all the information about the world (domain) that is correct and relevant (completeness).

Reference materials for further reading

Reference
https://blog.duncanworthy.me/sql/hierarchical-data-pt1-adjacency-list/

The list of all the queries