Triamorph Systems

Cloud & DevOps · 13 min read · Sep 08, 2024

Zero-Downtime PostgreSQL Major Version Upgrades: A Step-by-Step Guide with Logical Replication and pg_upgrade

Step-by-step production runbook for upgrading PostgreSQL major versions with zero downtime using logical replication, sequence syncing, and DNS blue-green cutover.

By Aman Aslam

Core Engineering Takeaways

  • Logical Replication streams row-level changes (DML) across different PostgreSQL major versions without locking tables or interrupting active transactions.
  • Sequences do not synchronize automatically over logical replication; export sequence values immediately prior to the cutover window.
  • Always configure reverse replication from the new target cluster back to the old cluster during switchover to allow instantaneous rollback without data loss.

Upgrading PostgreSQL between major versions (e.g. from Postgres 14 to Postgres 17) cannot be achieved via standard physical streaming replication because internal on-disk binary formats change. Shutting down the database for hours to run `pg_dump` and `pg_restore` is impossible for 24/7 global businesses. By implementing native PostgreSQL Logical Replication, engineering teams can sync live production data to a new target cluster in real time and execute a DNS cutover in under 30 seconds.

1. In-Place pg_upgrade vs Logical Replication Blue-Green

Evaluating risk profiles and maintenance windows for enterprise databases

`pg_upgrade --link` is fast but requires a complete database shutdown during execution and offers no instant rollback if queries perform unexpectedly. Logical replication creates a decoupled Blue-Green environment where the new cluster can be thoroughly benchmarked under read traffic before switching primary traffic.

Cutover Downtime
< 20 Seconds
DNS / Connection pooler reroute
Replication Lag
< 10ms
Low-latency WAL logical decoding
Rollback Capability
Instant
Bi-directional reverse sync enabled

2. Step-by-Step Publication & Subscription Configuration

Creating publication slots and initial table snapshot synchronization

On the source database, we set `wal_level = logical` and create a publication encompassing all application tables. The target database subscribes to the source, copying initial table states and streaming incremental changes.

db/ops/logical_replication_setup.sql (sql)
-- SOURCE CLUSTER (PostgreSQL 14)
ALTER SYSTEM SET wal_level = 'logical';
ALTER SYSTEM SET max_replication_slots = 10;
ALTER SYSTEM SET max_wal_senders = 10;
-- Reload configuration
SELECT pg_reload_conf();

-- Create publication for all tables
CREATE PUBLICATION upgrade_publication FOR ALL TABLES;

-- TARGET CLUSTER (PostgreSQL 17)
-- (Pre-requisite: Apply database schema without foreign keys and triggers)
CREATE SUBSCRIPTION upgrade_subscription
    CONNECTION 'host=source-pg14.internal port=5432 dbname=production user=replicator password=SECRET'
    PUBLICATION upgrade_publication
    WITH (copy_data = true, create_slot = true);

-- Monitor sync status on target
SELECT subname, active, received_lsn, last_msg_send_time FROM pg_stat_subscription;

3. Handling Sequences, Triggers & DDL Sync

Critical pre-cutover synchronization checks

Because logical replication does not replicate DDL schema migrations or sequence updates (`nextval()`), sequences must be queried on the source and updated on the target using `setval()` right before routing application traffic.

4. The 30-Second Cutover Runbook & Zero-Risk Rollback

Executing friction-free switchover with PgBouncer connection pausing

During the cutover window: 1) Pause PgBouncer connections, 2) Sync final sequence values, 3) Re-enable triggers and foreign keys on target, 4) Point PgBouncer to the PostgreSQL 17 target cluster, 5) Resume traffic. Total downtime: under 20 seconds.

Tags: zero downtime postgresql upgradepostgresql logical replication major version migrationpg_upgrade fallback strategyblue green postgresql database switchoveraws rds aurora postgres upgrade zero downtime

Related Engineering Practice & Case Studies

Engineering Practice Blueprint

Enterprise Workflow Automation & Event Orchestration →

Resilient event-driven webhook architectures, queue processing, and reliable data synchronization.

View Practice Blueprint & Capabilities →
Production Client System

Flux Enterprise Workflow Automation →

High-reliability webhook orchestrator processing 50,000+ daily sync records with zero dropped events.

Read Deployed Case Study (Zero data drops across 50,000+ daily event streams) →

Related Engineering Articles & Guides

Explore all technical guides in our Articles & Guides Index → or discuss your architecture with our engineers via Technical Discovery →