The list of all the queries

Perhaps a relationship should be irreflexive

Query goal: Enforce all the constraints. A binary relation is called irreflexive, if it does not relate any element to itself.
Notes about the query: The query finds implementations of the adjacency list design pattern where the corresponding table does not have a CHECK constraint that ensures in case of each row that the candidate key value is not equal with the foreign key column.
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: Add a check constraint that relates the candidate key and foreign key columns.
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
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' ),
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
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_final as (select conname, foreign_schema as fschema, foreign_table as ftable, case when foreign_col@>target_col and foreign_col<@target_col then target_col else foreign_col || target_col end as columns
from fk_grouped
where foreign_schema=target_schema and foreign_table=target_table),

ck as (select 
o.conname,
(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 = 'c'  and cardinality(o.conkey)>1),
ck_grouped as (
select ck.conname, ck.target_schema, ck.target_table, array_agg(ck.target_table  || '.' || a_target.attname  order by  a_target.attnum) as target_col
from ck inner join pg_attribute a_target on ck.target_col = a_target.attnum and ck.target_table_oid = a_target.attrelid and a_target.attisdropped = false
group by ck.conname, ck.target_schema, ck.target_table),
ck_final as (select conname, target_schema as cschema, target_table as ctable, target_col as columns
from ck_grouped)

select conname, fschema as schema, ftable as table, columns
from fk_final
where not exists (select 1 
from ck_final
where fk_final.fschema=ck_final.cschema and fk_final.ftable=ck_final.ctable and fk_final.columns@>ck_final.columns and fk_final.columns<@ck_final.columns)
order by fschema, ftable;

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
CHECK constraintsQueries of this category provide information about CHECK 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://en.wikipedia.org/wiki/Reflexive_relation
https://blog.duncanworthy.me/sql/hierarchical-data-pt1-adjacency-list/

The list of all the queries