Adding a new column to a database seems simple. It isn’t. Done wrong, it locks your tables, blocks writes, and could take an entire service down. Done right, it’s seamless, and production barely notices.
The key is understanding how your database engine handles schema changes. For PostgreSQL, ALTER TABLE ADD COLUMN is fast when the column has no default and allows nulls. Set a default and it rewrites the table. For MySQL, ALTER TABLE can lock the table unless you use ALGORITHM=INPLACE where supported. In both cases, schema migrations should be deployed in small, reversible steps.
Best practice:
- Add the column as nullable with no default.
- Backfill data in batches to avoid load spikes.
- Add defaults and constraints in separate migrations after backfill.
- Test the full migration sequence in staging with realistic data.
For large datasets, online schema change tools like pg_online_schema_change or gh-ost keep downtime near zero. These tools copy data to a new table structure while streaming changes, then swap at the end.