The table was ready, but the schema was not. You needed a new column, and you needed it fast.
Adding a new column sounds simple, but the wrong approach can lock your database, stall writes, or break production before you push the change. The key is knowing the safest path for your schema and your workload.
In SQL databases like PostgreSQL, MySQL, and MariaDB, ALTER TABLE is the standard tool. The syntax is simple:
ALTER TABLE users ADD COLUMN last_login TIMESTAMP;
Under light load, this works fine. But in large datasets, adding a new column with a default value can trigger a full table rewrite, resulting in downtime or latency spikes. Avoid defaults during the initial add. Instead:
- Add the column without a default.
- Backfill data in small batches.
- Apply the default and constraints after the backfill completes.
For PostgreSQL, ADD COLUMN without a default is instant. MySQL’s performance depends on the storage engine and version. InnoDB on modern MySQL supports instant column add in certain cases, but test on a staging environment first.