Adding a new column is not just about running ALTER TABLE. It is about speed, safety, and zero-downtime. In large databases, a blocking change can freeze reads and writes. On busy systems, even milliseconds matter.
First, define the new column with a default value set at the application layer, not in the migration, to avoid heavy table rewrites. Use NULL defaults if possible. Apply the column addition in small, safe steps:
- Add the new column, nullable.
- Backfill data in controlled batches.
- Update application code to write to the column.
- Gradually enforce
NOT NULLor constraints when it is safe.
For PostgreSQL, ALTER TABLE ADD COLUMN with a default can lock the table and rewrite it. Avoid that by adding the column without a default and filling it in later. MySQL has similar locking concerns, though online DDL in recent versions can reduce downtime. Always check execution plans before merging to production.