Adding a new column in a database is simple in definition but dangerous in practice. A poorly executed change can lock tables, stall writes, or trigger long-running migrations that bring systems to a halt. The right approach depends on scale, uptime requirements, and the database engine you use.
In SQL databases, the ALTER TABLE command is the standard for adding a new column. For small datasets, a direct schema change is fine:
ALTER TABLE users ADD COLUMN last_login TIMESTAMP NULL;
On large tables, altering in place can be risky. Consider doing it in steps:
- Add the new column with a safe, non-blocking migration.
- Backfill data in batches, avoiding large transactions.
- Update application code to read and write to the new column.
- Remove legacy paths once all data is populated and stable.
For PostgreSQL, tools like pg_repack or pg_online_migration can reduce downtime. MySQL users may rely on pt-online-schema-change from Percona. Modern migrations should be tested in staging with production-like volume before rollout.