Query goal: | Find CHECK constraints that have been defined in a supertable (parent table) but not in its subtables. An entity that belongs to a subtype should also belong to its supertype. If a subtype entity satisfies some constraint, then logically it must also satisfy the constraints of the supertype as well. If CHECK constraints are not inherited, then this is not guaranteed. If you implement subtyping not merely reuse implementation in the subtables, then the subtables must have at least the same CHECK constraints as the supertable. CHECK(false) on a supertable is an appropriate CHECK constraint if one wants to prevent registering data directly to the supertable, i.e., data can only be added to the subtables. |
Notes about the query: | The query finds also check constraints where the cardinality is zero, i.e., it does not involve any columns (for instance: CHECK(false)). |
Query type: | General (Overview of some aspect of the database.) |
Query license: | MIT License |
Fixing suggestion: | Define the supertable CHECK constraint as inherited. |
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, pg_get_constraintdef(o.oid) AS constraint_def from pg_constraint o inner join pg_class c on c.oid = o.conrelid where o.contype='c' and connoinherit='true'), constr_unnest as (select constraint_name, supertable_schema, supertable, supertable_oid, constraint_col, constraint_col_num, ordin, constraint_def from constr, unnest(constr.constraint_col) with ordinality as f(constraint_col_num, ordin) where constraint_col IS NOT NULL), constr_with_names as (select constraint_name, supertable_schema, supertable, array_agg(a_target.attname order by ordin) as constraint_col, supertable_oid, constraint_def from constr_unnest constr left 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 union select constraint_name, supertable_schema, supertable, NULL as constraint_col, supertable_oid, constraint_def from constr where constraint_col IS NULL) select constraint_name, supertable_schema, supertable, constraint_def, (select string_agg(pc.nspname || '.' || c.relname,';<br>' ORDER BY pc.nspname, c.relname) AS cols from pg_inherits pi inner join pg_class c on pi.inhrelid=c.oid inner join pg_namespace pc on c.relnamespace=pc.oid where pi.inhparent=constr_with_names.supertable_oid) as tables_without_the_constraint from constr_with_names order by supertable_schema, supertable, constraint_name; |
Collection name | Collection 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 . |
Category name | Category description |
---|---|
CHECK constraints | Queries of this category provide information about CHECK constraints. |
Table inheritance | Queries of this category provide information about the inheritance between base tables. |
Reference |
---|
https://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) |
https://www.postgresql.org/docs/current/tutorial-inheritance.html |