Adding a new column is one of the most common schema changes in software. Done wrong, it can bring down a production database. Done right, it’s seamless and invisible to users. The key is to understand both the database engine and the data migration path.
First, decide if the new column should allow NULL values or require defaults. Setting a default for large tables can lock the table during the update, depending on the database. In PostgreSQL, an ALTER TABLE ADD COLUMN with a default will re-write every row in older versions. In MySQL, adding a column with a NOT NULL constraint can block writes if not handled with tools like pt-online-schema-change.
For zero-downtime migrations, add the column without a default, populate it in batches, index only after the backfill, and finally enforce constraints when safe. Monitor replication lag. Test the migration in a staging environment with production-sized datasets. Always check execution plans before running the code in production.