Find all referential paths (chains of of parent-child tables that are associated through foreign key constraints) where all foreign key constraints have ON DELETE CASCADE compensating actions. Be careful with too long chains.
Notes
The query finds transitive closure of tables. In other words it finds all possible paths between tables where it is possible to reach from one table to another based on parent-child relationships. The query assumes that these relationships have been enforced in the database by declaring foreign key (referential) constraints. The path should be read from right to left, i.e., parent table is at right and its child table is at left.
Type
General (Overview of some aspect of the database.)
WITH RECURSIVE transitive_closure AS (
WITH foo AS (SELECT o.conrelid AS child, nl.nspname AS child_table_schema, cl.relname AS child_table,
o.confrelid AS parent, nf.nspname AS parent_table_schema, cf.relname AS parent_table
FROM pg_constraint AS o INNER JOIN pg_class AS cl ON o.conrelid=cl.oid
INNER JOIN pg_namespace AS nl ON cl.relnamespace=nl.oid
INNER JOIN pg_class AS cf ON o.confrelid=cf.oid
INNER JOIN pg_namespace AS nf ON cf.relnamespace=nf.oid
WHERE o.contype='f'
AND o.confdeltype='c')
SELECT child, parent, child_table_schema, child_table, parent_table_schema, parent_table, 1 AS depth,
child || '.' || parent || '.' AS path_string,
child_table_schema || '.' || child_table || '/' || parent_table_schema || '.' || parent_table || '/' AS path_string_long
FROM foo
UNION ALL
SELECT tc.child, e.parent, tc.child_table_schema, tc.child_table, e.parent_table_schema, e.parent_table, tc.depth + 1,
tc.path_string || e.parent || '.' AS path_string,
tc.path_string_long || e.parent_table_schema|| '.' || e.parent_table || '/' AS path_string_long
FROM foo AS e INNER JOIN transitive_closure AS tc ON e.child = tc.parent
WHERE tc.path_string NOT LIKE '%' || e.parent || '.%')
SELECT parent_table_schema, parent_table, child_table_schema, child_table, depth, rtrim(path_string_long,'.') AS path
FROM transitive_closure
ORDER BY depth DESC, parent_table_schema, parent_table, child_table_schema, child_table;
Collections
This query belongs to the following collections:
Name
Description
Find problems by overview
Queries that results point to different aspects of database that might have problems. A human reviewer has to decide based on the results as to whether there are problems 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.