Adding a new column should be simple, but small decisions here can break deployments, introduce downtime, or corrupt data. The process demands precision. You must pick the right data type, ensure proper defaults, and plan for how existing rows will be handled.
In relational databases like PostgreSQL, MySQL, and MariaDB, ALTER TABLE is the standard. For PostgreSQL:
ALTER TABLE users ADD COLUMN last_login TIMESTAMP WITH TIME ZONE DEFAULT NOW();
This command will succeed on small datasets. On large tables, though, it can lock writes and delay the migration. For high-traffic production systems, use an online schema change tool or a phased rollout:
- Add the new column as nullable.
- Backfill data in batches.
- Add constraints only after backfill completes.
For analytics databases or warehouses, adding a new column is often instant. But in transactional systems, unplanned schema changes ripple through application code, APIs, and pipelines. Always update ORM models, DTOs, and serialization logic in sync with the migration.