Adding a new column sounds small. It isn’t. If you aren’t careful, it can lock tables, slow queries, and create downtime no one forgives. The goal is to make the schema change fast, safe, and reversible.
First, decide on the correct column type. Match it to the data you’ll store, and anticipate growth. Storing integers as strings will create pain later. Keep nullability strict unless real-world logic demands flexibility.
Next, plan the migration. For large production tables, use online schema change tools like pt-online-schema-change or native features such as PostgreSQL’s ADD COLUMN with default expressions in newer versions. Break changes into phases:
- Add the column as nullable.
- Backfill data in batches.
- Apply constraints or defaults after the backfill completes.
Avoid updating every row at once—it can trigger massive locks. Use indexed queries for backfill jobs. Monitor replication lag if you run read replicas.