The query ran fast, but the schema was already out of date. You need a new column, and you need it without breaking production or losing data.
Adding a new column in a modern database is more than an ALTER TABLE statement. Schema changes can trigger table rewrites, lock rows, or spike CPU. The right approach depends on table size, engine type, and uptime requirements.
In PostgreSQL, adding a column with a default non-null value rewrites the entire table. For billions of rows, this is slow and blocks inserts. Instead, first add the column as nullable with no default. Then backfill the data in small batches. Finally, set the default and mark it NOT NULL once complete.
In MySQL or MariaDB, ALTER TABLE often copies the table in the background. With ALGORITHM=INPLACE, changes can be faster, but watch for edge cases with indexes and foreign keys. Always test on a staging instance with production-sized data.