How do you perform zero-downtime schema migrations in PostgreSQL?
Answer
Zero-downtime migrations require careful ordering to avoid table locks. Safe operations: adding a nullable column (instant in Postgres 11+ with a default), adding an index (CONCURRENTLY), adding a not-exists constraint. Dangerous operations requiring workarounds: (1) Add NOT NULL column: add nullable → backfill → add constraint as NOT VALID → VALIDATE CONSTRAINT (only takes ShareUpdateExclusiveLock). (2) Add foreign key: ADD CONSTRAINT ... NOT VALID then VALIDATE CONSTRAINT. (3) Reindex: REINDEX CONCURRENTLY. (4) Change column type: add new column → backfill → update trigger → cut over → drop old column. (5) Rename column: use a view or rename-then-add-alias approach. Tools: squawk (CI linter), pgroll, and frameworks like strong_migrations (Rails gem) enforce safe patterns.
Previous
What is pg_partman and how does it simplify partition management?
Next
What are exclusion constraints in PostgreSQL?
More PostgreSQL Questions
View all →- Advanced What is the query planner in PostgreSQL and how does it work?
- Advanced What causes poor query plans and how do you fix them?
- Advanced What is logical replication in PostgreSQL?
- Advanced How do you implement row-level security (RLS) in PostgreSQL?
- Advanced What is table bloat in PostgreSQL and how do you fix it?