The list of all the queries

Using system-defined names of constraints (constraints that involve more than one column)

Query goal: Find the constraint types in case of which there exists system-defined names.
Notes about the query: The query considers only constraints that involve more than one column. PostgreSQL 14 added primary keys, unique constraints, and foreign keys to system catalogs. Thus the query was modified to exclude the results from the system catalog. The query uses regexp_like() function that was added to PostgreSQL 15.
Query type: Problem detection (Each row in the result could represent a flaw in the design)
Query reliability: Medium (Medium number of false-positive results)
Query license: MIT License
Data source: system catalog only
SQL query: Click on query to copy it

with const as (select 
o.conname AS constraint_name, 
nc.nspname as table_schema,
c.relname as table_name, 
c.oid as const_table_oid,
o.conkey AS table_col,
CASE WHEN o.contype='p' THEN 'PRIMARY KEY'
WHEN o.contype='u' THEN 'UNIQUE'
WHEN o.contype='c' THEN 'CHECK'
WHEN o.contype='f' THEN 'FOREIGN KEY'
WHEN o.contype='x' THEN 'EXCLUDE' END AS constraint_type
from pg_constraint o inner join pg_class c on c.oid = o.conrelid
inner join pg_namespace as nc on nc.oid=c.relnamespace
inner join  pg_authid AS a on nc.nspowner=a.oid
where (nc.nspname='public' or rolname<>'postgres')
and o.contype in ('u', 'p','c','f','x')
and cardinality(o.conkey)>1),
const_unnest as (select constraint_name, table_schema, table_name, const_table_oid, table_col, const_col_num, ordin, constraint_type
from const, unnest(const.table_col) with ordinality as k(const_col_num, ordin)),
const_with_names as (select constraint_name, table_schema, table_name, constraint_type, string_agg(a_const.attname, '_' order by ordin) as const_col
from const_unnest k inner join pg_attribute a_const on k.const_col_num = a_const.attnum and k.const_table_oid = a_const.attrelid and a_const.attisdropped = false
group by constraint_name, table_schema, table_name, constraint_type),
stats AS (SELECT constraint_type, Count(*) AS cnt_total, 
Count(*) FILTER (WHERE lower(constraint_name) = lower(table_name) || '_pkey') AS cnt_system_generated
FROM const_with_names
WHERE constraint_type='PRIMARY KEY'
GROUP BY constraint_type

UNION SELECT constraint_type, Count(*) AS cnt_total, 
Count(*) FILTER (WHERE lower(constraint_name) = lower(table_name) || '_' || lower(const_col) || '_key') AS cnt_system_generated
FROM const_with_names
WHERE constraint_type='UNIQUE'
GROUP BY constraint_type

UNION SELECT constraint_type, Count(*) AS cnt_total, 
Count(*) FILTER (WHERE lower(constraint_name) = lower(table_name) || '_' || lower(const_col) || '_excl') AS cnt_system_generated
FROM const_with_names
WHERE constraint_type='EXCLUDE'
GROUP BY constraint_type

UNION SELECT constraint_type, Count(*) AS cnt_total, 
Count(*) FILTER (WHERE regexp_like(constraint_name, '^' || table_name || '_fkey[[:digit:]]*$','i')) AS cnt_system_generated
FROM const_with_names
WHERE constraint_type='FOREIGN KEY'
GROUP BY constraint_type

UNION SELECT constraint_type, Count(*) AS cnt_total, 
Count(*) FILTER (WHERE regexp_like(constraint_name, '^' || table_name || '_check[[:digit:]]*$','i')) AS cnt_system_generated
FROM const_with_names
WHERE constraint_type='CHECK'
GROUP BY constraint_type

)
SELECT constraint_type, cnt_total AS number_of_constraints, cnt_system_generated AS number_of_constraints_with_system_generated_names, cnt_total - cnt_system_generated AS number_of_constraints_with_user_defined_names
FROM stats
WHERE cnt_system_generated>0
ORDER BY cnt_system_generated DESC, constraint_type;

Collections where the query belongs to

Collection nameCollection description
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 where the query belongs to

Category nameCategory description
CHECK constraintsQueries of this category provide information about CHECK constraints.
NamingQueries of this category provide information about the style of naming.
Relationships between tablesQueries of this category provide information about how database tables are connected to each other and whether such connections have been explicitly defined and whether it has been done correctly.
UniquenessQueries of this category provide information about uniqueness constraints (PRIMARY KEY, UNIQUE, EXCLUDE) as well as unique indexes.

Reference materials for further reading

Reference
https://martinfowler.com/bliki/TwoHardThings.html
https://stackoverflow.com/questions/7662/database-table-and-column-naming-conventions
https://openacs.org/doc/eng-standards-constraint-naming
Smell "Using system-generated object names, particularly for constraints": Factor, P.: SQL Code Smells. Redgate. Http://assets.red-gate.com/community/books/sql-code-smells.pdf

The list of all the queries