Find columns of derived tables (i.e., views and materialized views) where in the creation statement of the table the name of the column has not been specified, i.e., it is generated by the system.
Notes
The query searches a system-defined column name ?column?.
Type
Problem detection (Each row in the result could represent a flaw in the design)
Give a name to the column in the creation statement of the table. Thus, instead of SELECT 1, … FROM write SELECT 1 AS constant, … FROM.
Data Source
system catalog only
SQL Query
WITH derived_tables AS (
SELECT
c.oid,
n.nspname AS schema_name,
c.relname AS table_name,
CASE WHEN c.relkind='v' THEN 'VIEW'
WHEN c.relkind='m' THEN 'MATERIALIZED VIEW' END AS table_type,
at.attname AS column_name,
regexp_replace(pg_get_viewdef(c.oid),'[\r\n]',' ','g') AS view_src
FROM pg_attribute at INNER JOIN pg_class c ON at.attrelid=c.oid
INNER JOIN pg_namespace AS n ON n.oid=c.relnamespace
INNER JOIN pg_authid AS a ON n.nspowner=a.oid
WHERE (nspname='public' OR rolname<>'postgres')
AND c.relkind IN ('v','m')
AND at.attisdropped='f'
AND at.attnum>0
)
SELECT schema_name, table_name, table_type, column_name, view_src
FROM derived_tables
WHERE column_name='?column?'
ORDER BY schema_name, 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
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.
Derived tables
Queries of this category provide information about the derived tables (views, materialized views), which are used to implement virtual data layer.
Naming
Queries of this category provide information about the style of naming.