The query had been slow for weeks. Then came the request: add a new column.
Adding a new column sounds simple. It rarely is. Schema changes ripple through systems like a signal through fiber. They touch the database, the ORM, the API, the UI, and often the data pipeline. Without planning, you get downtime, failed deployments, or silent data loss.
The first step is to decide how the new column fits your schema. Define the type with precision. Use the smallest necessary data type to reduce storage and improve performance. Decide whether the column should allow NULL values. If possible, set defaults to ensure backwards compatibility.
In relational databases like PostgreSQL or MySQL, the safest path for adding a new column in production involves a two-phase deployment. First, add the column with a default value and no constraints. Run migrations in a way that does not lock large tables for long periods. Populate existing rows in batches to avoid overwhelming I/O.
Next, update application code to read from and write to the new column. Only after the application has fully adopted it should you enforce constraints like NOT NULL or add indexes. Adding an index on a large table should be done concurrently, if supported, to prevent locking writes.