Re: [PATCH] Allow NOTIFY without cross-transaction commit ordering

From: Ivan Alekseev <nettrash(at)nettrash(dot)me>
To: "pgsql-hackers(at)lists(dot)postgresql(dot)org" <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Cc: Joel Jacobson <joel(at)compiler(dot)org>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Arseniy Mukhin <arseniy(dot)mukhin(dot)dev(at)gmail(dot)com>, Rishu Bagga <rishu(dot)postgres(at)gmail(dot)com>
Subject: Re: [PATCH] Allow NOTIFY without cross-transaction commit ordering
Date: 2026-12-04 09:22:15
Message-ID: 21E827F8-5F06-4333-BC4F-645D0ABAB9B5@nettrash.me
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

Attached is v2. The only change is in the new isolation test in 0003,
src/test/modules/injection_points/specs/notify_unordered.spec. On the
slower GitHub CI machines, the step that followed the injection point
wakeup could start before the woken session had finished committing, so
step completions were sometimes reported in a different order than the
expected output. The spec now runs an empty step in the woken session
right after the wakeup, which can't start until that session is done, so
the next step can't start early either. Where both sessions send ordered
notifications, a marker also keeps their completions in order. The test
still fails, as intended, with 0002 reverted.

0001 and 0002 are unchanged, and there are no code changes in 0003. The
series is rebased on current master and passes the full CI on my fork
(Linux meson 32/64-bit and autoconf, macOS, Windows, MinGW, compiler
warnings):
https://github.com/nettrash/postgres/actions/runs/47296608252

I've added the patch to the PG20-4 commitfest:
https://commitfest.postgresql.org/patch/7793/

Regards,
Ivan Alekseev

> On 25 Nov 2026, at 16:44, Ivan Alekseev <nettrash(at)nettrash(dot)me> wrote:
>
> Hi,
>
> PreCommit_Notify() takes a cluster-wide heavyweight lock that a
> notifying transaction holds until its commit is complete, including
> the WAL flush and any synchronous replication wait. So notifying
> transactions commit one at a time across the whole cluster: their
> commit rate is capped at about 1/(commit latency), whatever the number
> of clients, and their latency grows with the number of concurrent
> notifiers. This has come up before [1][2]. The v19 work in 282b1cde9
> [3] made wakeups much cheaper, but left this lock alone.
>
> The lock is what provides the documented guarantee that notifications
> from different transactions arrive in commit order. Many applications
> don't need that: they use NOTIFY as a doorbell ("something changed, go
> and look") and read the actual state from tables. The attached series
> lets them say so, per notification:
>
> SELECT pg_notify(channel, payload, ordered => false);
>
> A transaction whose notifications were all sent that way doesn't take
> the heavyweight lock. Nothing else changes: notifications are still
> sent only on commit and arrive in send order within their transaction,
> NOTIFY and the two-argument pg_notify() are unchanged, and a
> transaction that sends both kinds stays ordered. Unlike the GUC in
> [1], this is opt-in per notification, so code that relies on ordering
> is unaffected. Unlike [2], it doesn't change how notifications are
> stored or WAL-logged.
>
> The patches:
>
> 0001 holds NotifyQueueLock across the whole of a transaction's queue
> insertion instead of releasing it between pages. The direct
> advancement in SignalBackends() assumes that nobody writes between the
> queue head positions recorded before and after the insertion. That is
> currently guaranteed only by the heavyweight lock, and this makes it
> independent of it. No behavior change.
>
> 0002 fixes a lost wakeup that becomes possible as soon as writers
> aren't serialized. A listener stops reading at an uncommitted
> transaction's entries. When that transaction commits and the listener
> isn't interested in its channels, SignalBackends() moves the listener
> past those entries instead of waking it. If the listener had been
> woken for entries written after them, nobody wakes it again, and those
> notifications sit there until something else wakes it. With today's
> serialized writers I don't see a way to hit this, but any change that
> lets writers proceed without the lock needs it, including releasing
> the lock before XLogFlush() as discussed in [2]. Readers now record
> that they stopped at an uncommitted entry, and SignalBackends() wakes
> such a listener instead of advancing it if anything was written after
> the committing transaction's entries.
>
> 0003 adds pg_notify(text, text, boolean), with docs and tests. The new
> isolation test in src/test/modules/injection_points uses the
> commit-before-clog-update injection point to hold one notifier in the
> middle of its commit. It checks that unordered notifiers don't wait
> for it while ordered ones still do, and that the notification queued
> behind it is delivered once it commits. With 0002 reverted, that test
> fails in exactly the three permutations that involve an unordered
> writer. The new catalog entry (OID 9717) needs a catversion bump.
>
> Each patch builds without warnings and passes the core regression,
> isolation and injection_points tests on its own (macOS, autoconf). A
> backport of the series to 19.0 also passes them on Ubuntu/aarch64 with
> meson.
>
> Benchmark: each transaction updates one pgbench_accounts row and then
> sends no notification, pg_notify(ch, payload), or
> pg_notify(ch, payload, false). A separate libpq listener drains the
> notifications and counts them. The scripts:
>
> \set aid random(1, 100000 * :scale)
> BEGIN;
> UPDATE pgbench_accounts SET abalance = abalance + 1 WHERE aid = :aid;
> SELECT pg_notify('bench', :aid::text); -- or ..., false)
> END;
>
> These numbers are from a copy of a 19.0 cluster on an aarch64 Ubuntu
> server, with the series backported (async.c is the same as in master
> apart from unrelated fixes), an optimized build, synchronous_commit =
> on, local storage and no synchronous standby. "Delivered" is the
> listener's count, for both notifying modes:
>
> clients no NOTIFY ordered unordered delivered
> 8 5188 tps 677 tps, 11.8 ms 3388 tps, 2.4 ms 4000/4000
> 32 7033 tps 1164 tps, 27.5 ms 8571 tps, 3.7 ms 16000/16000
> 64 8422 tps 928 tps, 69.0 ms 7810 tps, 8.2 ms 19200/19200
>
> The ordered rate stays flat as clients are added, at about one commit
> per millisecond, which is the WAL flush time on that machine. With a
> synchronous standby the gap should be larger. More measurements on
> other hardware are in progress.
>
> Open questions:
>
> - API. A third argument to pg_notify() was the smallest change. NOTIFY
> itself has no option syntax; something like
> NOTIFY ch, 'payload' WITH (ordered = false) would need grammar work.
> A GUC would let existing triggers opt in without code changes, but
> it changes the semantics behind the caller's back, which is what [1]
> ran into.
>
> - Head-of-line delay. Unordered entries can wait behind entries of a
> transaction that is still committing. Normally that's bounded by one
> commit's latency, but a stuck synchronous standby delays the delivery
> of everything behind such an entry. Today the same situation stalls
> the other notifiers' commits instead.
>
> - 0001 makes readers wait while a multi-page insertion is in progress.
> I haven't seen this matter, but very large NOTIFY batches could make
> it visible.
>
> - Shrinking the lock for ordered notifications too (releasing it once
> the commit record is inserted, before XLogFlush()) is a separate
> question that 0002 would also be needed for.
>
> [1] Proposal: Out-of-Order NOTIFY via GUC to Improve LISTEN/NOTIFY Throughput
> https://www.postgresql.org/message-id/CAK80%3DjiWjMAfjwJymPoG3%3DFNE%3DhHygn5dXaoax1BWQT1rJrCCw%40mail.gmail.com
> and Tom's design notes in that thread:
> https://www.postgresql.org/message-id/1878165.1752858390%40sss.pgh.pa.us
> [2] [PATCH] Write Notifications Through WAL
> https://www.postgresql.org/message-id/CAK80%3DjjvgWJL5EdzH_Bxc37gn53PJaB5sDXB3VPfJuN7G1xL5Q%40mail.gmail.com
> [3] https://postgr.es/m/6899c044-4a82-49be-8117-e6f669765f7e@app.fastmail.com
>
> Regards,
> Ivan Alekseev
>
> <v1-0001-Hold-NotifyQueueLock-across-the-whole-of-a-NOTIFY.patch><v1-0002-Wake-listeners-that-stopped-reading-at-an-uncommi.patch><v1-0003-Allow-NOTIFY-without-cross-transaction-commit-ord.patch>

Attachment Content-Type Size
v2-0001-Hold-NotifyQueueLock-across-the-whole-of-a-NOTIFY.patch application/octet-stream 6.3 KB
v2-0002-Wake-listeners-that-stopped-reading-at-an-uncommi.patch application/octet-stream 8.5 KB
v2-0003-Allow-NOTIFY-without-cross-transaction-commit-ord.patch application/octet-stream 28.5 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Koshino Taiki 2026-12-04 09:32:03 Re: [PATCH] psql: Add tab completion for \dO, \dc and \det
Previous Message solai v 2026-12-04 09:21:37 Re: Allow pg_read_all_stats to read replication origin status