Adding a new column is one of the most common schema changes. It sounds simple. In production, it often isn’t. Locking tables, breaking queries, and delaying deploys are constant risks if you handle it poorly. The right approach ensures zero downtime, clean migrations, and predictable rollbacks.
First, define the purpose of the new column. Decide on type, constraints, and default values before you touch the database. Avoid implicit nulls unless they’re part of the model. The choice between nullable and non-nullable should be deliberate.
In PostgreSQL, use ALTER TABLE ADD COLUMN for basic additions. For large datasets, reduce locking by breaking the change into steps:
- Add the column as nullable.
- Backfill data in batches.
- Apply NOT NULL constraints after the data is consistent.
In MySQL, avoid schema changes that lock tables on high-traffic systems. Use pt-online-schema-change or native online DDL in newer versions. Run the migration in staging first, then on production replicas before the primary.