A database migration is running in production. You need to add a new column. There’s no room for guesswork.
Adding a new column sounds simple, but the reality is shaped by data size, schema dependencies, query load, and zero-downtime requirements. Small mistakes can lock tables, stall writes, or block API calls. That’s why planning the change is as important as the syntax itself.
The safest way to add a new column is to start with a migration script that avoids long-running locks. For most SQL databases, ALTER TABLE will block concurrent writes if done without care. On large datasets, use operations that run asynchronously or in multiple steps. For example:
- Create the new column with a default of
NULLto avoid backfilling in a single transaction. - Backfill data in small batches, using controlled transactions.
- Add constraints, indexes, or defaults only after the data is in place.
In PostgreSQL, adding a column without a default is instant. Adding with a non-null default rewrites the table. MySQL behaves differently depending on storage engine. Always check your database version’s documentation because the performance profile changes.