Adding a new column is simple, but the method you choose can make or break your deployment. In databases with millions of rows, an unsafe ALTER TABLE can lock writes and bring down production. In distributed systems, a schema change can ripple through services and caches. The right approach depends on size, latency needs, and your tolerance for downtime.
The common patterns are:
- Add a new column with a default value using
ALTER TABLE ... ADD COLUMN. This is fast for small datasets. For large tables, it can cause locks while rewriting rows. - Add a nullable column without a default. This avoids a costly table rewrite, letting you backfill in smaller batches.
- Use an online schema change tool like pt-online-schema-change or gh-ost to keep the table writable during migration. These stream changes in the background.
- Add versioned columns when you need zero downtime, writing to both old and new columns until the transition is complete.
Best practice is to stage the change: