The migration script failed at 2 a.m., and the logs pointed to one cause: a missing new column.
Adding a new column sounds simple, but the cost of doing it wrong is high. In production databases, schema changes can block queries, lock tables, and break downstream services. Understanding the safest and fastest way to add a new column is not optional. It’s survival.
A proper approach starts with the database engine. In PostgreSQL, ALTER TABLE ... ADD COLUMN is straightforward, but the operation can still lock writes if a default value is set without DEFAULT ... NULL. MySQL behaves differently; in older versions, adding a column often causes a full table rewrite. Modern versions with ALGORITHM=INPLACE avoid that cost, but only under specific conditions.
When you add a new column, define the nullability first. Nullable columns are faster to add because the database does not need to backfill values. If you must provide a default, consider doing it in two steps: