Goal This query identifies columns in views (derived tables) whose names suggest they store a person's name but lack context about the person's role. In a view, a column containing a person's name should indicate their specific role regarding the entity. For example, a column simply named surname in an active_product view is ambiguous; a more descriptive name, such as registrar_surname, is preferred. However, if the primary purpose of the view is to present personal data, generic names like surname are perfectly acceptable.
Notes The query considers both views and materialized views. The query considers both column names in English and Estonian.
Type Problem detection (Each row in the result could represent a flaw in the design)
Reliability Medium (Medium number of false-positive results)
License MIT License
Fixing Suggestion Add meaningful context to the name. Rename the column. Give to it more descriptive name. Follow a naming convention. To do this one has to drop the derived table and recreate it.
Data Source INFORMATION_SCHEMA+system catalog
SQL Query
WITH columns AS (SELECT table_schema, table_name, 'VIEW' AS table_type, column_name
FROM information_schema.columns 
WHERE (table_schema, table_name) IN (SELECT table_schema, table_name
FROM INFORMATION_SCHEMA.tables WHERE table_type='VIEW')
UNION SELECT nspname, relname, 'MATERIALIZED VIEW' AS table_type, attname
FROM pg_class INNER JOIN pg_namespace ON pg_class.relnamespace=pg_namespace.oid
INNER JOIN pg_attribute ON pg_class.oid=pg_attribute.attrelid
WHERE relkind = 'm' AND attnum>=1 AND attisdropped='f')
SELECT table_schema, table_name, table_type, column_name AS suspected_column_name
FROM columns
WHERE column_name~*'^([[:alpha:]]|_){0,3}(eesnimi|perenimi|isanimi|isiku([_]|[ ])*nimi|inimese([_]|[ ])*nimi|inimene|isik|first([_]|[ ])*name|last([_]|[ ])*name|surname|given([_]|[ ])*name|family([_]|[ ])*name|personal([_]|[ ])*name|person)([[:alpha:]]|_){0,3}$' 
AND table_name!~*'(isik|tootaja|klient|nimi|nimed|person|worker|client|customer|name)'
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)
ORDER BY table_schema, table_name, column_name;

SQL statements that help generate fixes for the identified problem.

SQL Query to Generate FixDescription
SELECT DISTINCT format('DROP VIEW %1$I.%2$I;', table_schema, table_name) AS statements
FROM information_schema.columns 
WHERE (table_schema, table_name) IN (SELECT table_schema, table_name
FROM INFORMATION_SCHEMA.tables WHERE table_type='VIEW')
AND column_name~*'^([[:alpha:]]|_){0,3}(eesnimi|perenimi|isanimi|isiku([_]|[ ])*nimi|inimese([_]|[ ])*nimi|inimene|isik|first([_]|[ ])*name|last([_]|[ ])*name|surname|given([_]|[ ])*name|family([_]|[ ])*name|personal([_]|[ ])*name|person)([[:alpha:]]|_){0,3}$' 
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)
ORDER BY statements;
Drop the view.
SELECT DISTINCT format('DROP MATERIALIZED VIEW %1$I.%2$I;', nspname, relname) AS statements
FROM pg_class INNER JOIN pg_namespace ON pg_class.relnamespace=pg_namespace.oid
INNER JOIN pg_attribute ON pg_class.oid=pg_attribute.attrelid
WHERE relkind = 'm' AND attnum>=1 AND attisdropped='f'
AND attname~*'^([[:alpha:]]|_){0,3}(eesnimi|perenimi|isanimi|isiku([_]|[ ])*nimi|inimese([_]|[ ])*nimi|inimene|isik|first([_]|[ ])*name|last([_]|[ ])*name|surname|given([_]|[ ])*name|family([_]|[ ])*name|personal([_]|[ ])*name|person)([[:alpha:]]|_){0,3}$' 
AND nspname NOT IN (SELECT schema_name
FROM INFORMATION_SCHEMA.schemata
WHERE schema_name<>'public' AND
schema_owner='postgres' AND schema_name IS NOT NULL)
ORDER BY statements;
Drop the materialized view.
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
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.
NamingQueries of this category provide information about the style of naming.

Further reading and related materials:

Reference
The corresponding code smells in case of cleaning code are "N1: Choose Descriptive Names" and "N4: Unambiguous Names". (Robert C. Martin, Clean Code)
The corresponding code problem in case of cleaning code is "Add Meaningful Context". (Robert C. Martin, Clean Code)