Home / Databases / PostgreSQL

DatabasesRunbookAdvanced

Sev1: PostgreSQL commits hang after the synchronous standby goes down. Writes stall on SyncRep

Every write transaction hangs at COMMIT because the synchronous standby is unreachable. Decide on durability, release the waiting commits safely, and restore synchronous replication.

Deepak Bhardwaj6 min read

Applies toPostgreSQL 13-18 with streaming replication and synchronous_standby_names; Patroni-managed clusters; self-managed on VMs, EC2 or Azure VMs

Symptoms

  • Application writes time out. Reads are fine. INSERT, UPDATE and DELETE statements finish, but COMMIT never returns.
  • Connection pools fill up with sessions that wait on commit, which leads to "too many connections" errors.
  • pg_stat_activity shows wait_event = 'SyncRep' on many backends.
  • The standby host, its network link or its PostgreSQL service went down just before the hang.

The primary is behaving as designed. With synchronous_commit = on (the default) and a non-empty synchronous_standby_names, each commit waits until a synchronous standby confirms it has the WAL. If no listed standby is connected, commits wait forever.

1. Confirm scope and impact

Count the sessions stuck on synchronous replication:

SELECT count(*) AS waiting, min(now() - xact_start) AS oldest_wait_from
FROM pg_stat_activity
WHERE wait_event = 'SyncRep';

Check which standbys the primary can see and their sync state. Values are async, potential, sync or quorum.

SELECT application_name, client_addr, state, sync_state,
       write_lag, flush_lag, replay_lag
FROM pg_stat_replication;

SHOW synchronous_standby_names;
SHOW synchronous_commit;

If pg_stat_replication returns no row with sync_state of sync or quorum, and synchronous_standby_names is set, you have found the cause.

2. Triage the standby quickly

Spend no more than 5 minutes on this before you make the durability decision in step 3.

# On the standby host
systemctl status postgresql          # unit name varies by distro and version, e.g. postgresql-16
sudo -u postgres psql -c "SELECT pg_is_in_recovery(), status, sender_host FROM pg_stat_wal_receiver;"
tail -n 50 /var/lib/pgsql/16/data/log/postgresql-*.log   # path varies by install
Finding Likely cause Action
Service down, disk full on the standby Standby crashed Free space and start it. Commits resume when it reconnects
requested WAL segment ... has already been removed Standby fell too far behind Standby needs a rebuild. Go to step 3 now
Network or firewall change Standby cannot reach port 5432 Revert the change, or go to step 3
Standby is up but application_name does not match Name in primary_conninfo differs from synchronous_standby_names Fix the name and reload the standby

If the standby comes back within a few minutes, the waiting commits complete on their own and nothing else is needed.

3. Release the stalled commits

This is a business decision: availability now, or a guarantee that every commit is on two servers. Get a quick yes from the service owner and record it in the incident channel.

Caution

When synchronous replication is off, a commit is acknowledged once it is on the primary only. If the primary is lost before a standby catches up, recently acknowledged transactions are lost. Keep the window short and restore synchronous mode as soon as a healthy standby is connected.

Plain streaming replication (no Patroni)

Clear the synchronous standby list and reload. Backends that were waiting are released straight away, and no restart is needed.

ALTER SYSTEM SET synchronous_standby_names = '';
SELECT pg_reload_conf();

Check that the waits have cleared:

SELECT count(*) FROM pg_stat_activity WHERE wait_event = 'SyncRep';

If another healthy standby exists, list it instead of disabling synchronous replication completely. A quorum list tolerates the loss of one member:

ALTER SYSTEM SET synchronous_standby_names = 'ANY 1 (standby_b, standby_c)';
SELECT pg_reload_conf();

Patroni-managed clusters

Patroni writes synchronous_standby_names itself, so do not edit it with ALTER SYSTEM. Change the cluster configuration instead:

patronictl -c /etc/patroni/patroni.yml show-config
patronictl -c /etc/patroni/patroni.yml edit-config -s synchronous_mode=false --force

If synchronous_mode_strict: true is set, Patroni blocks writes on purpose when no synchronous standby is available. That setting was a deliberate choice for zero data loss. Confirm with the owner before you turn it off.

Do not just cancel sessions

Cancelling a backend that is waiting on SyncRep does not roll back its transaction. The transaction has already committed locally, and PostgreSQL warns: canceling wait for synchronous replication due to user request and The transaction has already committed locally, but might not have been replicated to the standby. The application sees an error for a write that actually succeeded, which leads to duplicate retries. Change the configuration instead.

4. Verify recovery

  • SyncRep waits are at zero, and application write latency is back to normal.
  • Connection pool usage is falling. You may need to restart pools that marked connections as broken.
  • Check the WAL the lost standby still needs. Either keep enough WAL for it, or plan to rebuild it:
SELECT slot_name, active, pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)) AS retained
FROM pg_replication_slots;

Warning

An inactive replication slot for the dead standby keeps WAL on the primary forever and will eventually fill pg_wal. Drop the slot (SELECT pg_drop_replication_slot('standby_a');) if you are going to rebuild the standby.

5. Restore synchronous replication

Rebuild or restart the standby, wait until state = 'streaming' and the lag is near zero, then put the setting back:

ALTER SYSTEM SET synchronous_standby_names = 'ANY 1 (standby_a, standby_b)';
SELECT pg_reload_conf();
SELECT application_name, sync_state FROM pg_stat_replication;

With Patroni, run patronictl edit-config -s synchronous_mode=true --force, then use patronictl list to confirm a member shows as Sync Standby.

Communicate

  • During the incident: "Writes to are stalled. Reads are working. We are restoring write availability. ETA 15 minutes."
  • After the fix: state the window when commits were acknowledged without synchronous replication, so the risk is on record.

Prevent it next time

  • Never run with a single synchronous standby when availability matters. Use ANY 1 (a, b) with at least two standbys, or Patroni synchronous_mode with synchronous_node_count sized correctly.
  • Alert when pg_stat_replication has fewer synchronous or quorum standbys than required, and when SyncRep waits exceed a few seconds.
  • For low-value writes, applications can set SET LOCAL synchronous_commit = local per transaction to opt out of the wait.
  • Document who can approve turning synchronous replication off, and how long it may stay off.
  • Test this runbook in a game day: stop the standby and time how long it takes to restore writes.