bigint Out Of Range Past Its Maximum

bigint stops at 9223372036854775807: one more overflows with an explicit out-of-range error (SQLSTATE 22003) instead of silently wrapping.

SqlData Integrity

Code

snippet
-- bigint 上限 9223372036854775807:再 +1 直接报错,不会静默回绕
SELECT 9223372036854775807::bigint + 1;
-- ERROR:  bigint out of range

-- 用 DO 块捕获验证(SQLSTATE 22003)
DO $$
BEGIN
    PERFORM 9223372036854775807::bigint + 1;
EXCEPTION
    WHEN numeric_value_out_of_range THEN
        RAISE NOTICE 'rejected: bigint out of range';
END $$;

-- 对比:numeric 没有上限(精度自选),bigint 是固定 64 位宽度
SELECT 9223372036854775807::numeric + 1 AS numeric_ok;

How the snippet works

SELECT statements share the script.

The first statement adds 1 to 9223372036854775807::bigint — the largest value the fixed 64-bit width of bigint can hold — and the engine replies with ERROR: bigint out of range instead of wrapping around to a negative number. The DO $$ block turns that behavior into a check: PERFORM triggers the same overflow inside BEGIN/EXCEPTION, the WHEN numeric_value_out_of_range arm (SQLSTATE 22003) catches it, and RAISE NOTICE 'rejected: bigint out of range' records the rejection without letting the error abort the session. The final line casts the same literal to numeric, whose precision is chosen rather than fixed, so 9223372036854775807::numeric + 1 AS numeric_ok succeeds — the ceiling is a property of bigint’s fixed width, not of arithmetic in general.

Statements that hold the behavior

SELECT 9223372036854775807::numeric + 1 AS numeric_ok;
SELECT 9223372036854775807::bigint + 1;
DO $$
BEGIN
    PERFORM 9223372036854775807::bigint + 1;
EXCEPTION
    WHEN numeric_value_out_of_range THEN
        RAISE NOTICE 'rejected: bigint out of range';
END $$;

Interactive Tools for This Topic