Find base tables that have more than one stored generated column with the same expression. The support of generated columns was added to PostgreSQL 12. Do remember that the same task can be solved in SQL usually in multiple different ways. Thus, the exact copies are not the only possible duplication.
Type
Problem detection (Each row in the result could represent a flaw in the design)
All but one are redundant. Drop the redundant stored generated columns.
Data Source
INFORMATION_SCHEMA only
SQL Query
SELECT table_schema, table_name, generation_expression, Count(*) AS number_of_occurrences, string_agg(column_name, '; ' ORDER BY column_name) AS columns
FROM INFORMATION_SCHEMA.columns
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'
GROUP BY table_schema, table_name, generation_expression
HAVING Count(*)>1
ORDER BY Count(*)>1 DESC, table_schema, table_name;
SQL statements that help generate fixes for the identified problem.
SQL Query to Generate Fix
Description
WITH duplicate_columns AS (SELECT table_schema, table_name, generation_expression, array_agg(column_name) AS columns
FROM INFORMATION_SCHEMA.columns
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'
GROUP BY table_schema, table_name, generation_expression
HAVING Count(*)>1)
SELECT DISTINCT format('ALTER TABLE %1$I.%2$I DROP COLUMN %3$I;', table_schema, table_name, unnest(columns)) AS statements
FROM duplicate_columns
ORDER BY statements;
Drop the column. One of the columns must stay in place.
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
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.