The migration was running fine until the schema froze. You had to add a new column, but downtime was not an option. The clock was ticking, and the database held everything.
A new column sounds simple, but the wrong approach can block queries, lock rows, or burn CPU. In relational databases like PostgreSQL and MySQL, the default ALTER TABLE ADD COLUMN can lock the table if not handled carefully. On high-traffic systems, that means stalled writes and lost revenue.
Before adding a column, check if it can be nullable or have a default. For large datasets, adding a default with ALTER TABLE writes to every row, which can be slow and cause locks. One safer pattern is:
- Add the new column as nullable with no default.
- Backfill the column in controlled batches.
- Set the default and constraints after backfill completes.
For Postgres, this avoids a full-table rewrite. MySQL users may need to check the storage engine and apply online schema change tools like gh-ost or pt-online-schema-change. Always test changes in a staging environment with production-like load before touching live data.