Adding a new column in a production database is simple to describe but risky to execute. The design choice can affect query performance, application logic, and deployment stability. When done without planning, it can trigger locks, slow writes, and backfilling nightmares. When done right, it can be a smooth schema evolution that supports growth without downtime.
Start by defining the new column in your migration file. Set explicit data types and defaults. Resist the urge to backfill all data in the same transaction. Large backfills can lock rows and block queries. Instead, run the migration in two steps:
- Add the new column with a nullable definition.
- Backfill data in small batches through background jobs or controlled scripts.
If your database supports it, use ALTER TABLE ... ADD COLUMN with non-blocking operations. PostgreSQL can add a nullable column instantly, but adding a default to existing rows rewrites the entire table. In MySQL and other engines, check for online DDL options to reduce table locking.