Adding a new column sounds simple, but the wrong approach will lock tables, stall queries, or crash production. Choosing the right method depends on data size, database engine, and uptime requirements.
In SQL, a basic ALTER TABLE ... ADD COLUMN statement works for small datasets. For large tables, it can cause downtime. Engines like PostgreSQL, MySQL, and MariaDB handle schema changes differently. PostgreSQL often adds new columns instantly if they have no default; MySQL may need a full table rewrite depending on the storage engine and options.
When adding a new column to a live system, plan for concurrent reads and writes. Use background migrations if your database supports them. Break the change into phases:
- Add the column without a default or constraint.
- Backfill rows in batches to prevent locking.
- Add constraints or defaults after the backfill is complete.
Nullability is critical. A non-nullable new column with no default will fail to add unless you rewrite all existing rows. Defaults can help, but in some engines, setting a default during ALTER TABLE triggers a blocking rewrite.