Adding a new column should be simple. In reality, the wrong approach can lock tables, break queries, and slow production traffic. Whether you’re working with PostgreSQL, MySQL, or a distributed store, you need a process that adds a new column safely, without downtime or data loss.
First, define the schema change with precision. In SQL, use ALTER TABLE to add the new column. Keep it null by default unless backfilling immediately, as non-null defaults can rewrite the entire table on some engines. In PostgreSQL, avoid NOT NULL on first add; set defaults in a later, indexed update step to prevent locks.
Second, backfill data in batches. Use small transactions to limit load on replicas and primaries. Monitor performance with query plans before and after the new column update.
Third, update application code in a phased rollout. Deploy code that reads the new column after it exists but before it becomes critical. Write to both the old and new columns during a dual-write phase if necessary. Once the transition is stable, remove dependencies on the old structure.