Changing a Data Type Without a Major Blocking Operation: Add a Column Instead of ALTER COLUMN
Changing a Data Type Without a Major Blocking Operation: Add a Column Instead of ALTER COLUMN
ALTER COLUMN looks harmless until the table contains hundreds of millions of rows.
Changing a data type may require rebuilding data, indexes, and dependencies.
Example
We want to move from:
| |
to:
| |
Step 1 — Add a New Column
| |
Step 2 — Validate the Conversion
| |
First, identify the rows that cannot be migrated.
Step 3 — Backfill in Batches
| |
Step 4 — Dual Write
The new application version writes both representations or writes one and synchronizes the other.
Step 5 — Switch Reads
After validation, the new column becomes the source for reads.
Indexes
Remember that indexes depending on the old column must be planned separately.
You can prepare the new index before switching:
| |
Contract
Remove the old column only when:
- all reads use the new column,
- all writes use the new column,
- integrations have been switched,
- the data has been validated,
- rollback no longer requires the old representation.
Summary
Changing a data type does not have to be a single DDL operation.
A new column, backfill, validation, and cutover give you much greater control over risk.