The list of all the queries

Not inherited CHECK constraints that are recreated in the immediate subtable

Query goal: Find base table CHECK constraints that have been defined as NOT INHERITED but the constraint with the same Boolean expression has been defined in the immediate subtable of the table.
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: Define the CHECK constraint of the supertable as inherited constraint.
Data source: system catalog only
SQL query: Click on query to copy it

with constr as (select
o.conname as constraint_name,
(select nspname from pg_namespace where oid=c.relnamespace) as supertable_schema,
c.relname as supertable,
c.oid as supertable_oid,
o.conkey AS constraint_col,
regexp_replace(pg_get_constraintdef(o.oid),'( NO INHERIT)','') AS constraint_def,
o.connoinherit
from pg_constraint o inner join pg_class c on c.oid = o.conrelid
where o.contype='c'),
constr_unnest as (select constraint_name, supertable_schema, supertable, supertable_oid, constraint_col, constraint_col_num, ordin, constraint_def, connoinherit
from constr, unnest(constr.constraint_col) with ordinality as f(constraint_col_num, ordin)),
constr_with_names as (select constraint_name, supertable_schema, supertable, supertable_oid, array_agg(a_target.attname order by ordin) as constraint_col , constraint_def, connoinherit
from constr_unnest constr inner join pg_attribute a_target on constr.constraint_col_num = a_target.attnum and constr.supertable_oid = a_target.attrelid and a_target.attisdropped = false
group by constraint_name, supertable_schema, supertable, supertable_oid, constraint_def, connoinherit),
supertable_constraints as (select constraint_name, supertable_schema, supertable, constraint_col, pc.nspname as subtable_schema, c.relname as subtable_name, constraint_def, connoinherit
from constr_with_names inner join pg_inherits pi on pi.inhparent=constr_with_names.supertable_oid
inner join pg_class c on pi.inhrelid=c.oid
inner join pg_namespace pc on c.relnamespace=pc.oid)
SELECT sc.supertable_schema, sc.supertable, sc.constraint_col, sc.constraint_def, subtable_schema, subtable_name, sc.constraint_name AS supertable_constraint_name,  cn.constraint_name AS subtable_constraint_name
FROM supertable_constraints as sc, constr_with_names AS cn
WHERE sc.subtable_schema=cn.supertable_schema 
AND sc.subtable_name=cn.supertable
AND sc.constraint_def=cn.constraint_def
AND sc.connoinherit='true'
order by sc.supertable_schema, sc.supertable, sc.constraint_col;

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
CHECK constraintsQueries of this category provide information about CHECK constraints.
Table inheritanceQueries of this category provide information about the inheritance between base tables.

Reference materials for further reading

Reference
https://www.postgresql.org/docs/current/tutorial-inheritance.html

The list of all the queries