Find base tables that implement a highly generic database design (EAV design - Entiry-Attribute-Value design), according to which attribute values are recorded in a generic table that contains attribute-value pairs.
Notes
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. Because the query relies on the names of base tables the query result can have false positive and false negative results. The query considers both table names in English and Estonian.
Type
Problem detection (Each row in the result could represent a flaw in the design)
WITH columns AS (SELECT c.table_schema, c.table_name, regexp_replace(c.column_name, '[[:digit:]]','') AS repeating_column,
c.data_type || CASE WHEN c.character_maximum_length IS NOT NULL THEN '(' || c.character_maximum_length::text || ')'
WHEN c.numeric_precision ||'.'|| c.numeric_scale IS NOT NULL THEN '(' || c.numeric_precision ||'.'|| c.numeric_scale || ')'
ELSE '' END AS type, c.column_name, c.ordinal_position
FROM INFORMATION_SCHEMA.columns AS c INNER JOIN INFORMATION_SCHEMA.tables AS t
USING (table_schema, table_name)
WHERE t.table_type='BASE TABLE' AND
c.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 table_name~* '(attribute|property|properties|atribuu|omadus)' ),
col_values AS (SELECT table_schema, table_name
FROM columns
WHERE column_name~*'(value|vaartus|väärtus)')
SELECT table_schema, table_name, string_agg(column_name || ' ' || type, '; ' ORDER BY ordinal_position) AS columns
FROM columns
WHERE (table_schema, table_name) IN (SELECT table_schema, table_name
FROM col_values)
GROUP BY table_schema, table_name
ORDER BY table_schema, table_name;
Categories
This query is classified under the following categories:
Name
Description
Database design antipatterns
Queries of this category provide information about possible occurrences of SQL database design antipatterns.
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.
Structure of base tables
Queries of this category provide information about the structuring of base tables at the database conceptual level
Further reading and related materials:
Reference
This is one of the antipatterns from the Bill Karwin's book of SQL antipatterns. See Chapter 6: Entity-Attribute-Value.
Smell "Metadata as data": Sharma, T., Fragkoulis, M., Rizou, S., Bruntink, M. and Spinellis, D.: Smelly relations: measuring and understanding database schema quality. In: Proceedings of the 40th International Conference on Software Engineering: Software Engineering in Practice, pp. 55-64. ACM, (2018).
Eessaar, E. and Soobik, M.: On universal database design. Baltic DB & IS, pp. 349-360, (2008).