| 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: | [PATCH] Allow NOTIFY without cross-transaction commit ordering |
| Date: | 2026-11-25 15:44:03 |
| Message-ID: | 4B741003-AB93-4252-8F36-65D5AE06768F@nettrash.me |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
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
| Attachment | Content-Type | Size |
|---|---|---|
| v1-0001-Hold-NotifyQueueLock-across-the-whole-of-a-NOTIFY.patch | application/octet-stream | 6.3 KB |
| v1-0002-Wake-listeners-that-stopped-reading-at-an-uncommi.patch | application/octet-stream | 8.5 KB |
| v1-0003-Allow-NOTIFY-without-cross-transaction-commit-ord.patch | application/octet-stream | 27.9 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Greg Sabino Mullane | 2026-11-25 15:46:34 | Re: passwordcheck: Make the user name check case-insensitive |
| Previous Message | Kirill Reshke | 2026-11-25 15:38:38 | Re: CYCLE over calculates windows functions |