(Note · · 6 min read)
The migration that locked the table in the middle of the day
Migrations are tested against development data, where every statement is instant because there are two hundred rows. The same statement against two hundred million takes a lock and holds it, and everything waiting behind it piles up until the application appears to be down.
By Md. Habibur Rahman Shohel, full-stack & AI product engineer in Dhaka
Know which changes are cheap
Some changes only touch the table's definition and return immediately whatever its size — typically adding a nullable column, or dropping one. Others rewrite every row or scan the whole table to verify a condition, and those are the ones that cause an incident.
The usual culprits are adding a column with a default that must be written to existing rows, changing a column's type, and adding a constraint or index that validates existing data. Check your database's behaviour for the specific version you run, because what is cheap has changed between versions and the folklore has not kept up.
Build indexes without blocking
Creating an index is the most common cause of a locked table in a deploy. Most databases offer a concurrent variant that does not block writes; it is slower and has conditions, such as not running inside a transaction and needing cleanup if it fails. Use it anyway on anything large.
The same applies to constraints: add them as not yet validated, then validate separately, so the expensive scan happens without holding everything else.
Expand, migrate, contract
Any change that would break running code has to be split across deploys, because during a rolling deploy both versions are live at once and the database must satisfy both.
- Expand: add the new column or table, nullable, writing nothing that old code depends on.
- Deploy code that writes both the old and new shape and reads the old one.
- Backfill existing rows in batches, with pauses, not as one statement.
- Deploy code that reads the new shape.
- Contract: stop writing the old shape, then drop it, once nothing refers to it.
It is slower than renaming a column and it is the difference between a deploy and an outage. A rename is two deploys, not one.
Backfill in batches that can be stopped
Updating millions of rows in one statement holds a long transaction, bloats the table and blocks. Work through in batches with a pause between, and make the job resumable so it can be interrupted and continued rather than restarted from the beginning.
Keep backfills out of the migration itself. A migration should be fast and structural; data movement belongs in a job you can watch, throttle and stop.
Set a lock timeout, and rehearse on real data
Set a short lock timeout so a migration that cannot acquire a lock fails quickly instead of queueing every other query behind it. A failed migration you retry at a quieter moment is vastly better than a successful one that stops the application for four minutes.
And rehearse against a restored copy of production, not development. That is the only way to learn what a statement actually costs at real size — and on a system with no migration history at all, it is the only safety net you have before a change meets live data.
Common questions
Why was my migration instant in development and slow in production?
Because statement cost depends on row count. Changes that rewrite every row or scan the table to validate a condition are instant on two hundred rows and take a blocking lock on two hundred million.
How do I add an index without locking the table?
Use your database's concurrent index creation, which does not block writes. It is slower and has conditions — typically it cannot run inside a transaction and needs cleanup if it fails — but on a large table it is the difference between a deploy and an outage.
How do I rename a column safely?
In two deploys, using expand and contract: add the new column, deploy code writing both and reading the old, backfill in batches, deploy code reading the new, then stop writing and drop the old. During a rolling deploy both code versions are live, so the database must satisfy both.
Should data backfills run inside the migration?
No. Migrations should be fast and structural. Move data in a separate batched job that can be watched, throttled, stopped and resumed, rather than as one long statement that holds a transaction and blocks.