Sub topic 10
Synchronous replication: synchronous_standby_names and synchronous_commit
This chapter assumes you have a working primary and at least one hot standby streaming from it, as built in the earlier chapters. You should also know how to read pg_stat_replication and reload the server configuration.
Why asynchronous replication can lose commits
By default, streaming replication is asynchronous. The primary flushes a commit record to its own WAL, tells the client the commit succeeded, and sends the WAL to standbys independently. If the primary's disk is lost before the standby receives the last records, a promoted standby is missing transactions that clients were told were committed.
Synchronous replication closes that gap. The primary delays the commit acknowledgement until one or more standbys confirm that they have the commit's WAL. The cost is latency: every commit now includes a network round trip, plus a disk flush on the standby, depending on the level you choose.
Two settings control this behavior:
synchronous_standby_namesdecides which standbys count as synchronous and how many must confirm.synchronous_commitdecides how far the WAL must progress on those standbys before the commit returns.
If synchronous_standby_names is empty, replication is asynchronous no matter what synchronous_commit says.
What happens at commit
The backend that runs the commit sleeps until the standby's reported LSN passes the commit record's LSN. Other sessions keep working. Only the sessions waiting on a commit are held back.
Configuring synchronous_standby_names
Each name in the list is matched against the application_name of the standby's replication connection. The standby sets it in primary_conninfo, as in the earlier chapter (application_name=standby1). If you do not set it, the walreceiver uses cluster_name when that is set, and otherwise the string walreceiver. Check what the primary sees:
SELECT application_name, state, sync_state
FROM pg_stat_replication;The setting accepts three forms:
| Form | Meaning |
|---|---|
| `FIRST 1 (standby1, standby2)` | Priority-based. Commits wait for the first listed standbys that are connected and streaming. Earlier names have higher priority. |
| `ANY 1 (standby1, standby2, standby3)` | Quorum-based. Commits wait for any 1 of the listed standbys to confirm. Which one does not matter. |
| `standby1, standby2` | The same as `FIRST 1 (standby1, standby2)`. |
A plain number works too: 2 (s1, s2, s3) is the same as FIRST 2 (s1, s2, s3). The name * matches any standby.
Here is a priority-based setup, applied without a restart:
ALTER SYSTEM SET synchronous_standby_names = 'FIRST 1 (standby1, standby2)';
ALTER SYSTEM SET synchronous_commit = 'on';
SELECT pg_reload_conf();With FIRST 1, standby1 is synchronous while it is streaming. If it disconnects, standby2 takes over. Once standby1 returns, it takes the role back. The sync_state column in pg_stat_replication shows who holds which role:
| sync_state | Meaning |
|---|---|
| `async` | Not in the list. Commits never wait for it. |
| `potential` | In a `FIRST` list but outranked. It takes over if a higher-priority standby fails. |
| `sync` | Currently chosen. Commits wait for it. |
| `quorum` | A candidate in an `ANY` list. Any of the candidates can satisfy a commit. |
A standby counts as synchronous only while it is connected and streaming. During its initial catch-up, which comes before it reaches the streaming state, it does not hold commits.
Choosing FIRST or ANY
Use FIRST when you want to know which standby has every acknowledged commit, for example so that you can always fail over to it. Use ANY when you want to tolerate the loss of a standby without a pause. ANY 1 (a, b) keeps committing at the speed of the faster standby. It also keeps committing if either standby is down.
With ANY, any single standby may be missing some commits that another one has. After a failure, promote the standby with the highest replay position. Compare pg_last_wal_receive_lsn() on each candidate before you choose.
What synchronous_commit controls
synchronous_commit sets how far the commit's WAL must get before the primary acknowledges the client:
| Value | The commit returns after | Survives |
|---|---|---|
| `off` | Nothing is flushed. The WAL writer flushes shortly afterward. | Nothing guaranteed. A primary crash can lose recent commits, but the database stays consistent. |
| `local` | The primary's own WAL flush. | Primary restart. Not loss of the primary's disk. |
| `remote_write` | The standby has received the WAL and written it to the OS, but not flushed it. | Primary failure. Not a simultaneous OS crash on the standby. |
| `on` (default) | The standby has flushed the WAL to disk. | Primary failure and a standby crash. |
| `remote_apply` | The standby has replayed the WAL. | The same as `on`, and queries on the standby see the commit. |
Two points are easy to miss.
- The default
onmeans "local flush" when no synchronous standbys are configured, and "standby flush" when they are. You do not need to changesynchronous_committo turn synchronous replication on. - Only
remote_applygives you read-your-writes consistency on a standby. Withon, the WAL is safe on the standby, but replay can still lag, so a query sent to the standby right after the commit may not see the row.
Choosing the level per transaction
synchronous_commit can be set per session or per transaction. This lets you keep strict durability for important data and skip the wait for low-value writes:
BEGIN;
SET LOCAL synchronous_commit = 'local';
INSERT INTO page_views (url, seen_at) VALUES ('/home', now());
COMMIT;You can also put a stricter level on a single role or database with ALTER ROLE ... SET synchronous_commit = 'remote_apply'. A transaction that runs at local is still replicated. It simply does not make the client wait.
Failure modes
The main risk is availability. With synchronous_standby_names = 'standby1' and no other candidate, a standby outage stops every commit that needs a synchronous standby. Writing sessions hang, and reads continue. PostgreSQL does not fall back to asynchronous mode on its own, because that would silently remove the durability guarantee.
You have three ways to handle this:
- List at least two candidates, for example
ANY 1 (standby1, standby2), so that one failure does not block commits. - Monitor
sync_stateand alert when no standby issyncorquorum. - In an emergency, clear
synchronous_standby_namesand reload. The sleeping backends wake up and their commits complete. You have now chosen availability over durability, so record that decision.
A client can also cancel a waiting commit. PostgreSQL then issues a warning that the transaction was committed locally but may not have replicated. The data is committed on the primary and will survive a restart. Treat the result as "committed, not yet confirmed".
Other effects to expect:
- Commit latency now includes the network round trip and, at
on, the standby's disk flush. Slow standby storage slows every writer. - Long-running transactions are unaffected until they commit, but a short write-heavy workload pays the cost on every commit. Batch rows into fewer transactions where you can.
- A synchronous standby is still subject to query conflicts. A conflict that delays replay does not delay commits at
on, but it does atremote_apply. - Synchronous replication protects against losing acknowledged commits. It does not make failover automatic. You still need promotion tooling, as covered in the failover chapter.
Exercise
A primary has synchronousstandbynames = 'ANY 1 (a, b, c)' and synchronouscommit = remoteapply. Standby a is slow to replay because of a long query conflict, and b and c are healthy. Do commits slow down, and why?
Show answer
Commits do not slow down. ANY 1 is satisfied by the first standby to confirm, and at remote_apply that means the first to replay the commit. Standby b or c answers quickly, so the commit returns without waiting for a. Only if all three were slow, or only a were listed, would the commit wait.
Exercise
An application inserts a row and then immediately reads it from a standby that is configured as synchronous with synchronous_commit = on. The row is sometimes missing. Why, and which setting fixes it?
Show answer
At on, the commit returns once the standby has flushed the WAL, not once it has replayed it. A read that arrives before replay finishes does not see the row. Setting synchronouscommit = remoteapply for those transactions makes the commit wait until the standby has replayed it, so the read sees the row.
Exercise
The only listed synchronous standby loses its network connection and application sessions start hanging on COMMIT. Name one immediate action that restores writes and the trade-off it makes.
Show answer
Clear synchronousstandbynames (for example with ALTER SYSTEM RESET and pgreloadconf()). Waiting commits then complete. The trade-off is that commits are no longer guaranteed to exist on any standby, so a primary failure could lose them until you restore a synchronous standby.