The query ran fast, but the table was old. You needed a new column. Not later. Now.
Adding a new column sounds simple, but the wrong approach can lock transactions, slow queries, or even drop connections. In modern production systems, schema changes are dangerous. The key is to know which method works for your database size, traffic patterns, and runtime constraints.
Start with intent. Define the data type and default value before touching the schema. Use ALTER TABLE with precision. For small tables or low-traffic environments, a straightforward ALTER is fine. In high-traffic systems, consider building the column in multiple steps:
- Create the column without a default or constraints.
- Backfill data in controlled batches.
- Add constraints after validation.
On PostgreSQL, adding a new column without a default is practically instant. Adding with a constant default forces a full table rewrite. MySQL behaves differently, with some operations faster but others more prone to locking. Check the engine-specific documentation before deploying changes.