| Goal | This query identifies CHECK constraints that use non-deterministic time functions (such as now or current_date) in a way that causes initially valid data to become invalid over time. For example, a constraint like localtimestamp(0) > end_date is an anti-pattern because advancing time will eventually violate it. However, safe implementations—such as birth_date < CURRENT_DATE—are permitted, because once the condition is met, it remains true indefinitely as time moves forward. |
|---|---|
| Notes | 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. According to the SQL standard: Certain |
| 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) |
| Fixing Suggestion | Drop the constraints. Implement the checks by using triggers, user-defined routines, or in the application. |
| Data Source | INFORMATION_SCHEMA only |
| SQL Query |
|
SQL statements that help generate fixes for the identified problem.
WITH checks AS (SELECT ccu.table_schema, ccu.table_name, t.table_type, cc.check_clause, cc.constraint_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))
SELECT DISTINCT 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 check_clause~* '[[:space:]]+(>|>=|=)([[:space:]]|[(])+(current_|localtimestamp|now)'
OR check_clause~* '[[:space:]]+(>|>=|=)([[:space:]]|[(])+extract[(].*(current_|localtimestamp|now)'
OR check_clause~* '(current_date|current_timestamp([(][[:digit:]][)]){0,1}|localtimestamp([(][[:digit:]][)]){0,1}|now[(][)])([[:space:]]|[)])+(<|<=|=)[[:space:]]+'
OR check_clause~* 'extract[(].*(current_date|current_timestamp([(][[:digit:]][)]){0,1}|localtimestamp([(][[:digit:]][)]){0,1}|now[(][)])([[:space:]]|[)])+(<|<=|=)[[:space:]]+'
ORDER BY statements;Drop the constraint that is associated directly with the table.
WITH checks AS (SELECT cdu.domain_schema, cdu.domain_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 format('ALTER DOMAIN %1$I.%2$I DROP CONSTRAINT %3$I;', domain_schema, domain_name, constraint_name) AS statements
FROM checks
WHERE check_clause~* '[[:space:]]+(>|>=|=)([[:space:]]|[(])+(current_|localtimestamp|now)'
OR check_clause~* '[[:space:]]+(>|>=|=)([[:space:]]|[(])+extract[(].*(current_|localtimestamp|now)'
OR check_clause~* '(current_date|current_timestamp([(][[:digit:]][)]){0,1}|localtimestamp([(][[:digit:]][)]){0,1}|now[(][)])([[:space:]]|[)])+(<|<=|=)[[:space:]]+'
OR check_clause~* 'extract[(].*(current_date|current_timestamp([(][[:digit:]][)]){0,1}|localtimestamp([(][[:digit:]][)]){0,1}|now[(][)])([[:space:]]|[)])+(<|<=|=)[[:space:]]+'
ORDER BY domain_schema, domain_name, constraint_name;Drop the constraint that is associated with the domain.
| SQL Query to Generate Fix | Description |
|---|---|
| Drop the constraint that is associated directly with the table. |
| Drop the constraint that is associated with the domain. |
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.
System-defined functions
Queries of this category provide information about the use of system-defined functions.
Temporal data
Queries of this category provide information about temporal (time-related) data that is kept in the database.
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. |
| System-defined functions | Queries of this category provide information about the use of system-defined functions. |
| Temporal data | Queries of this category provide information about temporal (time-related) data that is kept in the database. |
| 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). |