The list of all the queries

CHECK constraints with the cardinality bigger than one that involve the same set of columns

Query goal: CHECK constraints with the cardinality bigger than one that involve the same set of columns. Make sure that there is no duplication.
Query type: General (Overview of some aspect of the database.)
Query license: MIT License
Fixing suggestion: Drop the duplicate constraints.
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)>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;

Collections where the query belongs to

Collection nameCollection description
Find problems by overviewQueries 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 .

Categories where the query belongs to

Category nameCategory description
CHECK constraintsQueries of this category provide information about CHECK constraints.
Duplication of implementation elementsQueries of this catergory provide information about the duplication of the database objects.

The list of all the queries