Goal Find not valid foreign key constraints. These constraints have been created so that the existing data has not been checked against the constraint. It could be deliberate in case of legacy systems that have data quality problems. However, ideally all the data in the table conforms to the constraint.
Type Problem detection (Each row in the result could represent a flaw in the design)
Reliability High (Few or no false-positive results)
License MIT License
Fixing Suggestion Validate the constraint after fixing the data problems.
Data Source system catalog only
SQL Query
SELECT (select nspname from pg_namespace where oid=c.relnamespace) AS table_schema,
c.relname AS table_name,
o.conname AS constraint_name
FROM pg_constraint o INNER JOIN pg_class c ON c.oid = o.conrelid
WHERE o.contype = 'f' AND o.convalidated=false
ORDER BY table_schema, c.relname, o.conname;

SQL statements that help generate fixes for the identified problem.

SQL Query to Generate FixDescription
WITH fk AS (SELECT (select nspname from pg_namespace where oid=c.relnamespace) AS table_schema,
c.relname AS table_name,
o.conname AS constraint_name
FROM pg_constraint o INNER JOIN pg_class c ON c.oid = o.conrelid
WHERE o.contype = 'f' AND o.convalidated=false)
SELECT format('ALTER TABLE %1$I.%2$I VALIDATE CONSTRAINT %3$I;', table_schema, table_name, constraint_name) AS statements
FROM fk
ORDER BY table_schema, table_name, constraint_name;
Validate the constraint.
Collections

This query belongs to the following collections:

NameDescription
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

This query is classified under the following categories:

NameDescription
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.