The table was live in production when the request came in: add a new column. No downtime. No data loss. No rollback. Just the update, clean and fast.
Adding a new column sounds simple, but in systems under load, it can wreck performance or block queries for minutes. The wrong migration locks a table. Traffic backs up. Queues overflow. You need a method that changes schema without pain.
First, define why the new column exists. Is it nullable? Does it need a default value? Do you need to backfill historical data? These answers shape the migration path. For large datasets, skip the full backfill in a single transaction. Instead, deploy in safe steps:
- Add the new column with
NULLallowed. - Backfill in batches, controlling transaction size.
- Add constraints or indexes only after data is in place.
In PostgreSQL, ALTER TABLE ADD COLUMN is fast for nullable columns. Adding NOT NULL with a default rewrites the whole table. MySQL behaves differently depending on storage engine, version, and column definition. Always test on a staging dataset with a realistic row count.