Goal This query checks whether timestamp columns used to record last update times are named consistently across different tables. Using a uniform naming convention (such as consistently using updated_at or last_modified) significantly improves database schema readability and maintainability.
Notes 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 High Few or no false-positive results
License MIT (opens in new tab)
Data Source INFORMATION_SCHEMA only
SQL Query
WITH columns AS (SELECT table_schema, table_name, column_name, replace(lower(column_name), lower(table_name),'<b>table</b>') AS column_pattern 
FROM INFORMATION_SCHEMA.columns AS c
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 column_name~*'(muut|update)'
AND column_name!~*'(tahtaeg)'
AND data_type~*'(timestamp|date)'),
columns_agg AS (SELECT column_pattern, string_agg(table_schema || '.' || table_name || '.' || column_name,'<br>' ORDER BY table_schema, table_name, column_name) AS table_columns, Count(column_name) AS nr_of_columns
FROM columns
GROUP BY column_pattern)
SELECT column_pattern, table_columns, nr_of_columns
FROM columns_agg
GROUP BY column_pattern, table_columns, nr_of_columns
HAVING (SELECT Count(*) AS cnt FROM columns_agg)>1
ORDER BY nr_of_columns DESC, column_pattern;

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:

Inconsistencies

Queries of this catergory provide information about inconsistencies of solving the same problem in different places.

Naming

Queries of this category provide information about the style of naming.

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.

Temporal data

Queries of this category provide information about temporal (time-related) data that is kept in the database.

NameDescription
InconsistenciesQueries of this catergory provide information about inconsistencies of solving the same problem in different places.
NamingQueries of this category provide information about the style of naming.
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.
Temporal dataQueries of this category provide information about temporal (time-related) data that is kept in the database.