Safe Migrations

Problem / Context

You need to add columns, indexes, and constraints to production tables that serve live traffic. The goal is to change the schema safely with minimal blocking, clear rollback or roll‑forward options, and good communication.

Core Concept

Goal: Apply DDL in production with minimal locks, clear rollback or roll-forward, and good communication.

1. Core Principles

2. Standard Playbook

  1. Plan (list risk ops: type changes, defaulted columns, big table rewrites)
  2. Test on staging clone (row counts similar)
  3. Backup or confirm PITR window healthy
  4. Communicate window + fallback
  5. Run migration tool (Flyway, Sqitch, Rails, etc.)
  6. Monitor: locks, errors, replication lag
  7. Validate post-change (schema, sample queries)

3. Change Catalog (where to find details)

This chapter is the high-level playbook. Detailed, copyable SQL lives in focused chapters:

Quick references

4. Rollback vs Roll Forward

DDL rollback can be messy. Prefer forward fixes: create a missing index, adjust a new column, etc. Keep a quick revert script only for trivial additions if disaster strikes early.

5. Monitoring During Migration

Watch:

6. Checklist Before You Run

7. Common Pitfalls

Mistake Pain
Adding column WITH default on huge table (old versions) Full table rewrite & lock
Single transaction with many long steps Lock pileups, hard rollback
No batch size limit in backfill Long running transaction, bloat
Dropping index before verifying replacement Sudden query regression

Start minimal, measure, iterate. Safety is structure + patience.

8. Variations and Trade‑Offs

9. Pitfalls

10. Recap (Short Summary)

Declare structure fast, migrate data gradually, validate correctness later. Use CONCURRENTLY and NOT VALID to keep locks short, and favor roll‑forward fixes.

11. Optional Exercises

12. Summary (Cheat Sheet)

Key Principle: Declare structure fast (metadata), migrate data gradually, validate correctness late.

References