Adding a new column is one of the most common changes in any database schema. It looks simple, but the wrong approach can lock tables, block writes, or take your system offline. The right approach depends on your database engine, your dataset size, and your uptime requirements.
In SQL, adding a column is done with ALTER TABLE. For small tables, a direct command works:
ALTER TABLE users ADD COLUMN last_login TIMESTAMP;
On large production tables, this command can block queries while the database rewrites the table. Postgres, MySQL, and other engines each handle schema changes differently. For example:
- Postgres can add a nullable column without rewriting the table. But adding a column with a default value before Postgres 11 rewrites all rows.
- MySQL uses an in-place algorithm for some column additions, but not all. ENUM expansions or column type changes can still block.
- SQLite supports adding a column at the end, but not removing or reordering columns.
For zero-downtime deployments, you can add the new column in one release and backfill data in the background. Once complete, apply constraints, defaults, or indexes in a separate migration. This prevents long locks and keeps the application live. Tools like gh-ost or pg_online_schema_change allow online migrations without impacting reads and writes.