INT to BIGINT: Migrating a Primary Key Without One Massive ALTER
INT to BIGINT: Migrating a Primary Key Without One Massive ALTER
Changing a primary key from int to bigint sounds like a data type change.
In practice, it changes an entire graph of dependencies.
First, Measure How Much Headroom You Have
| |
Do not wait until you run out of values.
Why Is a Simple ALTER Risky?
A primary key may be:
- referenced by many foreign keys,
- part of indexes,
- used in history tables,
- passed through APIs,
- mapped as
intin an ORM.
Add the New Key Alongside the Old One
| |
Next, backfill the data and build the new index.
Foreign Keys
Every dependent table also needs a new column:
| |
An unambiguous OldId -> NewId mapping is useful during migration, especially when the new identifier is not a straightforward type conversion.
The Application
During the transition, the application model must understand both keys.
This is a good opportunity to introduce:
- a new API contract,
- shadow reads,
- comparative telemetry,
- gradual cutover of individual modules.
Contract Only at the End
The old int key can be removed only after all references have been migrated.
Summary
A primary-key migration is not just an ALTER COLUMN. It is a project to change the contract between tables and applications.