datetime field overflow
SQLSTATE 22008 is raised when a datetime or interval value computation produces a result that overflows the valid range of the timestamp or interval type, or when a datetime field (month, day, hour, etc.) contains an out-of-range value.
- 1Computing a timestamp that falls outside the Postgres timestamp range (4713 BC to 294276 AD)
- 2Adding an interval that pushes a timestamp past the maximum value
- 3Datetime field value out of range (e.g., month 13, day 32)
Interval arithmetic pushing a timestamp out of range.
SELECT '294276-01-01'::timestamp + interval '1 year';
expected output
ERROR: timestamp out of range
Fix 1
Clamp the timestamp to the valid range before arithmetic
WHEN When computing future or past timestamps that may overflow.
SELECT LEAST(my_date + interval '1 year', '9999-12-31'::timestamp);
Why this works
LEAST clamps the result to a maximum safe value, preventing overflow.
Fix 2
Validate input datetime fields before storing
WHEN When accepting datetime input from external sources.
Why this works
Check month (1-12), day (1-31 depending on month), hour (0-23), minute/second (0-59) before constructing timestamps.
Class 22 — Data Exception
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev