The database schema was perfect until the product team sent a new spec. Now you need a new column.
Adding a new column is not just a quick ALTER TABLE command. Done wrong, it can lock writes, slow reads, and put your system at risk. Done right, it becomes a safe, zero-downtime migration that keeps your application fast and stable.
Start with the goal. Define the column name, data type, nullability, default value, and index strategy. Be exact. Avoid vague types like TEXT unless you need them. In most relational databases, adding a nullable column without a default is instant. Adding one with a non-null default rewrites the whole table and can block traffic.
Plan your migrations in steps.
- Add the new column as nullable.
- Write a backfill job that gradually populates the column for existing rows.
- Update application code to read and write the new column.
- Enforce constraints only after the data is complete and validated.
For huge datasets, run backfills in small batches. Use indexed filters to avoid sequential scans. Test the migration on a staging environment with production-sized data to measure runtime and lock behavior.