The list of all the queries

System-generated table constraint names (constraints that involve one column)

Query goal: Find the names of database constraints that have been system-generated. Additional restrictions are that the constraints must involve only one column and are associated directly with a table (not through a domain). Names should follow the same style. If there is a mix of system-generated and user-defined names, then the style is most probably different.
Notes about the query: The query considers only constraints that involve one column.
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: Use consistent style of naming things. Give names to the constraints explicitly in the statements of data definition language.
Data source: system catalog only
SQL query: Click on query to copy it

WITH pkey_constraint_names AS (
select 
'PRIMARY KEY' AS type,
o.conname AS constraint_name,
n.nspname as schema_name,
c.relname as table_name, 
(select a.attname from pg_attribute a where a.attrelid = c.oid and a.attnum = o.conkey[1] and a.attisdropped = false) as column_name
from pg_constraint o inner join pg_class c on c.oid = o.conrelid 
inner join pg_namespace AS n on n.oid=c.relnamespace
inner join pg_authid AS a ON n.nspowner=a.oid
where (n.nspname='public' OR a.rolname<>'postgres')
and o.contype ='p'  and cardinality(o.conkey)=1),

ukey_constraint_names AS (
select 
'UNIQUE' AS type,
o.conname AS constraint_name,
n.nspname as schema_name,
c.relname as table_name, 
(select a.attname from pg_attribute a where a.attrelid = c.oid and a.attnum = o.conkey[1] and a.attisdropped = false) as column_name
from pg_constraint o inner join pg_class c on c.oid = o.conrelid 
inner join pg_namespace AS n on n.oid=c.relnamespace
inner join pg_authid AS a ON n.nspowner=a.oid
where (n.nspname='public' OR a.rolname<>'postgres')
and o.contype ='u'  and cardinality(o.conkey)=1),

chk_constraint_names AS (select 
'CHECK' AS type,
o.conname AS constraint_name,
(select nspname from pg_namespace where oid=c.relnamespace) as schema_name,
c.relname as table_name, 
(select a.attname from pg_attribute a where a.attrelid = c.oid and a.attnum = o.conkey[1] and a.attisdropped = false) as column_name
from pg_constraint o inner join pg_class c on c.oid = o.conrelid 
inner join pg_namespace AS n on n.oid=c.relnamespace
inner join pg_authid AS a ON n.nspowner=a.oid
where (n.nspname='public' OR a.rolname<>'postgres')
and o.contype ='c'  and cardinality(o.conkey)=1),

fk_constraint_names AS (select 
'FOREIGN KEY' AS type,
o.conname AS constraint_name,
n.nspname as schema_name,
c.relname as table_name, 
(select a.attname from pg_attribute a where a.attrelid = c.oid and a.attnum = o.conkey[1] and a.attisdropped = false) as column_name
from pg_constraint o inner join pg_class c on c.oid = o.conrelid 
inner join pg_namespace AS n on n.oid=c.relnamespace
inner join pg_authid AS a ON n.nspowner=a.oid
where (n.nspname='public' OR a.rolname<>'postgres')
and o.contype ='f'  and cardinality(o.conkey)=1),

exclude_constraint_names AS (select 
'EXCLUDE' AS type,
o.conname AS constraint_name,
n.nspname as schema_name,
c.relname as table_name, 
(select a.attname from pg_attribute a where a.attrelid = c.oid and a.attnum = o.conkey[1] and a.attisdropped = false) as column_name
from pg_constraint o inner join pg_class c on c.oid = o.conrelid 
inner join pg_namespace AS n on n.oid=c.relnamespace
inner join pg_authid AS a ON n.nspowner=a.oid
where (n.nspname='public' OR a.rolname<>'postgres')
and o.contype ='x'  and cardinality(o.conkey)=1)

SELECT schema_name, table_name, column_name, type, constraint_name
FROM pkey_constraint_names
WHERE lower(constraint_name) = lower(table_name) || '_pkey'
UNION SELECT schema_name, table_name, column_name, type, constraint_name
FROM ukey_constraint_names
WHERE lower(constraint_name) = lower(table_name) || '_' || lower(column_name) || '_key'
UNION SELECT schema_name, table_name, column_name, type, constraint_name
FROM chk_constraint_names
WHERE lower(constraint_name) = lower(table_name) || '_' || lower(column_name) || '_check'
UNION SELECT schema_name, table_name, column_name, type, constraint_name
FROM fk_constraint_names
WHERE lower(constraint_name) = lower(table_name) || '_' || lower(column_name) || '_fkey'
UNION SELECT schema_name, table_name, column_name, type, constraint_name
FROM exclude_constraint_names
WHERE lower(constraint_name) = lower(table_name) || '_' || lower(column_name) || '_excl'
ORDER BY schema_name, table_name, type, constraint_name;

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.
Comfortability of database evolutionQueries of this category provide information about the means that influence database evolution.
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://digikogu.taltech.ee/et/item/05157bb4-763e-4a9d-bc50-2025692b8e1d
https://pgxn.org/dist/constr_name_unif
https://github.com/katrinaibast/constr_name_unif

The list of all the queries