The query returned fast, but the data was wrong. You needed another field — right in the middle of a production rollout. The fix was clear: add a new column.
In relational databases, adding a new column is common, but it’s not trivial in systems under heavy load. The right approach depends on the database engine, data size, and uptime requirements. Done wrong, it causes downtime, locks writes, or triggers a costly full-table rewrite.
For PostgreSQL, ALTER TABLE my_table ADD COLUMN new_column_name data_type; is straightforward for empty columns with defaults set to NULL. But adding a column with a non-null default in older versions rewrites the table. That means longer locks. In MySQL, ALTER TABLE also rewrites the table in many cases, unless you’re on a later version with ALGORITHM=INPLACE or INSTANT support.
Schema migrations in production require batching, version control, and proper rollback plans. Use tools like gh-ost or pt-online-schema-change for MySQL when working on large datasets. For PostgreSQL, plan schema changes during low traffic windows or use logical replication for zero downtime.