Goal This query identifies derived tables (views and materialized views) whose WHERE clause filters data based on a descriptive name or label (e.g., dname = 'ACCOUNTING') rather than an immutable code or identifier (e.g., deptno = 10 or status_code = 'ACTIVE'). Descriptive names, titles, and labels are designed for human presentation and are inherently mutable—they may change over time due to terminology updates, typo corrections, organizational restructuring, or localization. Hardcoding descriptive names into view predicates creates fragile database logic: if a name is updated in the underlying lookup or base table, the derived table will silently stop matching those rows and return incomplete or empty results without raising an error.
Notes The query considers both column names in Estonian and in English.
Type Problem detection Each row in the result could represent a flaw in the design
Reliability Low Many false-positive results
License MIT (opens in new tab)
Data Source INFORMATION_SCHEMA+system catalog
SQL Query
WITH user_defined_derived_tables AS (
    SELECT 
        views.table_schema, 
        views.table_name, 
        views.view_definition,
        'VIEW' AS table_type
    FROM information_schema.views
    WHERE 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
    )
    UNION 
    SELECT 
        schemaname AS table_schema, 
        matviewname AS table_name, 
        definition AS view_definition, 
        'MATERIALIZED VIEW' AS table_type
    FROM pg_catalog.pg_matviews
    WHERE schemaname NOT IN (
        SELECT schema_name
        FROM INFORMATION_SCHEMA.schemata
        WHERE schema_name <> 'public' 
          AND schema_owner = 'postgres' 
          AND schema_name IS NOT NULL
    )
)
SELECT
    table_schema,
    table_name,
    regexp_replace(
        regexp_replace(
            view_definition,
            '((?<!(LEADING|TRAILING|BOTH)[[:space:]])FROM|ORDER BY|WHERE|GROUP BY|SELECT|HAVING|UNION|EXCEPT|INTERSECT|LIMIT|FETCH FIRST)',
            '<br><b>\1</b>',
            'g'
        ),
        '(LEFT JOIN|RIGHT JOIN|CROSS JOIN|FULL JOIN)',
        '<font color=red>\1</font>',
        'g'
    ) AS def,
    table_type
FROM user_defined_derived_tables
WHERE 
    view_definition ~* '(?<=WHERE.*)(?<!:)\y[[:alnum:]_]*(name|nimetus|nimi|title|label)\y\)?(?:::[[:alnum:]_ ]+)?[[:space:]]*(?:=|<>|!=|~~\*?|~\*?|[[:space:]]IN[[:space:]]*\()[[:space:]]*(?:ANY[[:space:]]*\(\(?ARRAY\[)?''[^'']+'''    
    OR     
    view_definition ~* '(?<=WHERE.*)(?<!:)\y(?![[:alnum:]_]*(code|kood|id)\y)[[:alnum:]_]+\)?(?:::[[:alnum:]_ ]+)?[[:space:]]*(?:=|<>|!=|~~\*?|~\*?|[[:space:]]IN[[:space:]]*\()[[:space:]]*(?:ANY[[:space:]]*\(\(?ARRAY\[)?''[[:alpha:] ]{6,}'''
ORDER BY table_schema, table_name;

Fixing Suggestion

Replace the name-based search condition in the WHERE clause with a condition that filters by the corresponding immutable code or identifier column (for example, filter by deptno = 10 or status_code = 'ACTIVE' instead of dname = 'ACCOUNTING' or status_name = 'Active'). If the underlying table currently lacks a dedicated, immutable code column for business logic references, introduce one rather than relying on display names.

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:

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.

Result quality depends on names

Queries of this category use names (for instance, column names) to try to guess the meaning of a database object. Thus, the goodness of names determines the number of false positive and false negative results.

NameDescription
Comfortability of data managementQueries 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 tablesQueries of this category provide information about the derived tables (views, materialized views), which are used to implement virtual data layer.
Result quality depends on namesQueries of this category use names (for instance, column names) to try to guess the meaning of a database object. Thus, the goodness of names determines the number of false positive and false negative results.