The same table should not have multiple CHECK constraints with exactly the same Boolean expression. Do remember that the same task can be solved in SQL usually in multiple different ways. Thus, the exact copies are not the only possible duplication.
Notes
The query only considers CHECK constraints that are directly associated with a table, i.e., not constraints that are associated with a domain. In case of the string_agg function, the line break (br) tag is used as a part of the separator for the better readability in case the query result is displayed in a web browser.
Type
Problem detection (Each row in the result could represent a flaw in the design)
SELECT ccu.table_schema, ccu.table_name, cc.check_clause,
string_agg(DISTINCT cc.constraint_name, '; ') AS constraints,
Count(DISTINCT cc.constraint_name) AS number_of_constraints
FROM INFORMATION_SCHEMA.constraint_column_usage AS ccu INNER JOIN INFORMATION_SCHEMA.check_constraints AS cc
USING (constraint_schema, constraint_name)
WHERE cc.check_clause NOT LIKE '%IS NOT NULL' AND
ccu.table_schema NOT IN (SELECT schema_name
FROM INFORMATION_SCHEMA.schemata
WHERE schema_name<>'public' AND
schema_owner='postgres' AND schema_name IS NOT NULL)
GROUP BY ccu.table_schema, ccu.table_name, cc.check_clause
HAVING Count(DISTINCT cc.constraint_name)>1
ORDER BY ccu.table_schema, ccu.table_name, Count(DISTINCT cc.constraint_name) DESC;
SQL statements that help generate fixes for the identified problem.
SQL Query to Generate Fix
Description
WITH duplicate_checks AS (SELECT ccu.table_schema, ccu.table_name, cc.check_clause,
array_agg(DISTINCT cc.constraint_name) AS constraints_array
FROM INFORMATION_SCHEMA.constraint_column_usage AS ccu INNER JOIN INFORMATION_SCHEMA.check_constraints AS cc
USING (constraint_schema, constraint_name)
WHERE cc.check_clause NOT LIKE '%IS NOT NULL' AND
ccu.table_schema NOT IN (SELECT schema_name
FROM INFORMATION_SCHEMA.schemata
WHERE schema_name<>'public' AND
schema_owner='postgres' AND schema_name IS NOT NULL)
GROUP BY ccu.table_schema, ccu.table_name, cc.check_clause
HAVING Count(DISTINCT cc.constraint_name)>1)
SELECT format ('ALTER TABLE %1$I.%2$I DROP CONSTRAINT %3$I;', table_schema, table_name, unnest(constraints_array)) AS statements
FROM duplicate_checks
ORDER BY table_schema, table_name;
Drop the constraint. One of the constraints must stay in place.
Collections
This query belongs to the following collections:
Name
Description
Find problems about integrity constraints
A selection of queries that return information about the state of integrity constraints in the datadabase. Contains all the types of queries - problem detection, software measure, and general overview
Find problems automatically
Queries, 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:
Name
Description
CHECK constraints
Queries of this category provide information about CHECK constraints.
Duplication of implementation elements
Queries of this catergory provide information about the duplication of the database objects.