Upgrade Strategies
After this lesson, you will be able to:
- Plan and execute PostgreSQL major version upgrades.
- Upgrade Patroni with zero downtime.
- Use
pg_upgradefor in-place upgrades. - Implement logical replication for upgrades.
- Rollback failed upgrades safely.
1. Upgrade Planning
1.1. Pre-upgrade checklist
1.2. Version compatibility matrix
| From → To | Method | Downtime | Risk |
|---|---|---|---|
| 17 → 18 | pg_upgrade | Minutes | Low |
| 15 → 18 | pg_upgrade | Minutes | Medium |
| 12 → 18 | Logical replication | None | Medium |
| 9.6 → 18 | Dump/restore | Hours | High |
1.3. Document current state
2. PostgreSQL Minor Version Upgrade
2.1. Minor upgrade process (e.g., 18.0 → 18.1)
2.2. Rolling minor upgrade
3. PostgreSQL Major Version Upgrade with pg_upgrade
3.1. Architecture
3.2. Install new PostgreSQL version
3.3. Prepare for upgrade (node2 - first replica)
3.4. Update Patroni configuration for v18
3.5. Upgrade remaining nodes
3.6. Post-upgrade tasks
4. Zero-Downtime Upgrade with Logical Replication
4.1. Architecture
4.2. Setup new v18 cluster
4.3. Create publication on v17 (source)
4.4. Create subscription on v18 (target)
4.5. Monitor replication lag
4.6. Cutover procedure
5. Patroni Version Upgrade
5.1. Check compatibility
5.2. Upgrade Patroni (Python package)
5.3. Rolling Patroni upgrade
6. etcd Upgrade
6.1. etcd minor upgrade
6.2. etcd major upgrade (e.g., 3.4 → 3.5)
7. Rollback Strategies
7.1. Rollback pg_upgrade
7.2. Rollback logical replication
7.3. Rollback Patroni upgrade
8. Testing Upgrades
8.1. Staging environment test
8.2. Upgrade rehearsal
9. Best Practices
✅ DO
- Test in staging first: Multiple times.
- Backup everything: Full backup + WAL archive.
- Use pg_upgrade --check: Catch issues early.
- Document procedures: Step-by-step runbook.
- Schedule maintenance window: Off-peak hours.
- Monitor closely: During and after upgrade.
- Keep old version: Don't delete for 1-2 weeks.
- Use logical replication: For zero-downtime.
- Upgrade extensions: After PostgreSQL upgrade.
- Vacuum analyze: After major upgrade.
❌ DON'T
- Don't skip backups: Critical safety net.
- Don't upgrade all at once: Rolling upgrades.
- Don't delete old cluster immediately: Keep for rollback.
- Don't ignore release notes: Breaking changes.
- Don't skip testing: Staging is essential.
- Don't upgrade during peak hours: Plan maintenance window.
- Don't forget about extensions: May need updates.
10. Lab Exercises
Lab 1: Minor version upgrade
Tasks:
- Check current PostgreSQL version.
- Update packages on replica.
- Restart Patroni on replica.
- Switchover to upgraded replica.
- Upgrade old leader.
Lab 2: Major version upgrade with pg_upgrade
Tasks:
- Install PostgreSQL 18 on all nodes.
- Run
pg_upgrade --checkon replica. - Perform
pg_upgradeon replica. - Update Patroni configuration.
- Complete rolling upgrade.
Lab 3: Zero-downtime upgrade with logical replication
Tasks:
- Setup new v18 cluster.
- Create publication on v17.
- Create subscription on v18.
- Monitor replication lag.
- Perform cutover.
- Verify application functionality.
Lab 4: Rollback procedure
Tasks:
- Simulate failed upgrade.
- Stop Patroni on all nodes.
- Restore old configuration.
- Restart old cluster.
- Verify rollback successful.
11. Summary
Upgrade Methods Comparison
| Method | Downtime | Complexity | Risk | Use Case |
|---|---|---|---|---|
| pg_upgrade | Minutes | Low | Low | Minor version jumps |
| Logical replication | None | High | Medium | Zero-downtime required |
| Dump/restore | Hours | Low | Low | Ancient versions |
| pg_upgrade --link | Seconds | Medium | Medium | Same server upgrade |
Typical Timeline
Upgrade Checklist
Next Steps
Lesson 25 will cover Real-world Case Studies:
- Production architecture examples
- Scaling to 1000+ queries/second
- Cost optimization techniques
- Lessons learned from failures
- Industry-specific implementations