Query goal: | Find columns of tables that start with the word comment or komment but end differently (excluding numbers). Return result only if there is more than one naming variant of such columns in the database. For instance, a column has the name "comment" but another "comments". |
Notes about the query: | The query considers names like comment1, comment2, comment3 as the same naming variant. The query considers only columns with a textual type. The query takes into account that some columns can be defined based on a domain. |
Query type: | Problem detection (Each row in the result could represent a flaw in the design) |
Query reliability: | Medium (Medium number of false-positive results) |
Query license: | MIT License |
Data source: | INFORMATION_SCHEMA+system catalog |
SQL query: | Click on query to copy it
WITH comment_cols AS (SELECT nspname AS table_schema, relname AS table_name, CASE WHEN relkind='r' THEN 'BASE TABLE' WHEN relkind='v' THEN 'VIEW' WHEN relkind='m' THEN 'MATERIALIZED VIEW' WHEN relkind='f' THEN 'FOREIGN TABLE' WHEN relkind='p' THEN 'PARTITIONED TABLE' END AS table_type, translate(attname,'1234567890','') AS column_name FROM pg_class INNER JOIN pg_namespace ON pg_class.relnamespace=pg_namespace.oid INNER JOIN pg_attribute ON pg_class.oid=pg_attribute.attrelid INNER JOIN pg_type ON pg_attribute.atttypid =pg_type.oid WHERE attnum>=1 AND relkind IN ('r','v','m','f','p') AND 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 attname~*'^(kommentaar|comment)' AND CASE WHEN typbasetype=0 THEN pg_type.typname ELSE (SELECT typname FROM pg_type AS domain_type WHERE domain_type.oid=pg_type.typbasetype) END~*'(text|char)') SELECT column_name, Count(*) AS nr_of_columns, string_agg(DISTINCT table_schema || '.' || table_name || ' (' || table_type || ')', ';<br>' ORDER BY table_schema || '.' || table_name || ' (' || table_type || ')') AS columns FROM comment_cols WHERE (SELECT Count(DISTINCT column_name) AS dist_names FROM comment_cols)>1 GROUP BY column_name; |
Collection name | Collection description |
---|---|
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 . |
Category name | Category description |
---|---|
Derived tables | Queries of this category provide information about the derived tables (views, materialized views), which are used to implement virtual data layer. |
Naming | Queries of this category provide information about the style of naming. |