| 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 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 |
|
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 .
| Name | 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 . |
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).
| Name | Description |
|---|---|
| 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). |