Goal This query identifies potentially overly strict CHECK constraints applied to base or foreign table columns that store personal names. It highlights validation rules that might inadvertently block valid legal names.
Notes The query takes into account only constraints that are associated with exactly one column. Thus, there could be constraints that apply restrictions to multiple columns that still reject legal values. The query takes into account constraints that are associated directly with the column as well as constraints that are associated with the column through a domain. The query does not find CHECK constraints of domains that are not associated with any table. The query considers both column 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
Fixing Suggestion Drop the constraints.
Data Source INFORMATION_SCHEMA only
SQL Query
WITH checks AS (SELECT ccu.table_schema, ccu.table_name, t.table_type, ccu.column_name, cc.check_clause, cc.constraint_name, 'TABLE CHECK' AS check_type
FROM INFORMATION_SCHEMA.constraint_column_usage AS ccu INNER JOIN INFORMATION_SCHEMA.check_constraints AS cc USING (constraint_schema, constraint_name)
INNER JOIN INFORMATION_SCHEMA.tables AS t USING (table_schema, table_name)
WHERE cc.check_clause NOT LIKE '%IS NOT NULL' AND 
ccu.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)
UNION SELECT cdu.table_schema, cdu.table_name, t.table_type, cdu.column_name, cc.check_clause, cc.constraint_name, 'DOMAIN CHECK' AS check_type
FROM INFORMATION_SCHEMA.column_domain_usage AS cdu INNER JOIN INFORMATION_SCHEMA.tables AS t USING (table_schema, table_name)
INNER JOIN INFORMATION_SCHEMA.domain_constraints AS dc USING (domain_schema, domain_name)
INNER JOIN INFORMATION_SCHEMA.check_constraints AS cc USING (constraint_schema, constraint_name)
WHERE t.table_type IN ('BASE TABLE','FOREIGN') AND cc.check_clause NOT LIKE '%IS NOT NULL'  AND 
cdu.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)),
checks_agg AS (SELECT table_schema, table_name, table_type, string_agg(column_name, ',
' ORDER BY column_name) AS columns, check_clause, constraint_name, check_type FROM checks WHERE column_name~*'(first[_]*name|last[_]*name|given[_]*name|surname|person[_]*name|eesnimi|perenimi|perekonnanimi|isik.*nimi)' GROUP BY table_schema, table_name, table_type, check_clause, constraint_name, check_type) SELECT table_schema, table_name, table_type, columns, check_clause, constraint_name, check_type FROM checks_agg WHERE check_clause ~* '((char_length|length)\s*\([^<>=]+\)\s*(>\s*[1-9]\d*|>=\s*[2-9]\d*)|!?~[*]?\s*(E?'')?(?!(\^\$|\^\[\[:space:\]\][\*\+]\$|\^ [\*\+]\$|\^\\s[\*\+]\$|\[\^\[:space:\]\]|\[\^ \]|\\S)'')[^'']*(\^|\$|\[|\\[dDwWsS])|SIMILAR TO\s*(E?'')?[^'']*\[|NOT\s+(LIKE|ILIKE)\s*(E?'')?[^'']*[- _0-9'']+|(position|strpos)\s*\([^)]+''[- _0-9'']+''|(initcap|upper|lower)\s*\()' ORDER BY table_schema, table_name;

SQL statements that help generate fixes for the identified problem.

SQL Query to Generate FixDescription
WITH ck AS (SELECT 
o.conname,
(SELECT nspname FROM pg_namespace WHERE oid=c.relnamespace) AS target_schema,
c.relname AS target_table, 
c.oid As target_table_oid,
unnest(o.conkey) AS target_col,
substring(pg_get_constraintdef(o.oid),7) AS consrc,
CASE WHEN c.relkind='r' THEN 'BASE TABLE'
WHEN c.relkind='f' THEN 'FOREIGN'
ELSE 'TABLE' END AS table_type
FROM pg_constraint o INNER JOIN pg_class c ON c.oid = o.conrelid
WHERE o.contype = 'c' AND cardinality(o.conkey)=1),
checks AS (
SELECT ck.target_schema AS table_schema, ck.target_table AS table_name,  table_type, a_target.attname AS column_name, ck.consrc AS check_clause, ck.conname AS constraint_name
FROM ck INNER JOIN pg_attribute a_target ON ck.target_col = a_target.attnum AND ck.target_table_oid = a_target.attrelid AND a_target.attisdropped = FALSE
WHERE ck.target_schema NOT IN (SELECT schema_name
FROM INFORMATION_SCHEMA.schemata
WHERE schema_name<>'public' AND
schema_owner='postgres' AND schema_name IS NOT NULL))
SELECT format('ALTER %4$s TABLE %1$I.%2$I DROP CONSTRAINT %3$I;', table_schema, table_name, constraint_name, CASE WHEN table_type='FOREIGN' THEN table_type END) AS statements
FROM checks
WHERE column_name~*'(first[_]*name|last[_]*name|given[_]*name|surname|person[_]*name|eesnimi|perenimi|perekonnanimi|isik.*nimi)'
AND (check_clause~*'!~.*''.*(0-9|digit)'
OR check_clause~*'[^!](~|~*)[[:space:]]''.*(a-z).*''')
ORDER BY table_schema, table_name, column_name;
Drop the constraint that is directly associated with the table.
WITH ck AS (SELECT 
o.conname,
(SELECT nspname FROM pg_namespace WHERE oid=c.relnamespace) AS target_schema,
c.relname AS target_table, 
c.oid As target_table_oid,
unnest(o.conkey) AS target_col,
substring(pg_get_constraintdef(o.oid),7) AS consrc
FROM pg_constraint o INNER JOIN pg_class c ON c.oid = o.conrelid
WHERE o.contype = 'c' AND cardinality(o.conkey)=1),
checks AS (
SELECT cdu.domain_schema, cdu.domain_name, cdu.table_name, cdu.column_name, cc.check_clause, cc.constraint_name
FROM INFORMATION_SCHEMA.column_domain_usage AS cdu INNER JOIN INFORMATION_SCHEMA.tables AS t USING (table_schema, table_name)
INNER JOIN INFORMATION_SCHEMA.domain_constraints AS dc USING (domain_schema, domain_name)
INNER JOIN INFORMATION_SCHEMA.check_constraints AS cc USING (constraint_schema, constraint_name)
WHERE t.table_type IN ('BASE TABLE','FOREIGN') AND cc.check_clause NOT LIKE '%IS NOT NULL'  AND 
cdu.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))
SELECT DISTINCT format('ALTER DOMAIN %1$I.%2$I DROP CONSTRAINT %3$I;', domain_schema, domain_name, constraint_name) AS statements
FROM checks
WHERE column_name~*'(first[_]*name|last[_]*name|given[_]*name|surname|person[_]*name|eesnimi|perenimi|perekonnanimi|isik.*nimi)'
AND (check_clause~*'!~.*''.*(0-9|digit)'
OR check_clause~*'[^!](~|~*)[[:space:]]''.*(a-z).*''')
ORDER BY statements;
Drop the domain constraint.
Collections

This query belongs to the following collections:

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:

NameDescription
CHECK constraintsQueries of this category provide information about CHECK constraints.
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.
Validity and completenessQueries of this category provide information about whether database design represents the world (domain) correctly (validity) and whether database design captures all the information about the world (domain) that is correct and relevant (completeness).