The list of all the queries

Names of columns that hold personal names but do not take into account cultural diversity

Query goal: Find columns of tables (base tables, views, materialized views, foreign tables) that have the name first_name or last_name. Such column names do not take into account that different cultures use different personal name components and the number of possible components is more than two. If in a culture, the surname is presented before the given name, then the column names causes confusion.
Query type: Problem detection (Each row in the result could represent a flaw in the design)
Query reliability: High (Few or no false-positive results)
Query license: MIT License
Fixing suggestion: Instead of column name first_name use given_name. Instead of column name last_name use surname. Consider replacing the two columns with the single column person_name. In case of base tables and foreign tables it is possible to rename the columns. In case of derived tables one has to drop and recreate the tables.
Data source: system catalog only
SQL query: Click on query to copy it

SELECT n.nspname AS table_schema, c.relname AS table_name, 
CASE WHEN c.relkind='r' THEN 'BASE TABLE'
WHEN c.relkind='v' THEN 'VIEW'
WHEN c.relkind='m' THEN 'MATERIALIZED VIEW'
WHEN c.relkind='f' THEN 'FOREIGN TABLE' END AS table_type,
a.attname AS column_name 
FROM pg_class AS c INNER JOIN pg_namespace AS n ON c.relnamespace=n.oid
INNER JOIN pg_attribute AS a ON c.oid=a.attrelid
WHERE c.relkind  IN ('r','v', 'm','f') AND a.attnum>=1 AND a.attisdropped='f'
AND (a.attname~*'^first[_]*name$' OR a.attname~*'^last[_]*name$')
ORDER BY n.nspname, c.relname, a.attname;

SQL statements for generating SQL statements that help us to fix the problem

SQL queryDescription
SELECT format('ALTER TABLE %1$I.%2$I RENAME COLUMN %3$I TO given_name;', n.nspname, c.relname, a.attname) AS statements
FROM pg_class AS c INNER JOIN pg_namespace AS n ON c.relnamespace=n.oid
INNER JOIN pg_attribute AS a ON c.oid=a.attrelid
WHERE c.relkind  IN ('r','f') AND a.attnum>=1 AND a.attisdropped='f'
AND a.attname~*'^first[_]*name$'
ORDER BY n.nspname, c.relname, a.attname;
Rename the base table or foreign table column first_name.
SELECT format('ALTER TABLE %1$I.%2$I RENAME COLUMN %3$I TO surname;', n.nspname, c.relname, a.attname) AS statements
FROM pg_class AS c INNER JOIN pg_namespace AS n ON c.relnamespace=n.oid
INNER JOIN pg_attribute AS a ON c.oid=a.attrelid
WHERE c.relkind  IN ('r','f') AND a.attnum>=1 AND a.attisdropped='f'
AND a.attname~*'^last[_]*name$'
ORDER BY n.nspname, c.relname, a.attname;
Rename the base table or foreign table column last_name.

Collections where the query belongs to

Collection nameCollection description
Find problems about namesA selection of queries that return information about the names of database objects. Contains all the types of queries - problem detection, software measure, and general overview.
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 where the query belongs to

Category nameCategory description
NamingQueries of this category provide information about the style of naming.

Reference materials for further reading

Reference
https://digi.lib.ttu.ee/i/?7072
https://www.fbiic.gov/public/2008/nov/Naming_practice_guide_UK_2006.pdf
https://www.w3.org/International/questions/qa-personal-names
https://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/

The list of all the queries