The table is in production, streaming millions of rows, and the request comes: add a new column.
Adding a column sounds simple. It is not. A careless schema change can lock writes, block reads, or bring down entire services. In SQL databases, a ALTER TABLE ADD COLUMN can trigger a full table rewrite, especially with defaults or constraints. In NoSQL systems, adding a new column or field may be schema-on-write or schema-on-read, but storage and query planning still matter.
The first choice is whether the column can be nullable. A nullable new column avoids expensive backfills. You can deploy it instantly on many systems. If a default is required, set it in application logic until a backfill can run in the background.
For large datasets, use additive schema changes that avoid blocking operations. Break work into steps:
- Add the column as nullable, with no default.
- Deploy code to handle both old and new rows.
- Backfill in small batches.
- Enforce constraints only after the migration is complete.
In PostgreSQL 11+, adding a column with a constant default is fast, but adding with a computed default still needs a rewrite. MySQL’s ALGORITHM=INPLACE can help, but engine and version matter. Check the execution plan before you run migrations on production tables.