Adding a new column is one of the most common schema migrations in application development. Done wrong, it can lock tables, slow queries, and cause downtime. Done right, it happens in seconds with zero disruption.
Start with clarity: define the column name, data type, default value, and constraints. A NOT NULL column with no default will fail on creation if rows already exist, so decide early whether to allow nulls or backfill in a staged deployment.
For relational databases like PostgreSQL or MySQL, the safest process is:
- Add the new column as nullable.
- Backfill data in small batches to avoid long-running locks.
- Apply constraints and defaults in a subsequent migration.
In PostgreSQL, an ALTER TABLE ... ADD COLUMN is fast if it’s nullable with no default. Adding a DEFAULT with NOT NULL to a large table in one step can rewrite the table and block writes. Use ALTER TABLE followed by an UPDATE in controlled chunks, then apply constraints.