Goal Find implementations of state machines that uses an enumeration type.
Notes The query considers both column/type 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
Data Source INFORMATION_SCHEMA+system catalog
SQL Query
WITH state_enum AS (SELECT t.oid, nspname AS type_schema, typname, array_agg(enumlabel ORDER BY enumsortorder) AS values
FROM pg_catalog.pg_type t LEFT JOIN pg_catalog.pg_enum e ON t.oid=e.enumtypid
INNER JOIN pg_catalog.pg_namespace n ON n.oid=t.typnamespace
WHERE nspname NOT IN (SELECT schema_name
FROM INFORMATION_SCHEMA.schemata
WHERE schema_name<>'public' AND
schema_owner='postgres' AND schema_name IS NOT NULL) 
AND typtype='e'
GROUP BY t.oid, nspname, typname),
cols AS (SELECT n.nspname AS table_schema,
c.relname AS table_name,
a.attname AS column_name,
CASE WHEN t.typbasetype<>0 THEN t.typbasetype ELSE t.oid END AS type_oid
FROM pg_class AS c INNER JOIN pg_namespace AS n ON n.oid=c.relnamespace
LEFT JOIN pg_attribute AS a ON a.attrelid = c.oid
LEFT JOIN pg_type AS t ON a.atttypid=t.oid
WHERE c.relkind='r')
SELECT cols.table_schema, cols.table_name, cols.column_name,
e.type_schema AS enum_type_schema,
e.typname AS enum_type,
e.values AS type_values
FROM cols INNER JOIN state_enum AS e ON e.oid=cols.type_oid
WHERE (e.typname~*'(olek|staatus|seisund|state(?!ment)|status)'
OR cols.column_name~*'(olek|staatus|seisund|state(?!ment)|status)')
ORDER BY table_schema, table_name;

Collections

This query belongs to the following collections:

NameDescription
Find problems about base tablesA 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
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
Data typesQueries of this category provide information about the data types and their usage.
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.
State machineQueries of this category provide information about registration of states of entities in a database.