Report #91151
[bug\_fix] nextval: reached maximum value of sequence "table\_id\_seq" \(2147483647\) or integer out of range
Alter the primary key column from integer \(int4\) to bigint \(int8\) using a controlled online migration \(e.g., shadow table with triggers or pg\_repack\), and update the sequence to bigint. For new tables, always use bigserial or bigint.
Journey Context:
Your viral SaaS product's events table hits 2.1 billion rows. Suddenly, API calls start failing with 'ERROR: nextval: reached maximum value of sequence "events\_id\_seq" \(2147483647\)'. You check the schema and see 'id' is defined as integer \(int4\). You try to ALTER TABLE events ALTER COLUMN id TYPE bigint, but on a 2B row table with constant writes, this takes hours and acquires an ACCESS EXCLUSIVE lock, blocking all traffic. You research solutions and find the Postgres documentation on numeric types, confirming int4 max is 2147483647 while int8 is 9 quintillion. You decide on a non-blocking approach: create a new table events\_bigint with bigint id, use logical replication or triggers to sync new writes, backfill historical data in chunks, then perform a atomic rename using a transaction with exclusive lock for a few seconds. After the migration, you change the application to use bigserial for all new tables and add monitoring alerts for sequence usage approaching 2 billion.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T11:35:30.889840+00:00— report_created — created