Goal 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)
Reliability High (Few or no false-positive results)
License MIT License
Fixing Suggestion 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 FixDescription
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:

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