Overview

PostgreSQL replicates by shipping its WAL (pg-wal-checkpoints). Physical (streaming) replication copies the whole cluster byte for byte to read-only standbys. Logical replication publishes row changes of selected tables to other databases. Replication gives read scaling and a failover target; it is not a backup (pg-backup-pitr).

Core concepts

  • Streaming replication: a standby connects with the replication protocol and receives WAL continuously. The standby replays it and, with hot_standby = on (default), serves read-only queries.
  • Asynchronous (default): commit does not wait for the standby; a failover can lose the most recent commits. Synchronous: synchronous_standby_names makes commit wait for the listed standbys, trading latency and availability for zero data loss.
  • Replication slots make the primary keep WAL until the consumer has it. Safe against lag, dangerous if a consumer disappears (disk fills).
  • Cascading replication: a standby can feed other standbys.
  • Logical replication: CREATE PUBLICATION on the source, CREATE SUBSCRIPTION on the target; needs wal_level = logical. It works across major versions and subsets of tables, which makes it the standard tool for zero-downtime major upgrades. DDL and sequences are not replicated; tables need a replica identity (primary key).
  • Failover vs switchover: switchover is planned and clean; failover promotes a standby after the primary is lost. pg_ctl promote or SELECT pg_promote() promotes. The old primary must be rebuilt or rewound (pg_rewind) before it can rejoin as a standby.
  • Split brain is two nodes both accepting writes. Use fencing and a consensus-based manager (Patroni with etcd/Consul, or a Kubernetes operator such as CloudNativePG) rather than hand-written scripts.

Worked example

Setting up a streaming standby (commands from the manual; not executed here):

# primary: postgresql.conf has wal_level=replica, max_wal_senders>=3; pg_hba.conf allows the replica user
pg_basebackup -h primary -U replicator -D /var/lib/postgresql/17/main -R -X stream -C -S standby1
# -R writes standby.signal and primary_conninfo; -C -S creates the physical slot

Monitoring lag on the primary:

SELECT application_name, state, sync_state,
       pg_wal_lsn_diff(pg_current_wal_lsn(), replay_lsn) AS replay_lag_bytes,
       replay_lag
FROM pg_stat_replication;

On a standby: SELECT now() - pg_last_xact_replay_timestamp(); approximates time lag.

Pitfalls and production notes

  • A lagging or dead slot keeps WAL on the primary forever; set max_slot_wal_keep_size.
  • Queries on a standby can conflict with replay of cleanup records; the standby then cancels them, or with hot_standby_feedback = on it asks the primary to retain old rows (bloat on the primary, see pg-vacuum-bloat).
  • Test failover regularly, including application reconnection and DNS or virtual IP handover.
  • Applications should send reads to replicas only when stale reads are acceptable (replication lag is not zero).
  • Logical replication does not copy sequences or schema changes; keep schema migrations in step (safe-schema-migrations). PostgreSQL 17 can carry logical slots across a failover with slot synchronisation.
  • Same-major-version only for physical replication; use logical replication or pg_upgrade to change versions.

Practice questions

  1. What does a synchronous standby cost you, and what happens if it goes away?
  2. Why is replication not a substitute for backups?
  3. Outline a zero-downtime major upgrade with logical replication.

References

pg-wal-checkpoints · pg-backup-pitr · pg-monitoring · pg-connection-pooling