AI Hints
Home›PostgreSQL Tutorial›Backups and Replication
PostgreSQL · Lesson 96 of 120

Replication

Copies that stay in sync.

⏱ 2 min read▶ Video at 1:26:35🎓 Free
Watch this lesson in the PostgreSQL courseWatch on YouTube ↗Full PostgreSQL course

One server is a single point of failure. Replication streams every change to other servers, called replicas or standbys. They can take over if the primary fails, and serve read queries.

  • One server = risk
  • Stream changes
  • Standbys

Streaming replication

The primary writes every change to its write ahead log. It streams that log to the standbys. Standbys replay it, and stay a moment behind. If the primary fails, a standby is promoted.

  1. Primarywrites WAL
  2. StreamWAL to standbys
  3. Standbyreplays, read-only
  4. Failoverpromote standby

Physical vs logical

Physical replication copies the whole server, byte for byte, and is used for high availability. Logical replication copies chosen tables, as row changes, and can even go between different major versions.

Physical

  • Whole serverByte for byteHigh availability

Logical

  • Chosen tablesRow changesUpgrades, sharing

Summary

Replication streams changes to standbys, for failover and read scaling. Next, LISTEN and NOTIFY.

Key takeaways
  • WAL
  • Standbys
  • Failover
Next up: LISTEN and NOTIFY — messages from the database