Goal This query identifies base table columns participating in a PRIMARY KEY, UNIQUE, or FOREIGN KEY constraint whose names suggest they store surrogate key values (having an id prefix or suffix), yet are defined with a data type other than an integer type (smallint, integer, bigint) or uuid. Surrogate keys are system-generated identifiers carrying no business meaning and should be stored using compact, fixed-width integer types or native uuid values. Storing surrogate keys as text (varchar, text) or numeric types increases storage overhead, introduces unnecessary string collation costs, and slows down index lookups and table joins.
Type Problem detection Each row in the result could represent a flaw in the design
Reliability Medium Medium number of false-positive results
License MIT (opens in new tab)
Data Source INFORMATION_SCHEMA+system catalog
SQL Query
WITH key AS (SELECT 
(SELECT nspname FROM pg_namespace WHERE oid=c.relnamespace) AS key_schema,
c.relname AS key_table, 
o.conkey AS key_col,
c.oid AS key_table_oid
FROM pg_constraint o INNER JOIN pg_class c ON c.oid = o.conrelid
WHERE o.contype IN ('f','u','p')),
key_unnest AS (SELECT key_schema, key_table, key_table_oid, key_col, key_col_num, ordin
FROM key, unnest(key.key_col) with ordinality AS k(key_col_num, ordin)),
key_with_names AS (SELECT key_schema, key_table, a_key.attname AS key_col
FROM key_unnest k INNER JOIN pg_attribute a_key ON k.key_col_num = a_key.attnum AND k.key_table_oid = a_key.attrelid AND a_key.attisdropped = FALSE)
SELECT table_schema, table_name, column_name, data_type
FROM INFORMATION_SCHEMA.columns AS c
WHERE 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)
AND (table_schema, table_name) IN (SELECT table_schema, table_name
FROM INFORMATION_SCHEMA.tables
WHERE table_type='BASE TABLE')
AND EXISTS (SELECT 1
FROM key_with_names AS kwn
WHERE c.table_schema=kwn.key_schema
AND c.table_name=kwn.key_table
AND c.column_name=kwn.key_col)
AND column_name~*'(_id$|^id_|^id$)'
AND column_name!~*'code'
AND data_type NOT IN ('integer', 'smallint','bigint','uuid')
ORDER BY table_schema, table_name, ordinal_position;

Fixing Suggestion

Migrate true surrogate key columns (and their referencing foreign keys) from text or numeric types to an integer type (integer/bigint) or uuid for better storage efficiency and join performance. If the column intentionally stores an external natural identifier (like national_id), consider renaming it (e.g., using a _code suffix) or excluding it.

Collections

This query belongs to the following collections:

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 .

NameDescription
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

This query is classified under the following categories:

Data types

Queries of this category provide information about the data types and their usage.

Result quality depends on names

Queries of this category use names (for instance, column names) to try to guess the meaning of a database object. Thus, the goodness of names determines the number of false positive and false negative results.

Validity and completeness

Queries of this category provide information about whether database design represents the world (domain) correctly (validity) and whether database design captures all the information about the world (domain) that is correct and relevant (completeness).

NameDescription
Data typesQueries of this category provide information about the data types and their usage.
Result quality depends on namesQueries of this category use names (for instance, column names) to try to guess the meaning of a database object. Thus, the goodness of names determines the number of false positive and false negative results.
Validity and completenessQueries of this category provide information about whether database design represents the world (domain) correctly (validity) and whether database design captures all the information about the world (domain) that is correct and relevant (completeness).