The database groaned under the weight of another migration. You need a new column, and you need it without breaking production. One wrong step can lock tables, slow queries, and frustrate every user on the system.
Adding a new column in SQL is simple in syntax, but the real work is in doing it safely. Schema changes are often the riskiest database operations. They need careful planning, precise execution, and rollback strategies that work under pressure.
Start with the DDL. In PostgreSQL, the basic command is:
ALTER TABLE users ADD COLUMN last_login TIMESTAMP;
This runs instantly for metadata-only columns without defaults on large tables. But if you add a default with NOT NULL, the database rewrites the whole table. That can lock reads and writes for longer than your SLA allows. The fix: create the column nullable, backfill in small batches, then alter it to NOT NULL after the data is ready.
In MySQL, the same ALTER TABLE may lock the full table unless you use ALGORITHM=INPLACE or target a version that supports instant ADD COLUMN for certain types. Even then, test on a staging dataset equal in size to production.