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)>1),
ck_grouped as (select ck.conname, ck.target_schema, ck.target_table, string_agg(a_target.attname, ', ' order by a_target.attname) as target_col, ck.consrc
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
cg1.target_schema as schema,
cg1.target_table as table,
cg1.target_col as columns,
cg1.consrc as check_clause1,
cg2.consrc as check_clause2,
cg1.conname AS constraint_name1,
cg2.conname AS constraint_name2
from ck_grouped cg1, ck_grouped cg2
where cg1.target_schema=cg2.target_schema
and cg1.target_table=cg2.target_table
and cg1.conname<>cg2.conname
and cg1.target_col=cg2.target_col
order by cg1.target_schema, cg1.target_table;