Goal This query checks whether columns used to record registration and last update times have inconsistent data types across the database. Using varying data types for these temporal columns (for example, mixing timestamp and timestamptz) can lead to unexpected timezone behaviors and complicate time-based calculations.
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, data_type
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~*'(reg|lisa|create|add|muut|update)'
AND column_name!~*'(tahtaeg)'
AND data_type~*'(timestamp|date)'),
columns_agg AS (SELECT data_type, 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 data_type)
SELECT data_type, table_columns, nr_of_columns
FROM columns_agg
GROUP BY data_type, table_columns, nr_of_columns
HAVING (SELECT Count(*) AS cnt FROM columns_agg)>1
ORDER BY nr_of_columns DESC, data_type;

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:

Data types

Queries of this category provide information about the data types and their usage.

Inconsistencies

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

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
Data typesQueries of this category provide information about the data types and their usage.
InconsistenciesQueries of this catergory provide information about inconsistencies of solving the same problem in different places.
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.