The query ran. The results came back. You realized the schema had changed. You need a new column.
Adding a new column is one of the most common database operations, yet it can carry hidden risks. It can lock tables. It can impact replication. It can break application code if not coordinated. Done carelessly, it causes downtime. Done well, it ships cleanly in production with zero disruption.
The first step is to decide column type and constraints. Avoid guessing. Map the exact data behavior: nullability, default values, indexing. Changing these later is costly. Next, choose the migration strategy. For small tables, a direct ALTER TABLE ADD COLUMN may be fine. For large datasets, migrate in stages:
- Add the column as nullable without defaults.
- Backfill data in batches to avoid long locks.
- Apply constraints and indexes after the data is filled.
Test the migration in a staging environment with production-sized data. Measure execution time. Watch for lock contention. If downtime is unacceptable, schedule during low-traffic windows or use online schema change tools like pt-online-schema-change or gh-ost.