liveBook · Manning

Replication

As explained in PostgreSQL Mistakes and How to Avoid Them

Database replication is the process of copying and maintaining database data across multiple database instances.

Replication copies database changes to one or more other PostgreSQL instances so standby nodes can support redundancy and failover. PostgreSQL replication began with log shipping: continuous archiving copied WAL files to another server, producing a warm standby ready to take over. A single server without failover provision creates an avoidable downtime risk; standby nodes are the basic redundancy measure. section 9.5 section 9.7

Streaming replication changes the trade-off. Since PostgreSQL 9.0, WAL can stream automatically and asynchronously to a standby, keeping it closer to the primary than whole-file copying. Such a standby can serve read-only queries as a hot standby. Synchronous replication, introduced in PostgreSQL 9.1, waits for the WAL round trip so committed transactions are present on both sides; it removes loss caused by replication lag, but adds commit latency. section 9.5

Choose and operate replication with its failure modes in mind:

  • Use cascading replication when a standby relays streaming changes to another standby, rather than requiring every standby to stream directly from the primary. section 9.5
  • Do not substitute manual failover or custom scripts for established HA management: they risk prolonged downtime, divergence, or data loss. RepMgr, Patroni, and CloudNativePG are named alternatives. section 9.7
  • Treat long-running transactions as a lag risk for synchronous or logical replication, because their changes replicate only after commit. Asynchronous streaming can be selected by setting synchronous_standby_names to an empty string. section 6.5.2

Read workload conflicting with WAL replay

A heavy update on the primary can conflict with a simultaneous long-running SELECT on a standby. The standby must apply WAL that affects the rows being read, so PostgreSQL can cancel the standby query. This makes a hot standby useful for reads, but not a guarantee that every long analytical query can finish while primary-side updates continue. section 6.5.2

Figure 9.3 A sample PostgreSQL HA setup with a cascaded replication for redundancy and backup
figure

Inspect a topology in which cascaded replication supplies redundancy and supports backup planning. section 9.5

How PostgreSQL Mistakes and How to Avoid Them covers it

How deep it goes
Explains WAL log shipping, streaming, hot and warm standby, synchronous commits, cascading, and query-conflict consequences.
Examples
Uses WAL shipping and streaming standbys, a cascaded HA topology, and a primary update conflicting with a standby SELECT.
What you can do after
Operate/configure standby replication while weighing synchronous durability against latency and diagnosing lag or standby-query conflicts.
How it connects
Connects replication with high availability, WAL, hot standby, failover, RPO, RTO, logical replication, and autovacuum.

Also explained in

Compare how 13 books explain replication →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of PostgreSQL Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.