The query ran fast, but the table had changed. You needed a new column, and you needed it without breaking production.
Adding a new column seems simple. In practice, schema changes can cause downtime, table locks, or inconsistent data. The goal is to design and deploy the change so it’s safe, fast, and reversible.
Start by defining the column precisely. Set the correct data type. Decide if it can be NULL or must have a default value. For large datasets, avoid default values that require backfilling the entire table in one transaction—this can lock rows or the entire table. Instead, add the column as nullable, then backfill in small batches.
If the system supports it, use online schema changes. PostgreSQL, MySQL, and most modern databases now have options or tools to add columns without blocking reads and writes. With MySQL, ALTER TABLE ... ADD COLUMN combined with tools like pt-online-schema-change can limit downtime. In PostgreSQL, adding a nullable column without a default is instantaneous. Only when you update existing rows does the operation touch disk heavily.