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).
Type Problem detection (Each row in the result could represent a flaw in the design)
Reliability Medium (Medium number of false-positive results)
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
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 || ')', ',
' 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

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