Query goal: | Find generated stored base table columns that expression does not refer to any column of the table. It could be that there will be a constant value in every row in case of this column. The support of generated columns was added to PostgreSQL 12. |
Query type: | Problem detection (Each row in the result could represent a flaw in the design) |
Query reliability: | Medium (Medium number of false-positive results) |
Query license: | MIT License |
Fixing suggestion: | Change the expression of the generated column. |
Data source: | INFORMATION_SCHEMA only |
SQL query: | Click on query to copy it
WITH all_columns AS (SELECT table_schema, table_name, column_name 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='NEVER'), generated_columns AS (SELECT table_schema, table_name, column_name, generation_expression 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'), referred_columns AS ( SELECT a.table_schema, a.table_name, a.column_name, g.generation_expression FROM all_columns AS a INNER JOIN generated_columns AS g ON a.table_schema=g.table_schema AND a.table_name=g.table_name WHERE g.generation_expression LIKE '%' || a.column_name || '%') SELECT table_schema, table_name, column_name, generation_expression FROM generated_columns AS ge WHERE NOT EXISTS (SELECT 1 FROM referred_columns AS r WHERE r.table_schema=ge.table_schema AND r.table_name=ge.table_name AND r.generation_expression=ge.generation_expression) ORDER BY table_schema, table_name; |
Collection name | Collection 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 . |
Category name | Category description |
---|---|
Comfortability of data management | Queries of this category provide information about the means that have been used to make the use or management of database more comfortable and thus, more efficient. |
Generated columns | Queries of this category provide information about generated stored base table columns. |
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). |