Find implementations of state machines that uses a set of columns with a timestamp type.
Notes
The query finds all the base tables that have at least two temporal columns where the name of the column refers to an event that may be caused by a state transition. The query shows all the temporal columns of the tables. It returns the name of table and its schema as well as names of Boolean columns. In case of the string_agg function, the line break (br) tag is used as a part of the separator for the better readability in case the query result is displayed in a web browser. The query may return false positive results if the columns are not used for the registration of data about the state.
Type
Problem detection (Each row in the result could represent a flaw in the design)
Use instread state classifier. See Level 2 Status Pattern, Current Status from L. Silverston ja P. Agnew, „The Data Model Resource Book, Volume 3: Universal Patterns for Data Modeling,“ Chichester, John Wiley & Sons Ltd, 2009.
Data Source
INFORMATION_SCHEMA only
SQL Query
WITH timestamp_columns AS (SELECT table_schema, table_name, column_name, data_type, ordinal_position
FROM INFORMATION_SCHEMA.columns
WHERE data_type IN ('timestamp without time zone', 'timestamp with time zone') AND
(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)),
has_at_least_two_event_cols AS (SELECT table_schema, table_name
FROM timestamp_columns
WHERE column_name~*'(accept|active|approve|change|clean|clear|close|create|delete|dirty|disable|enable|execute|hidden|inactive|lock|maile|modif|obsolete|on_hold|overlook|print|public|register|remove|restore|request|sent|submit|reject|update|use|valid|visible|withdraw)'
OR column_name~*'(aktiiv|eemalda|esita|kaivita|kinnita|kustuta|loo|lopeta|lukusta|meili|mitteaktiivne|nahtav|peata|peida|peidetud|prindi|puhas|registreeri|saada|saadetud|suletud|sulge|taasta|tyhista)'
GROUP BY table_schema, table_name
HAVING Count(*)>1)
SELECT table_schema, table_name, Count(*) AS nr_of_columns, string_agg(column_name || ' ' || data_type, '; ' ORDER BY ordinal_position) AS timestamp_columns
FROM timestamp_columns
WHERE (table_schema, table_name) IN (SELECT
table_schema, table_name
FROM has_at_least_two_event_cols)
GROUP BY table_schema, table_name
ORDER BY Count(*) DESC, table_schema, table_name;
Collections
This query belongs to the following collections:
Name
Description
Find problems about base tables
A selection of queries that return information about the data types, field sizes, default values as well as general structure of base tables. Contains all the types of queries - problem detection, software measure, and general overview
Categories
This query is classified under the following categories:
Name
Description
Comfortability of database evolution
Queries of this category provide information about the means that influence database evolution.
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.
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.
State machine
Queries of this category provide information about registration of states of entities in a database.
Temporal data
Queries of this category provide information about temporal (time-related) data that is kept in the database.
Further reading and related materials:
Reference
L. Silverston ja P. Agnew, „The Data Model Resource Book, Volume 3: Universal Patterns for Data Modeling,“ Chichester, John Wiley & Sons Ltd, 2009. Level 1 Status Pattern