10 — Physical Streaming vs Native Logical vs Multi-Master & Distributed SQL¶
Series: PostgreSQL Multi-Master & Active-Active Architectures
Author: Jean-Marie Renouard (jmrenouard) — LightPath
License: MIT License
1. The Five-Tier PostgreSQL Replication Spectrum¶
Teams often jump straight to active-active multi-master without evaluating the two native single-writer replication tiers built into core PostgreSQL: Physical Streaming Replication (async or sync) and Native Uni-Directional Logical Replication. Moving rightward across the five tiers trades engine simplicity for selective filtering, multi-writer agility, or global consensus.
flowchart LR
T1["T1: Physical Async (pg17-async-streaming)"] -->|"Block WAL, RPO>0"| S1["Hot Standby (RO 25006)"]
T2["T2: Physical Sync (pg17-sync-streaming)"] -->|"Sync WAL, RPO=0"| S2["Sync Standby (RTT-coupled)"]
T3["T3: Native Logical (pg17-logical)"] -->|"pgoutput Pub/Sub"| S3["Subscriber (Cross-Ver, Seq Trap)"]
T4["T4: Logical Multi-Master (pg-birep / Spock / Bucardo / PGD)"] <-->|"Async Mesh / CRDT"| S4["Peer Writers (PA/EL)"]
T5["T5: Distributed SQL & Shards (Yugabyte / Cockroach / Citus)"] <-->|"Raft Quorum / 2PC"| S5["Global ACID (PC/EC, 40001)"]
Commit latency separates asynchronous local writes (T1, T3, T4 async) from synchronous standby WAL acknowledgment (T2) and multi-round quorum consensus (T5):
2. Architectural Analysis of the Five Tiers¶
Tier 1 — Physical Streaming Replication: Async (pg17-async-streaming)¶
- Mechanism:
walsenderships raw 8 KiB WAL page images byte-for-byte towalreceiverover a physical slot (pg_basebackup). Standby replays identical heap/index blocks in continuous recovery. - Strengths: Zero schema caveats apply to keyless tables, DDL,
SERIAL/IDENTITYsequences,pg_largeobject,TRUNCATE, and all C extensions. They replicate identically at local primary speed with zero write conflicts. - Hard Limits: Standby is strictly read-only (
SQLSTATE 25006on writes). Requires identical major PG version, CPU arch, andglibccollation lineage. Crash failover risks \(\text{RPO} > 0\) (unshipped WAL bytes).
Tier 2 — Physical Streaming Replication: Sync (pg17-sync-streaming)¶
- Mechanism: Physical WAL streaming plus
synchronous_standby_namesandsynchronous_commit = on(standby disk flush) orremote_apply(visible on standby before primaryCOMMITreturns). - Strengths: Strict
RPO = 0, 100% PostgreSQL parity, zero conflicts. Paired with Patroni + etcd (#15), it delivers fenced automated failover (RTO ~ 10–30 s). - Hard Limits: Commit latency tracks standby RTT: locally (
results/benchmark_summary.csv), Suite A drops52.8%(866.82 -> 409.48 TPS). At25 msWAN delay (Suite F), throughput collapses-86.1%(57.00 TPS,p95 = 353.49 ms). Without aFIRST 1 (s1, s2)quorum standby list, losing the sole sync standby stalls all primary writes.
Tier 3 — Native Uni-Directional Logical Replication (pg17-logical)¶
- Mechanism:
wal_level = logicaldecodes WAL withpgoutputinto row-levelINSERT/UPDATE/DELETEstreams filtered byCREATE PUBLICATIONand applied byCREATE SUBSCRIPTION. - Strengths: Table-level granularity,
WHERErow filters, column lists, writable subscriber for local reporting tables, cross-OS portability, and zero-downtime cross-major-version upgrades (PG 15 -> 17 -> 18). - Three Operational Traps:
- Keyless Tables (
SQLSTATE 55000):UPDATE/DELETEfails on publisher tables without aPRIMARY KEYorREPLICA IDENTITY FULL. - No Auto-DDL:
ALTER TABLE ADD COLUMNdoes not propagate. Apply DDL subscriber-first or apply workers halt. - Sequence Non-Synchronization Trap (
SQLSTATE 23505): Core PG 15–17 logical replication never syncs sequencelast_value. After50publisher inserts (id = 1..50), the subscriber sequence stays at1. Promoting without manualsetval()immediately triggers23505collisions.
Tier 4 — Native Bidirectional (pg-birep) vs Multi-Master Extensions (pgEdge, Bucardo, EDB PGD)¶
- Native Bidirectional (
#1 pg-birep,origin = none): BreaksA -> B -> Aloops natively in PG 16–18 (875.91 TPSSuite A,824.0425 msWAN). Without conflict resolution, PK/UNIQUEhalts (23505) and increments diverge (-71,063in D), so restrict to disjoint keyspaces. - Extensions (
#2 pgEdge Spock 5,#9 Bucardo 5,#4 EDB PGD): Add LWW, global IDs (snowflake/timeshard), and auto-DDL (Spock,PGD). Only CRDT Delta-Apply (spock.delta_apply:1,837.64 TPS,0drift in Suite D) or EDB PGD CAMO/Eager Raft avoid Lost Updates (-278,698Spock LWW,-531,620Bucardo).
Tier 5 — Distributed SQL Consensus (YugabyteDB, CockroachDB) & Sharding (Citus)¶
- Consensus (
#5 YugabyteDB,#6 CockroachDB): Per-tablet/range Raft (RF = 3) guarantees globalUNIQUE,RPO = 0, and 1-of-3 survival. Costs include40001retries (685failedsysbenchtx on CockroachDB), 2-of-3 halt (57014), and WAN drop (5.78/10.06 TPSat25 ms). - Sharding (
#7 Citus): Scales tenant-keyed PG 17 writes (0drift), but blocks non-shardUNIQUE(0A000), requires Coordinator HA, and drops-93.0%(21.67 TPS) under25 msWAN 2PC.
3. 25-Point Feature Matrix: Streaming vs Logical vs Multi-Master¶
| # | Criterion | T1: Phys Async | T2: Phys Sync | T3: Logical Uni | T4: Logical MM | T5: Dist SQL / Citus |
|---|---|---|---|---|---|---|
| 1 | Repl. Unit | 8 KiB WAL | 8 KiB WAL | Row tuples | Tuples / triggers | Raft ranges / shards |
| 2 | Active Writers | 1 Primary |
1 Primary |
1 Pub (+ Sub) |
N Peers |
N Nodes / Coord |
| 3 | CAP / PACELC | PA/EL |
PC/EC |
PA/EL |
PA/EL (PGD: hyb) |
PC/EC |
| 4 | RPO (1-Loss) |
> 0 |
0 |
> 0 |
> 0 (CAMO: 0) |
0 (RF>=3) |
| 5 | Commit Latency | Local fsync |
+ 1*RTT |
Local fsync |
Local (Eager: Raft) | 2*RTT Raft / 2PC |
| 6 | Standby Writes | No (25006) |
No (25006) |
Yes (drift risk) | Active-Active | Active-Active |
| 7 | Major Upgrade | No | No | Yes (15->17->18) | Yes (Spock) |
Rolling upgrade |
| 8 | Cross-OS/ABI | No | No | Yes | Yes | Yes |
| 9 | Row/Col Filter | No | No | Yes (WHERE) |
Partial (repset) |
Placement zones |
| 10 | Auto-DDL | 100% WAL | 100% WAL | No (manual) | No #1/#9; Yes #2/#4 |
Yes (global) |
| 11 | Keyless DML | 100% Native | 100% Native | 55000 (RI) |
55000/drop/reject |
Hidden rowid/0A000 |
| 12 | Sequence Sync | 100% WAL | 100% WAL | None (23505) |
Step / snowflake |
Global / unique_rowid |
| 13 | Write Conflicts | None | None | Sub -> 23505 |
Halt / LWW / CRDT | 40001 retry |
| 14 | Lost Update Safe | Yes | Yes | If Sub RO | delta_apply/Eager |
Yes |
| 15 | Sec. UNIQUE |
Yes | Yes | Pub only | 23505 halt/drop |
Global (0A000 Citus) |
| 16 | Large Objects | 100% WAL | 100% WAL | No | No (lolor: Yes) |
No (Citus Coord) |
| 17 | TRUNCATE |
100% WAL | 100% WAL | Yes (pub opt) | Yes (#1/#2/#9) |
Yes |
| 18 | Locks/LISTEN |
Primary-local | Primary-local | Pub-local | Node-local | Global/0A000/42601 |
| 19 | PG Extensions | 100% | 100% | 100% | 100% | High YB / No CRDB |
| 20 | Slot/Log Bloat | Physical slot | Physical slot | Logical slot | WAL / delta tbl |
Raft GC |
| 21 | Peer Outage | Primary OK | Primary stalls | Primary OK | Peers OK | 1/3 OK; 2/3 halt |
| 22 | Promotion | pg_promote |
pg_promote |
Manual cutover | LB reroute | Auto Raft election |
| 23 | Post-Fail Seq | Auto-safe | Auto-safe | Manual setval |
Step / snowflake |
Auto-safe |
| 24 | WAN 25ms |
Low (async) | -86.1% | Low (async) | Low (0..-6%) |
-76% to -97% |
| 25 | Ops / TCO | Lowest | Low (Patroni) | Low-Med | Med-High | High (Raft/retries) |
4. Empirical Benchmarks & Extended Limits Alignment (T11–T18)¶
Verified cross-tier results from results/benchmark_summary.csv, results/benchmark_wan_sweep.csv, and results/extended_functional_matrix.csv (all 10 topologies):
| Topology | Tier | Suite A TPS | WAN 10 / 25 / 50 ms TPS |
Suite D Drift | T15 RAW p50 / p95 |
T18 Peer Sequence State |
|---|---|---|---|---|---|---|
pg17-async-streaming |
T1 | 610.15 |
587.2 / 599.6 / 619.4 |
0 |
9.65 / 25.13 ms |
Safe: WAL 66\|true (next=67) |
pg17-sync-streaming |
T2 | 409.48 |
121.8 / 57.0 / 25.8 |
0 |
1.88 / 2.74 ms |
Safe: WAL 66\|true (next=67) |
pg17-logical |
T3 | 532.38 |
636.6 / 654.1 / 395.5 |
0 |
1.68 / 2.14 ms |
TRAP: 1\|false -> 23505 |
pg-birep |
T4 | 875.91 |
274.5 / 824.0 / 223.3 |
-71,063 |
1.47 / 2.28 ms |
Std seq 1\|false -> 23505 (step +2) |
pgedge (Spock 5) |
T4 | 556.06 |
644.6 / 646.8 / 687.0 |
-278,698 (0 CRDT) |
1.16 / 1.63 ms |
Std seq 1\|false -> 23505 (snowflake) |
bucardo |
T4 | 735.38 |
593.0 / 763.2 / 612.6 |
-531,620 |
548.7 / 1,275 ms |
Std seq 1\|false -> 23505 |
citus |
T5 | 309.04 |
44.4 / 21.7 / 8.8 |
0 |
1.71 / 13.89 ms |
Safe: single coord (next=51) |
yugabyte / cockroach |
T5 | 173.6 / 42.1 |
15.4/5.8/3.1 vs 19.4/10.1/4.5 |
0 / 0 |
2.0/2.8 vs 4.1/5.0 ms |
Safe: Raft (next=101 / 51) |
Empirical proof (T12, T18, L7): physical streaming retained 3,088 B WAL offline, replicated keyless DML + DDL, blocked standby writes (25006), and preserved 66|true across pg_promote(). Conversely, pg17-logical diverged on T12 (pub=upd_n1 vs sub=DELETED), rejected keyless updates (55000), retained 27,320 B WAL, and hit 23505 on failover until setval() restored id=51.
5. Failure, Promotion & Zero-Downtime Upgrade Playbooks¶
5.1 Physical Streaming Promotion (T1 / T2)¶
- Fence the failed primary (Patroni watchdog / STONITH) to prevent split-brain writes.
- Promote standby (
SELECT pg_promote(wait => true);or Patroni DCS election) and confirmpg_is_in_recovery() = f. - Zero sequence resync or DDL replay is needed. Switch HAProxy or PgBouncer immediately.
5.2 Logical Cutover & Zero-Downtime Major Upgrade (PG 15 -> 17 -> 18)¶
- Pre-flight: Verify PKs on 100% of tables, load
pg_dump --schema-onlyon PG 18, createPUBLICATION+SUBSCRIPTION(copy_data = true). - Freeze & Drain: Set source read-only (
SET default_transaction_read_only = on) and wait untilreplay_lsn = sent_lsn. - Mandatory Sequence Sync (Prevents
23505): Advance every sequence on target before opening writes:
SELECT format('SELECT setval(%L, %s, true);', schemaname || '.' || sequencename, COALESCE(last_value, start_value) + 1000)
FROM pg_sequences WHERE schemaname = 'public';
- Cutover:
DROP SUBSCRIPTION, repoint pooler to PG 18, and retain old primary read-only for fallback.
5.3 Migration & Selection Guidance¶
- Stay on Tier 1/2 (Physical Streaming + Patroni) for 80%+ of OLTP workloads: zero PK/DDL/sequence traps, full extension parity, deterministic
RPO = 0inside metro AZs. - Choose Tier 3 (Native Logical) for cross-major-version upgrades, OS/glibc transitions, or filtered data feeds—never as an automated HA standby without scripted DDL and sequence sync.
- Choose Tier 4 (
pgEdge Spock 5/EDB PGD) only when multi-region local write latency (PA/EL) is mandatory and counters use CRDTs (spock.delta_apply). - Choose Tier 5 (
YugabyteDB,CockroachDB,Citus) only when globalRPO = 0ACID or horizontal multi-terabyte write sharding justifies WAN latency and40001retry engineering.
6. PostgreSQL 18.6 Impact Across Tiers & Ecosystem Vitality¶
- Tiers 1-4 Under PG 18.6 (Ch. 12): Tested live across
87benchmark rows (results/benchmark_pg18.csv) and5 x 26probes (results/functional_limits_pg18.csv), PG 18.6 addsdata_checksums=onandio_method=worker. It addssubstream=p,pubgencols=s, 7confl_*counters, anduuidv7()(pg18-birepH2,066.70 TPS,3.30x,pg18-logical3,518.49,+85.1%). - Unchanged Tier Boundaries:
FOR ALL SEQUENCES/REFRESH SEQUENCESstill raiseSQLSTATE 42601in PG 18.6 (PG 19 target). Tier 3 cutovers still requiresetval(), Tier 4 overlapping writes require Spock/PGD CRDTs, and globalRPO=0ACID stays Tier 5. - Vitality & Charts: Cross-reference ecosystem bus-factor and 12m commit velocity in Ch. 11 and
chart_08/chart_09/chart_10.