Adding a new column is simple. Doing it without downtime, data loss, or query failures is not. Whether you’re working in PostgreSQL, MySQL, or any other relational database, the execution details matter.
First, define the new column with the correct data type. If you’re extending a critical table, consider adding it as NULL initially to avoid locking issues. Many databases will rewrite the entire table if you add a column with a non-null default value. In high-traffic systems, that’s unacceptable.
Second, backfill data in controlled batches. Use transactions where possible, but avoid long transactions that hold locks. Throttle your updates so the database stays responsive.
Third, update the application code to read from and write to the new column. Deploy this change alongside a feature flag or in a staged rollout. This protects against unexpected nulls or inconsistent states if your migration lags.