The list of all the queries

Multicolumn CHECK constraints with with inconsistent Boolean expressions

Query goal: Find CHECK constraints that involve two columns, i.e., the cardinality of the constraint is 2, the columns have the same name in different tables, and the Boolean expressions of these constraints are different. For instance, in one table it is last_change_time>=reg_time and in another table it is not (reg_time>last_change_time).
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: If the constraints have to enforce the same rule in case of different tables, then try to use the same Boolean expression.
Data source: system catalog only
SQL query: Click on query to copy it

with 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,
substring(pg_get_constraintdef(o.oid),7) as consrc
from pg_constraint o inner join pg_class c on c.oid = o.conrelid
where o.contype = 'c' 
and cardinality(o.conkey)=2),
ck_names as (select ck.conname, ck.target_schema, ck.target_table, ck.consrc, string_agg(a_target.attname, ',' ORDER BY a_target.attname) as target_cols 
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.consrc)
SELECT target_cols, string_agg (target_schema || '.' || target_table || '.' || conname || '.(' || consrc || ')', ',<br>' ORDER BY target_schema, target_table, conname) AS tables
FROM ck_names
GROUP BY target_cols
HAVING Count(DISTINCT consrc)>1
ORDER BY target_cols;

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.
Comfortability of database evolutionQueries of this category provide information about the means that influence database evolution.
InconsistenciesQueries of this catergory provide information about inconsistencies of solving the same problem in different places.

The list of all the queries