Diagnostic Queries
Symptoms
An arithmetic result or input value exceeds the range of a 32-bit integer (±2,147,483,647). PostgreSQL raises SQLSTATE 22003 (numeric_value_out_of_range).
- Classic cause: a serial/integer surrogate key approaching 2.1 billion.
- Also from multiplications/sums that overflow int.
- The fix is usually
bigint.
What the server log shows
ERROR: integer out of range
Why PostgreSQL raises this — what the manual says
Section 8.1.1 Integer Types:
“Attempts to store values outside of the allowed range will result in an error.”
integer is a signed 4-byte type. Any input or computed result outside its range cannot be represented, and PostgreSQL raises 22003 instead of wrapping around.
Common causes
- A monotonically increasing
serialkey reaching the int limit. - Sums/products overflowing 32 bits.
- Importing values larger than int into an int column.
How to fix it
- Migrate the column to
bigint(and the sequence tobigint):ALTER TABLE t ALTER COLUMN id TYPE bigint;. - Cast intermediate arithmetic to
bigintto avoid overflow:sum(x::bigint). - Use
bigserial/bigintfor surrogate keys from the start.
Related & next steps
Reference: PostgreSQL 18 Section 8.1 “Numeric Types”.
Thanks — noted. This helps keep the database accurate.