Goal This query audits the database schema for syntactical inconsistency in CHECK constraints designed to prohibit empty or whitespace-only strings. It identifies the concurrent use of disparate expression patterns—such as column ~ '\S', column !~ '^[[:space:]]*$', or trim(column) <> ''—to achieve the same functional validation. While these patterns may enforce identical logical rules, the lack of a single, standardized idiom increases cognitive load and maintenance complexity. The query facilitates a review to select one preferred syntax and enforce its uniform application across all relevant columns.
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 (opens in new tab)
Data Source INFORMATION_SCHEMA+system catalog
SQL Query
WITH checks AS (
select 
n.nspname as table_schema,
c.relname as table_name,
CASE WHEN c.relkind='r' THEN 'BASE TABLE' ELSE 'FOREIGN' END AS table_type,
a.attname as column_name,
regexp_replace(pg_get_constraintdef(o.oid),'CHECK ', '','g') AS check_clause,
'TABLE CHECK' AS check_type, 
o.conname AS constraint_name
from pg_constraint o inner join pg_class c on c.oid = o.conrelid
inner join pg_namespace n on o.connamespace=n.oid
INNER JOIN pg_attribute a ON a.attrelid = c.oid AND a.attnum = o.conkey[1] AND a.attisdropped = FALSE
where o.contype = 'c' 
AND cardinality(o.conkey)=1
AND c.relkind IN ('r', 'f')
UNION ALL SELECT cdu.table_schema, cdu.table_name, t.table_type, cdu.column_name, regexp_replace(cc.check_clause, 'VALUE', '(' || cdu.column_name ||')::' || lower(d.data_type)) AS check_clause, 'DOMAIN CHECK' AS check_type, 
d.domain_schema||'.'||d.domain_name || '.' || cc.constraint_name AS 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.domains AS d 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_cleaned AS (SELECT 
CASE WHEN check_clause~'[[:space:]]<>[[:space:]]+''[ ]*''' THEN '<>'''''
WHEN check_clause~'[[:space:]]~[*]{0,1}[[:space:]]+''\[\^\\s\]''' THEN '~''[^\s]'''
WHEN check_clause~'[[:space:]]~[*]{0,1}[[:space:]]+''\[\^\[\:space\:\]\]''' THEN '~''[^[:space:]]'''
WHEN check_clause~'[[:space:]]~[*]{0,1}[[:space:]]+''\\S''' THEN '~''\S'''
WHEN check_clause~'[[:space:]]!~[*]{0,1}[[:space:]]+''[\^][[]{2}[:]space[:][]]{2}[*+]{0,1}[$]''' THEN '~''^[[:space:]]*$'''
WHEN check_clause~'[[:space:]]!~[*]{0,1}[[:space:]]+''[\^]\\s[*+]{0,1}[$]' THEN '~''^\s*$'''
WHEN check_clause~*'(length[[:space:]]*\(.+>[[:space:]]*0|0[[:space:]]*<.+length[[:space:]]*\()' THEN 'length(..)>0' END AS check_clause_cleaned, 
check_clause, table_schema, table_name, column_name
FROM checks
WHERE check_clause~'[[:space:]]<>[[:space:]]+''[ ]*'''
OR check_clause~'[[:space:]]~[*]{0,1}[[:space:]]+''\[\^\\s\]'''
OR check_clause~'[[:space:]]~[*]{0,1}[[:space:]]+''\[\^\[\:space\:\]\]'''
OR check_clause~'[[:space:]]~[*]{0,1}[[:space:]]+''\\S'''
OR check_clause~'[[:space:]]!~[*]{0,1}[[:space:]]+''[\^][[]{2}[:]space[:][]]{2}[*+]{0,1}[$]'''
OR check_clause~'[[:space:]]!~[*]{0,1}[[:space:]]+''[\^]\\s[*+]{0,1}[$]'
OR check_clause~*'(length[[:space:]]*\(.+>[[:space:]]*0|0[[:space:]]*<.+length[[:space:]]*\()'),
checks_agg AS (SELECT check_clause_cleaned, string_agg(table_schema || '.' || table_name || '.' || column_name, ';<br>' ORDER BY table_schema, table_name, column_name) AS columns, Count(*) AS nr_of_columns
FROM checks_cleaned
GROUP BY check_clause_cleaned
HAVING Count(*)>1)
SELECT check_clause_cleaned, columns, nr_of_columns
FROM checks_agg
WHERE (SELECT Count(*) AS cnt FROM checks_agg)>1 
ORDER BY check_clause_cleaned;

Collections

This query belongs to the following collections:

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 .

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:

CHECK constraints

Queries of this category provide information about CHECK constraints.

Comfortability of database evolution

Queries of this category provide information about the means that influence database evolution.

NameDescription
CHECK constraintsQueries of this category provide information about CHECK constraints.
Comfortability of database evolutionQueries of this category provide information about the means that influence database evolution.