NOTIFY's direct queue advancement can still skip messages for a backend that is committing LISTEN: a notifier that commits while the listener is in PreCommit_Notify between registering its queue position and adding its channel entry advances it past the message ===================================================================================================================================================================================================================================================================== AI-generated bug report from the AI Annex Report ID: 01a23007-ae97-7e07-9edb-acccb4a38eba Filed: 2026-12-01 05:15:39 UTC Severity: 25/100 Blamed commit: 282b1cde9dedf456ecf02eb27caf086023a7bb71 ## Summary Commit 0f24332aeb4f43409c2a7bec9fef1e3317689bc5 ("Fix NOTIFY wakeups for pre-commit LISTEN entries") settled that a new listener should see every message after its initially registered queue position, because the direct-advance logic in SignalBackends() is meant to be an optimization that does not change semantics. A window remains inside PreCommit_Notify(): after BecomeRegisteredListener() has put the backend in the listener list (and caught it up to the queue head), but before PrepareTableEntriesForListen() has added the backend to the channel's entry in the shared channel table. A NOTIFY that commits in that window finds the backend in the listener list but not in the channel entry, treats it as uninterested, and directly advances its queue position past the message. The session then commits its LISTEN and never receives the message, although it lies after its registered position. If the same notifier commits a moment later (after the channel entry exists but still before the LISTEN commits), the message is delivered. The same happens for a backend that is already listening on another channel and runs LISTEN on a new channel: a notification on the new channel that commits during that transaction's PreCommit_Notify, before the channel entry is added, is skipped by direct advancement, whereas one committing slightly later is delivered. With several LISTEN commands in one transaction the window spans all channel entries created before the affected one. ## Reproduction Needs a debugger to hold the LISTEN backend in the window. ``` -- session L SELECT pg_backend_pid(); -- say 12345 ``` In a shell: `gdb -p 12345`, then `break PrepareTableEntriesForListen` and `continue`. ``` -- session L (blocks at the breakpoint during COMMIT) LISTEN x; -- session S, while L is stopped NOTIFY x, 'first'; ``` In gdb: `detach`. Session L's LISTEN completes. ``` -- session S NOTIFY x, 'second'; -- session L SELECT 1; ``` Expected: L reports both 'first' and 'second' (both messages are after L's registered queue position, and both arrive after L has registered). Actual: L reports only 'second'. Control: repeat with `break RecordTransactionCommit` instead (L stops later in the same commit, after the channel entry exists but before its commit record). L reports both 'first' and 'second'. In both runs S's first NOTIFY commits before L's LISTEN commits; only the position of L inside PreCommit_Notify differs. Same result when L was already listening on another channel (run `LISTEN a;` in L before attaching the debugger): 'first' is lost with the PrepareTableEntriesForListen pause and delivered with the RecordTransactionCommit pause. ## Analysis src/backend/commands/async.c, PreCommit_Notify(): for LISTEN actions it calls BecomeRegisteredListener(), which links the backend into the listener list with a queue position (and catches up to the head with isAdvancing set), and only afterwards PrepareTableEntriesForListen(), which adds the ListenerEntry to the channel's globalChannelTable entry. SignalBackends() first marks for wakeup the listeners found in the notified channels' entries, then scans the whole listener list: any listener not marked and not isAdvancing whose position lies in [queueHeadBeforeWrite, queueHeadAfterWrite) is moved to queueHeadAfterWrite. A backend between the two steps above is in the listener list but not in the channel entry, is not advancing, and sits at the head (just caught up), so it is moved past the notifier's message. Once its channel entry is created and the LISTEN commits, the message is behind its position and is never read. Before 282b1cde9dedf456ecf02eb27caf086023a7bb71 there was no direct advancement and such a listener read the message later. 0f24332aeb4f43409c2a7bec9fef1e3317689bc5 closed the part of the window between PreCommit_Notify and AtCommit_Notify by signalling staged entries, but not the part between listener registration and channel-entry creation inside PreCommit_Notify. ## Notes Tested on master b7822f9992216f9aa85422337c15f1e7a8d6db72 (cassert build). Three runs each: the PrepareTableEntriesForListen pause lost 'first' every time; the RecordTransactionCommit pause delivered both every time. The window has no wait in it, so hitting it without a debugger takes timing luck, but it widens with many LISTEN commands in one transaction. ## Branches Reproduced on master. The blamed commit is in REL_19_STABLE and master only.