22008ERRORTier 2 — Caution✅ HIGH confidencedatetime field overflow
What this means
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.
Why it happens
- 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)
How to reproduce
Interval arithmetic pushing a timestamp out of range.
SELECT '294276-01-01'::timestamp + interval '1 year';Fix 1: Clamp the timestamp to the valid range before arithmetic
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 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.
Sources
📚 Official docs: https://www.postgresql.org/docs/current/errcodes-appendix.html
🔧 Source ref: Class 22 — Data Exception
Confidence assessment
✅ HIGH confidence
Standard SQLSTATE. Postgres timestamp range and overflow behaviour is well-documented.
See also
🔗 Related errors
📄 Reference pages