Goal Find base table columns that belong to a primary key, unique, or foreign key constraint and that name refers to the possibility that these are used to hold surrogate key values. Find the columns where the data type of the column is not an integer type or uuid.
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 License
Fixing Suggestion Choose data types carefully. Be as precise as possible. In PostgreSQL one should use SMALLINT, INTEGER, or BIGINT type in case of storing surrogate key values (ID-s). Change the data type of the column. It could also be that the column name is misleading and should instead contain a word kood or code. In this case changing the data type might not be needed and instead one should rename the column.
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;

Collections

This query belongs to the following collections:

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:

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).