The query was slow. The data was wrong. The problem was simple: the table needed a new column.
Adding a new column sounds easy, but in production it can break critical code paths, lock database tables, and disrupt deployments. The right approach depends on scale, schema design, and the database engine you use.
In PostgreSQL, ALTER TABLE ADD COLUMN is the standard way. By default, it’s fast if the column allows nulls and has no default value. Adding a default value without NULL on large tables forces a rewrite, which can take minutes or hours. The safer pattern is:
ALTER TABLE users ADD COLUMN last_login TIMESTAMP NULL;
UPDATE users SET last_login = NOW() WHERE id < 1000;
ALTER TABLE users ALTER COLUMN last_login SET DEFAULT NOW();
This sequence avoids a long lock and lets the change roll out in steps.
In MySQL, ALTER TABLE often copies the entire table. On huge datasets, that’s not acceptable in normal hours. Use ALGORITHM=INPLACE where supported, or rely on online schema change tools like gh-ost or pt-online-schema-change to avoid downtime.