Do not create unnecessary tables. If a table has cardinality 1 (one column), then most probably the values in this column should not be system generated unique values.
Notes
The query takes into account both external and internal (are created as the result of declaring a column as the identity column) sequence generators. The query takes into account a possibility that a column may be associated with a sequence generator through a domain.
Type
Problem detection (Each row in the result could represent a flaw in the design)
Drop the column association with the sequence generator.
Data Source
INFORMATION_SCHEMA only
SQL Query
WITH tables_with_a_surrogate AS (SELECT table_schema, table_name
FROM ((information_schema.table_constraints tc
INNER JOIN information_schema.constraint_column_usage cu
USING (table_schema, table_name, constraint_name))
INNER JOIN information_schema.columns c
USING (table_schema, table_name, column_name))
LEFT JOIN information_schema.domains d USING (domain_schema, domain_name)
INNER JOIN information_schema.schemata s
ON tc.table_schema=s.schema_name
WHERE tc.constraint_type IN ('PRIMARY KEY','UNIQUE') AND
(coalesce (c.column_default, d.domain_default) ILIKE '%nextval%' OR C.is_identity='YES')
AND (tc.table_schema = 'public'
OR S.schema_owner<>'postgres'))
SELECT table_schema, table_name
FROM tables_with_a_surrogate AS tws
WHERE (SELECT Count(*) AS cnt FROM information_schema.columns AS c WHERE c.table_schema=tws.table_schema AND c.table_name=tws.table_name)=1
ORDER BY table_schema, table_name ;
Collections
This query belongs to the following collections:
Name
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 .
Categories
This query is classified under the following categories:
Name
Description
Default value
Queries of this catergory provide information about the use of default values.
Sequence generators
Queries of this category provide information about sequence generators and their usage.
Uniqueness
Queries of this category provide information about uniqueness constraints (PRIMARY KEY, UNIQUE, EXCLUDE) as well as unique indexes.
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).