PG
PRO
22008ERRORTier 2 — Caution✅ HIGH confidence

datetime field overflow

Category: Data ExceptionVersions: All Postgres versions

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

  1. 1Computing a timestamp that falls outside the Postgres timestamp range (4713 BC to 294276 AD)
  2. 2Adding an interval that pushes a timestamp past the maximum value
  3. 3Datetime field value out of range (e.g., month 13, day 32)

How to reproduce

Interval arithmetic pushing a timestamp out of range.

trigger — this will ERROR
SELECT '294276-01-01'::timestamp + interval '1 year';
ERROR: timestamp out of range

Fix 1: Clamp the timestamp to the valid range before arithmetic

When computing future or past timestamps that may overflow.

fix
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.

fix

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

📄 Reference pages

Date/Time Typestimestamp
⚙️ This error reference was generated with AI assistance and reviewed for accuracy. Examples are provided to illustrate common scenarios and may not cover every case. Always test fixes in a development environment before applying to production. Spotted an error? Suggest a correction →