A new column changes a schema. It can add data you need or unlock a feature. In a relational database, adding a column seems easy. It’s one line of SQL:
ALTER TABLE users ADD COLUMN last_login TIMESTAMP;
But every production system carries risk. The wrong column type can cause failures. A blocking migration can freeze writes. An unindexed field can slow the app. Understanding how to add a new column safely means knowing the database engine, its locking behavior, and how your application reads and writes data.
For PostgreSQL, adding a nullable column with no default value is fast. It updates metadata only. Adding a non-null column with a default can rewrite the whole table, locking it for the duration. To avoid downtime, you can:
- Add the column as nullable.
- Backfill data in small batches.
- Add the NOT NULL constraint after the backfill.
MySQL behaves differently. Adding a new column can trigger a table copy unless you use ALGORITHM=INSTANT (available in recent versions). On older versions, expect a full table rebuild and plan migrations in off-peak hours.