Goal Find generated stored columns in PostgreSQL base tables that duplicate other columns in the table.
Type Problem detection (Each row in the result could represent a flaw in the design)
Reliability High (Few or no false-positive results)
License MIT (opens in new tab)
Fixing Suggestion If you need two columns with the same content but with different names, then use instead a view on top of the table.
Data Source INFORMATION_SCHEMA only
SQL Query
SELECT table_schema, table_name, column_name, generation_expression
FROM INFORMATION_SCHEMA.columns AS c
WHERE (table_schema, table_name) IN (SELECT table_schema, table_name
FROM INFORMATION_SCHEMA.tables WHERE table_type='BASE TABLE') 
AND 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 is_generated='ALWAYS'
AND EXISTS (SELECT *
FROM INFORMATION_SCHEMA.columns AS c2
WHERE c.table_name=c2.table_name
AND c.generation_expression=c2.column_name)
ORDER BY table_schema, table_name, ordinal_position;

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:

Duplication of implementation elements

Queries of this catergory provide information about the duplication of the database objects.

Generated columns

Queries of this category provide information about generated stored base table columns.

NameDescription
Duplication of implementation elementsQueries of this catergory provide information about the duplication of the database objects.
Generated columnsQueries of this category provide information about generated stored base table columns.