| Goal | This query identifies potentially flawed CHECK constraints on columns of type timestamp or a timestamp range (e.g., tstzrange, daterange). It targets range checks where the upper bound of the value or the range is defined using an inclusive operator (<=). This is a common source of bugs, as a condition like column <= '2025-12-31' or UPPER(column) <= '2025-12-31' is interpreted as being up to 00:00:00 on that day, inadvertently excluding the entire last day of the intended period. The more robust pattern is to use an exclusive upper bound, such as column < '2026-01-01'. |
| Type | Problem detection (Each row in the result could represent a flaw in the design) |
| Reliability | Low (Many false-positive results) |
| License | MIT License |
| Fixing Suggestion | For instance, if one has to check that values in the column reg_time are between years 2010 and 2100, then one should have CHECK (reg_time>='2010-01-01' AND reg_time<'2101-01-01') instead of checks like
These checks would omit some moments from the permitted range. |
| Data Source | INFORMATION_SCHEMA+system catalog |
| SQL Query |
|
This query belongs to the following collections:
| 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 . |
This query is classified under the following categories:
| Name | Description |
|---|---|
| CHECK constraints | Queries of this category provide information about CHECK constraints. |
| Temporal data | Queries of this category provide information about temporal (time-related) data that is kept in the database. |
Further reading and related materials:
| Reference |
|---|
| https://wiki.postgresql.org/wiki/Don%27t_Do_This#Don.27t_use_BETWEEN_.28especially_with_timestamps.29 |
| https://stackoverflow.com/questions/8723523/selecting-records-between-two-timestamps |