Goal This query identifies table and domain CHECK constraints that use a positive regular expression operator (~ or ~*) whose pattern unintentionally permits strings consisting solely of whitespace characters (or an optional prefix symbol such as + followed only by spaces).

A common regular expression pitfall occurs when a developer includes a space character inside a repeated character class spanning the entire pattern—for example, validating a phone number with CHECK (phone ~ '^\+?[0-9 ]{7,}$') or a name with CHECK (name ~ '^[a-zA-Z ]+$'). Because the character class matches spaces on equal footing with digits or letters and no mandatory digit or letter is required outside the class, strings such as seven spaces (' ') or a plus sign followed by six spaces ('+ ') satisfy the constraint without containing a single digit or letter.
Notes Queries information_schema views (constraint_column_usage, column_domain_usage, domain_constraints, check_constraints, tables, schemata) to inspect both table-level and domain-level CHECK constraints on base and foreign tables.

The WHERE clause flags a constraint if its regular expression matches either:

  • A pattern explicitly matching only whitespace characters ('^[[:space:]]*$', '^\s*$', or ' ').

  • An anchored pattern (^...$) consisting solely of optional non-alphanumeric prefixes (such as \+?) and one or more repeated character classes [...] that include whitespace (' ', \s, or [:space:]) without requiring any mandatory non-whitespace character class (such as [0-9] or [[:alnum:]]).



A NOT EXISTS subquery excludes columns that have a complementary condition (in the same or another CHECK constraint on the column) enforcing non-whitespace or digit presence, such as length(regexp_replace(col, '[^0-9]', '', 'g')) >= 7, col !~ '^[[:space:]]*$', or col ~ '[^[:space:]]'.
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 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, 
        NULL AS domain_schema, 
        NULL AS domain_name
    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, 
        cdu.domain_schema, 
        cdu.domain_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 
    table_schema, 
    table_name, 
    table_type, 
    column_name, 
    check_clause, 
    constraint_name, 
    check_type, 
    domain_schema, 
    domain_name
FROM checks
WHERE (

    check_clause ~ '[[:space:]]~[*]{0,1}[[:space:]]+''[\^][[]{2}[:]space[:][]]{2}[*+]{0,1}[$]'''
    OR check_clause ~ '[[:space:]]~[*]{0,1}[[:space:]]+''[\^]\\s[*+]{0,1}[$]'''
    OR check_clause ~ '[[:space:]]~[*]{0,1}[[:space:]]+''[ ]*'''
    

    OR check_clause ~* '[[:space:]]~[*]{0,1}[[:space:]]+''[\^](?:\\?[^[:alnum:]\\]\?|[()])*(\[[^\]]*(?: |\[:space:\]|\\s)[^\]]*\](?:[*+?]|[{][0-9,]+[}])?[()]*)+[$]'''
)

AND NOT EXISTS (
    SELECT 1
    FROM checks c2
    WHERE c2.table_schema = checks.table_schema
      AND c2.table_name = checks.table_name
      AND c2.column_name = checks.column_name
      AND (
          c2.check_clause ~* 'regexp_replace[[:space:]]*\([^)]*\[\^0-9\]'
          OR c2.check_clause ~* '[[:space:]]!~[*]{0,1}[[:space:]]+''[\^]?([[]{2}[:]space[:][]]{2}|\\s|[ ])'
          OR c2.check_clause ~* '[[:space:]]~[*]{0,1}[[:space:]]+''[^'']*(\[\^\[:space:\]\]|\\S)'
      )
)
ORDER BY table_schema, table_name, column_name;

Fixing Suggestion

Rewrite the regular expression so that it requires mandatory digits or letters and prohibits leading and trailing spaces (for example, replace '^\+?[0-9 ]{7,}$' with '^\+?[0-9]([0-9 ]{5,}[0-9])$'), or add an explicit check verifying the minimum number of digits (such as AND length(regexp_replace(phone, '[^0-9]', '', 'g')) >= 7).

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.

Validity and completeness

Queries 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).

NameDescription
CHECK constraintsQueries of this category provide information about CHECK constraints.
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).