Sub topic 10
Synchronous replication: `synchronous_standby_names` and `synchronous_commit`
Assume you know basic PostgreSQL administration, can edit server configuration, and understand that a transaction becomes durable when its commit record is safely written. This chapter builds on physical streaming replication and assumes a primary already streams WAL to at least one standby.
Asynchronous replication lets the primary confirm a commit before a standby receives it. This gives low write latency, but a failure can lose recently committed transactions. Synchronous replication makes the primary wait for acknowledgment from a selected standby before confirming the commit to the client.
Choosing synchronous standbys
The primary identifies standbys by their application_name value in primary_conninfo. The synchronous_standby_names setting defines which connected standbys must acknowledge commits.
# postgresql.conf on the primary
synchronous_standby_names = 'FIRST 1 (standby1, standby2)'
synchronous_commit = onFIRST 1 means that one standby is required. PostgreSQL chooses the first eligible standby in the listed priority order. If standby1 is connected, it normally serves as the synchronous standby. If it disconnects, standby2 can take over.
You can also use quorum syntax:
synchronous_standby_names = 'ANY 2 (standby1, standby2, standby3)'Here, any two listed standbys can acknowledge a commit. Quorum configurations can tolerate one standby failure while still preserving the required number of copies.
The names must match the standbys' application_name values. A name mismatch means the standby does not satisfy the synchronous requirement.
flowchart LR
C[Client commits] --> P[Primary writes WAL]
P --> S1[standby1]
P --> S2[standby2]
S1 --> A1[Acknowledgment]
S2 --> A2[Acknowledgment]
A1 --> P
A2 --> P
P --> R[Primary confirms commit]Controlling what the commit waits for
synchronous_commit controls the durability point required before the primary reports success.
| Setting | Primary waits for |
|---|---|
| `on` | Local WAL flush and synchronous standby WAL flush |
| `remote_write` | Standby writes WAL to its operating system |
| `remote_apply` | Standby flushes and replays the WAL |
| `local` | Local WAL flush only |
| `off` | Local WAL write is delayed; the client may receive success first |
For most synchronous replication, use on. It protects against losing a committed transaction when the primary fails, provided the synchronous standby remains durable.
Use remote_apply when a successful commit must already be visible to queries on the standby. This adds replay time to commit latency. remote_write can reduce latency, but an operating-system or standby failure before a durable flush can still lose the transaction.
local and off bypass the remote durability guarantee for sessions or transactions that use them. A session can override the setting:
SET synchronous_commit = 'remote_apply';
BEGIN;
UPDATE accounts SET balance = balance - 50 WHERE account_id = 10;
COMMIT;Failure modes and trade-offs
Synchronous replication improves durability by adding network and standby storage latency to every protected commit. If no suitable synchronous standby is available, commits can wait indefinitely. This is safer than silently confirming unreplicated data, but it can make an outage look like a frozen application.
Monitor connection state and commit latency. On the primary, verify the selected standby:
SELECT application_name, state, sync_state,
sent_lsn, flush_lsn, replay_lsn
FROM pg_stat_replication;Keep at least one standby in a different failure domain when possible. Synchronous replication cannot protect against a failure that affects both the primary and its synchronous standby.
Exercise
With synchronous_standby_names = 'FIRST 1 (standby1, standby2)', what happens if standby1 disconnects while standby2 remains connected?
Show answer
PostgreSQL can select standby2 as the synchronous standby. Protected commits continue after standby2 is ready to acknowledge them, although commit latency and availability may change.