The schema broke on a silent Thursday night. Logs filled with errors. Queries failed. The fix was simple: a new column.
Adding a new column to a database table seems routine. It is also dangerous. A careless migration can lock tables, block writes, and bring down production. The right approach keeps the change atomic, predictable, and reversible.
In SQL, the command is direct:
ALTER TABLE users ADD COLUMN last_login TIMESTAMP NULL;
But the workflow is where speed meets safety. Always check table size. On large datasets, an ALTER TABLE can block queries for minutes or hours. Use online schema change tools like gh-ost or pt-online-schema-change to add a new column without downtime. These tools copy data in the background, apply changes incrementally, and swap tables fast.
For PostgreSQL, version 11+ allows adding a column with a default value without rewriting the whole table, reducing overhead. Avoid non-null columns with defaults in older versions. Instead, add the column as nullable, backfill in batches, then alter to non-null.