We Widened the Sequence to BIGINT. Inserts Still Failed.
Kevin Tung
0:00 / 0:00
We Widened the Sequence to BIGINT. Inserts Still Failed.
1 просмотр · 4 дн. назад
Kevin Tung
2 подписчика
1 просмотр · 4 дн. назад
When a serial primary key hits 2,147,483,647, the sequence, the column and every foreign key all need widening. What each fix misses on PostgreSQL 17, and a path that avoids a full-table rewrite.
Alex and Maya dig into an illustrative composite: every insert into the busiest table fails at once because its serial key reached 2,147,483,647. Using real output from Kevin's runs on a throwaway PostgreSQL 17 instance, they cover why IDs aren't rows (rollbacks and conflicting upserts burn values), why widening only the sequence moves the error to the column (integer out of range), why widening a serial column leaves its sequence at integer while identity columns widen both, why CYCLE trades a hard stop for duplicate keys, and why every foreign key column still needs widening. Then why ALTER COLUMN TYPE takes an AccessExclusiveLock and rewrites the table (204 MB to 221 MB, new filenode, 1.45 to 2.30 s on a small cached table, a floor not an estimate), the expand-and-contract path (bigint columns, sync triggers, batched backfill, a concurrent unique index, a NOT VALID check, and one short swap with a lock timeout, no rewrite), headroom checks against the column type instead of the sequence, the negative-range last resort, Staff+ rules of thumb, and pushback.
Chapters:
0:00 Cold open — out of IDs at 2,147,483,647
1:52 The naive pattern: IDs aren't rows
3:45 Failure timeline — sequence, column, CYCLE, foreign keys
8:23 Why ALTER COLUMN TYPE hurts
10:36 Implementation: expand, prepare, one short swap
15:37 Headroom monitoring, last resorts, FinOps
18:24 Staff+ rules of thumb and pushback
19:51 Recap and reflective question
Question to sit with: which of your largest tables still use integer keys, and how much of the range have they used? If you had to widen one next quarter, would you know every column, service and schema that stores a copy of that ID?
This discussion is based on a post by Kevin Tung. Read the written version on Medium: https://chen0625tung.medium.com/we-widened...
Hosts Alex and Maya are synthesized voices for this discussion (Microsoft Edge neural text-to-speech) and are not the author.
#PostgreSQL #Database #SQL #DatabaseMigration #SRE #SoftwareEngineering #BackendDevelopment #StaffPlus