Re: Reduce SyncRepLock contention on the commit path

From: Ivan Alekseev <nettrash(at)nettrash(dot)me>
To: Vadim Ponomarev <vbponomarev(at)gmail(dot)com>
Cc: Yura Sokolov <y(dot)sokolov(at)postgrespro(dot)ru>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Reduce SyncRepLock contention on the commit path
Date: 2026-12-05 07:33:25
Message-ID: 06E1502D-F028-4177-A780-2A94739FC1A3@nettrash.me
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

I reviewed v6 on top of master (cb94cb4124c) and tested it with a
primary and a synchronous standby. Comments per patch.

0001 (wake after the lock is down): looks right to me. The unlink, the
barrier and the state store stay under the lock, so a waiter can't see
itself completed while still queued. On the list size: SyncRepWaitForLSN()
is called only from xact.c and twophase.c, and the processes that commit
there (backends, autovacuum and background workers, walsenders) are all
counted in MaxBackends, so that bounds the number of queued procs.

0002 (positions computed before the lock): I agree the guarantee isn't
needed. lsn[] only moves forward, so stale positions release nobody, and
the per-walsender priority and positions are still read under each
walsender's spinlock.

0003 (one release per drain): to answer the open question from your
last mail, yes, I think one exit path is missing: a FATAL raised inside
the drain. ereport(FATAL) goes straight to proc_exit(), so
WalSndErrorCleanup() doesn't run, and the walsender has only
on_shmem_exit(WalSndKill) registered, which doesn't flush the pending
release.

The concrete case is pg_terminate_backend() (or any SIGTERM) on a
walsender. When the next message in the drain is only partly in the
buffer, pq_getmessage() blocks in pq_recvbuf(), and with DoingCommandRead
false ProcessClientReadInterrupt(true) handles ProcDiePending with
CHECK_FOR_INTERRUPTS(), which raises FATAL. Any other CHECK_FOR_INTERRUPTS()
reached before the drain ends would do the same.

I could reproduce it with the walsender-reply-drained injection point from
the series. Setup: one synchronous standby, synchronous_commit = on, the
point attached with 'wait'. I let the walsender process replies until it
was held right after a reply whose flush position covered a waiting
commit; pg_stat_replication.flush_lsn confirmed that. Then I made
pg_hba.conf reject replication connections, so the standby couldn't come
back, and terminated the walsender. The commit was still waiting for
SyncRep 10 seconds later, although the standby had acknowledged it. It
returned only after I allowed reconnects again and a new walsender took
over (5 s, the standby's wal_retrieve_retry_interval). As a control, waking
the point instead of terminating returned the commit immediately. On
master the release runs inside ProcessStandbyReplyMessage(), so there is
no such window.

The acknowledgement is real, so it's a liveness problem rather than a
durability one, but the commit can wait for as long as the standby stays
away. A possible fix is to run the pending release from an exit callback
as well, e.g. a before_shmem_exit() callback registered with the
walsender, or at the top of WalSndKill() while MyWalSnd is still set.
Either way it would need LWLockReleaseAll() first, as
WalSndErrorCleanup() does, in case the FATAL came while holding
SyncRepLock. With that in place, the explicit flushes before proc_exit()
in ProcessRepliesIfAny() could probably go too.

0004 (atomic watermark fast path): looks right to me. The watermark only
moves forward, so a stale read can only send a committer to the slow
path. I only tested on arm64, where the read is a plain load, so I can't
say anything about the platforms the commit message mentions.

0005: the new 027_stream_regress_sync.pl is skipped in CI, since the CI's
PG_TEST_EXTRA doesn't include regress_sync_rep. The green cfbot run
doesn't cover it.

Benchmark: an Apple M4 Max laptop, the primary and the standby on the same
machine, -O2 builds without assertions, pgbench -M prepared, scale 50,
64 clients for 30 s, two rounds each. Throughput varied too much between
rounds to compare (master 18.1k and 9.1k tps, patched 11.4k and 33.0k
tps), so I don't draw any conclusion from it. The wait events were
consistent, though. Sampling pg_stat_activity 250 times during each
64-client run:

LWLock. SyncRep IPC SyncRep
master round 1 172 3179
master round 2 115 3020
v6 round 1 1 2900
v6 round 2 3 2633

So the queueing on SyncRepLock is essentially gone, and what's left is the
wait for the standby itself. A machine with a separate standby would give
better throughput numbers than mine.

I haven't run the TAP tests locally. I'll set the entry to Waiting on
Author for the exit path in 0003.

Regards,
Ivan Alekseev

> On 10 Oct 2026, at 12:02, Vadim Ponomarev <vbponomarev(at)gmail(dot)com> wrote:
>
> v6 attached, rebased onto f58ab18a904.
>
> cfbot flagged v5 as needing a rebase: master took 058 and 059, and the
> new test sat at the tail of src/test/recovery/meson.build, where every
> new recovery test lands. 0003's two scenarios now live at the end of
> 007_sync_rep.pl, so 0003 no longer touches meson.build. On Windows 007
> stops before them and runs its own tests as before, the way
> 019_replslot_limit.pl does.
>
> Nothing else changed.
>
> Regards,
> Vadim Ponomarev
> <v6-0001-Wake-the-released-sync-rep-waiters-after-the-queu.patch><v6-0002-Compute-the-synced-positions-before-taking-the-sy.patch><v6-0003-Release-the-sync-rep-waiters-once-per-drained-bat.patch><v6-0004-Let-a-committer-whose-acknowledgement-already-arr.patch><v6-0005-Add-stream-regress-test-for-synchronous-replicati.patch>

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Richard Guo 2026-12-05 08:40:33 FULL JOIN planning failure with virtual generated columns
Previous Message Manu 2026-12-05 06:58:55 [PATCH] Enumerate schema members through pg_depend