You opened the migration and typed it: ALTER TABLE orders ADD COLUMN priority VARCHAR(20);. A new column. Simple, but not always safe.
Adding a new column is one of the most frequent schema changes in modern systems. Done right, it’s seamless. Done wrong, it can lock tables, block writes, or break production. The execution depends on database type, size, and uptime requirements.
In PostgreSQL, adding a nullable column with a default avoids table rewrites if the default is set in a second step. For example:
ALTER TABLE orders ADD COLUMN priority VARCHAR(20);
UPDATE orders SET priority = 'normal' WHERE priority IS NULL;
ALTER TABLE orders ALTER COLUMN priority SET DEFAULT 'normal';
This minimizes locks while ensuring existing rows are consistent.
In MySQL, adding certain types of columns can still trigger a full table copy. For large datasets, use ALGORITHM=INPLACE or ALGORITHM=INSTANT if the version supports it. These options can reduce downtime, but must be verified in staging before production rollout.