Query goal: | Find names of foreign key constraints that are used within the same schema more than once. Find names of check constraints that are used within the same schema more than once. Find names of constraint triggers that are used within the same schema more than once. Different things should have different names. But here different constraints have the same name. Also make sure that this is not a sign of duplication. |
Notes about the query: | 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. The query considers only constraints that are connected directly with a base table or a foreign table, i.e., it does not consider domain CHECK constraints. It is entirely normal that a domain has a check constraint and the domain is used in case of multiple columns. The query considers only locally-defined constraints, i.e., constraints that are not inherited. The query does not consider constraint triggers becasuse these could be used to implement foreign key constraints and the same name could be used in case of different triggers that implement the same constraint. |
Query type: | Problem detection (Each row in the result could represent a flaw in the design) |
Query reliability: | High (Few or no false-positive results) |
Query license: | MIT License |
Fixing suggestion: | Give to foreign key and check constraints as well as to constraint triggers unique names within a schema. Use table name in the constraint name. Follow a naming convention. |
Data source: | system catalog only |
SQL query: | Click on query to copy it
with constraints as (select distinct o.conname, (select nspname from pg_namespace where oid=c.relnamespace) as table_schema, c.relname as table_name, case when o.contype='c' then 'CHECK' when o.contype='f' then 'FOREIGN KEY' when contype='t' THEN 'CONSTRAINT TRIGGER' end as constraint_type, case when c.relkind='r' then 'BASE TABLE' when c.relkind='f' then 'FOREIGN TABLE' end as table_type from pg_constraint o inner join pg_class c on c.oid = o.conrelid where c.relkind in ('r','f') and o.contype in ('f', 'c') and o.conislocal=true) select table_schema, constraint_type, conname, count(*) AS number_of_occurrences, string_agg(table_type || '.' || table_schema || '.' || table_name, ';<br>' order by table_schema, table_name) as tables from constraints group by table_schema, constraint_type, conname having count(*)>1 order by count(*) desc, table_schema, constraint_type; |
Collection name | Collection description |
---|---|
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 . |
Category name | Category description |
---|---|
Comfortability of database evolution | Queries of this category provide information about the means that influence database evolution. |
Naming | Queries of this category provide information about the style of naming. |
Uniqueness | Queries of this category provide information about uniqueness constraints (PRIMARY KEY, UNIQUE, EXCLUDE) as well as unique indexes. |