The database waits. A request comes in. A new column must be added, and it has to work without breaking the system.
Adding a new column sounds simple, but the wrong approach can lock tables, stall queries, and cause downtime. The right method keeps production running and deploys cleanly.
Start with the schema. Know exactly which table needs the column, the data type, and the default value. In most relational databases, ALTER TABLE is the command, but the execution plan depends on the database engine. PostgreSQL can add certain columns instantly if they have no default; MySQL may rewrite the whole table depending on the column type. This step matters.
If the column needs a default value, consider adding it in two steps:
- Add the column without a default to avoid table rewrites.
- Backfill the data in small batch updates.
For large datasets, run migrations online. Tools like pg_online_schema_change or gh-ost can add a new column without locking rows. In cloud-managed databases, enable features that support concurrent schema changes.