Replication & High Availability Basics

PostgreSQL

Chapter 11 · Replication & High Availability Basics

This chapter covers Postgres's own two genuinely distinct replication mechanisms, and closes with an honest correction of a common misconception about how Postgres's own replication compares to MySQL's.

Why Replication Matters

Three real, distinct motivations: high availability (a standby ready to take over if the primary fails), read scaling (offloading read-only queries to replicas), and disaster recovery or geographic distribution.

Streaming Replication (Physical Replication)

Every change to a Postgres database is first written to the WAL (Write-Ahead Log) before being applied to the actual data files — a durability mechanism this course hasn't named explicitly until now, though it underlies crash recovery generally. Streaming replication works by continuously shipping WAL records from a primary server to one or more standby servers, which replay those records to stay in sync.

This is "physical" replication — a byte-for-byte, block-level copy of the entire database cluster. A replica built this way is an exact physical copy: it can't have a different schema, can't replicate only a subset of tables, and in the classic setup is read-only.

A genuine, concrete trade-off: asynchronous replication (the default) is faster, but leaves a small window where data could be lost if the primary fails before a replica catches up; synchronous replication means a commit doesn't complete until at least one replica confirms receipt — zero data loss, at the cost of real added latency on every write.

Logical Replication

Logical replication (Postgres 10+) is a genuinely different, newer mechanism: it replicates actual row-level changes via a publish/subscribe model (CREATE PUBLICATION / CREATE SUBSCRIPTION), rather than raw physical WAL bytes. This unlocks real capability physical replication can't offer: replicating just a subset of tables rather than the whole cluster, replicating between different major Postgres versions (useful for near-zero-downtime major version upgrades), and — critically — the subscriber remains a genuinely independent, writable database that simply happens to receive a stream of changes for the tables being replicated. This is a real, meaningfully different tool, not the same mechanism with different configuration.

Contrasted With MySQL's Own Replication Model

MySQL's own replication has historically centered on binlog-based statement or row-based replication — conceptually much closer to Postgres's own logical replication in spirit, since both work at a row/statement level rather than raw physical bytes. MySQL never really had a direct equivalent to Postgres's own byte-for-byte physical streaming replication in the same way.

It's worth correcting a common misconception directly: it's not accurate to say "Postgres invented modern replication and MySQL is behind." MySQL has had working, production-grade replication for a very long time, including modern Group Replication and InnoDB Cluster for genuine multi-primary high availability. The real difference isn't "does it work" — it's architectural: Postgres offers both a physical (WAL-streaming) and a logical (row-level) replication mechanism as two genuinely distinct tools for different jobs, while MySQL's own replication has centered more consistently on the logical/row-level style throughout its history.

A Basic HA Pattern

A common real setup: one primary plus one or more streaming replicas, with a tool like Patroni or repmgr handling automatic failover — promoting a replica to primary if the original fails. It's worth being honest that Postgres itself doesn't include automatic failover out of the box: a raw streaming replica setup requires manual promotion (pg_promote()) unless a separate HA-management tool is layered on top.

Replication is not a backup
This echoes a principle already established for two other systems on this site — dbsec1's own material and mongodb2-5's own replication chapter make the identical point. A mistake or an accidental deletion on the primary replicates to every standby just as faithfully as a legitimate change does — replication protects against hardware or server failure, not against data corruption or accidental deletion. Real backups remain a separate, necessary practice no replication setup replaces.
The WAL closes a small implicit gap
The WAL — the same underlying durability mechanism introduced here — is what makes Postgres's crash recovery reliable in the first place, connecting this chapter's replication material back to postgres1-9's own MVCC/VACUUM chapter: durability and concurrency both rest on the same underlying write-ahead logging foundation.

Hands-On Exercises

Exercise 1

Explain the difference between physical (streaming) and logical replication in Postgres, and describe one concrete scenario where logical replication's own subset/cross-version capability is specifically needed and physical replication can't do it.

📄 View solution
Exercise 2

Explain the honest correction this chapter makes about MySQL's own replication history — what's the real architectural difference, and why is "Postgres invented modern replication" a misconception?

📄 View solution
Exercise 3

Using this chapter's own warn-box, explain why replication is not a substitute for backups, tying your answer to the specific mechanism by which a destructive change propagates to every replica.

📄 View solution

Chapter 11 Quick Reference

  • Streaming (physical) replication — byte-for-byte, whole-cluster, ships raw WAL records; async (default, faster) vs. sync (zero data loss, more latency)
  • Logical replication — row-level, publish/subscribe, can replicate a subset of tables and across major versions; subscriber stays independently writable
  • MySQL's own replication has historically been closer to "logical" in spirit — not "behind," architecturally different
  • Postgres offers both physical and logical mechanisms as genuinely distinct tools; MySQL centers on the logical/row-level style
  • No automatic failover out of the box — Patroni/repmgr or manual pg_promote() required
  • Replication ≠ backup — a destructive change replicates just as faithfully as a legitimate one (echoes dbsec1/mongodb2-5)
  • The WAL is the same durability mechanism underlying postgres1-9's own crash-recovery reliability
  • Next chapter: Capstone — Migrating and Extending a MySQL Database in PostgreSQL