Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1vE9Lq-006IZQ-Mt for pgsql-hackers@arkaria.postgresql.org; Wed, 29 Oct 2025 16:48:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1vE9Lp-002MuR-HP for pgsql-hackers@arkaria.postgresql.org; Wed, 29 Oct 2025 16:48:44 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1vE9Lp-002MuH-7z for pgsql-hackers@lists.postgresql.org; Wed, 29 Oct 2025 16:48:44 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vE9Ll-004uoc-37 for pgsql-hackers@postgresql.org; Wed, 29 Oct 2025 16:48:43 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 59TGmNN92759500; Wed, 29 Oct 2025 12:48:24 -0400 From: Tom Lane To: "Joel Jacobson" cc: "Heikki Linnakangas" , pgsql-hackers Subject: Re: LISTEN/NOTIFY bug: VACUUM sets frozenxid past a xid in async queue In-reply-to: <2297af80-dd5d-4fcc-815b-75bd64b00bf9@app.fastmail.com> References: <202510221548.tgggefdrlpo3@alvherre.pgsql> <149c402d-54c9-48dd-a730-2f5b958f2129@app.fastmail.com> <769bbdba-f86f-44ad-abad-f6ea4f74e3f2@iki.fi> <2297af80-dd5d-4fcc-815b-75bd64b00bf9@app.fastmail.com> Comments: In-reply-to "Joel Jacobson" message dated "Wed, 29 Oct 2025 13:30:12 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2759498.1761756503.1@sss.pgh.pa.us> Date: Wed, 29 Oct 2025 12:48:23 -0400 Message-ID: <2759499.1761756503@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk "Joel Jacobson" writes: > On Wed, Oct 29, 2025, at 12:40, Heikki Linnakangas wrote: >> That said, we could add COMMITTED/INVALID hint bits to AsyncQueueEntry, >> similar to heap tuples, and set the hint bit instead of replacing the >> original xid. That might be good for performance too: If the first >> backend to call TransactionIdDidCommit() on an AsyncQueueEntry would set >> the hint bit, that would save the effort for other listening backends. > Interesting, sounds like a really promising approach. I don't see any room for adding hint bits without enlarging AsyncQueueEntry, which I'd rather not do. But we could mechanize that without a separate hint bit by replacing the tested XID with FrozenXid or InvalidXid, as the patch already does. We already require writing an XID to shared memory to be atomic, so that seems okay. However ... that won't actually work, the reason being that asyncQueueProcessPageEntries() doesn't work directly from an SLRU page but from a local copy. Even if it were to modify the state of that copy, no other backend would see the effects. The reason it's like that is stated in the comments: * The current page must have been fetched into page_buffer from shared * memory. (We could access the page right in shared memory, but that * would imply holding the SLRU bank lock throughout this routine.) The patch proposed here likewise appears to involve holding an SLRU bank lock throughout what could be a significant number of TransactionIdDidCommit tests. That seems like it could result in a pretty bad "burp" in NOTIFY throughput. That problem is ameliorated by only doing it when VACUUM is trying to advance datfrozenxid, but still I wonder if we can't find a less concurrency-unfriendly answer. The local-copy behavior also means that this patch isn't quite a 100% fix. We could have a race condition like so: 1. Backend A grabs a copy of some SLRU page and begins running asyncQueueProcessPageEntries(), but then loses the CPU. 2. Backend B completes a VACUUM, finds it can advance datfrozenxid, marks some relevant XIDs as frozen in the notify SLRU, and truncates clog. 3. Backend A gets control back, tries to discover the state of some XID that's still present in its local copy of the XID page, and fails. Step 2 will take long enough that this isn't very plausible timing-wise, but it's still theoretically a hole. All of this is a problem mainly because of the presumption that holding an SLRU bank lock for a long time is bad. I wonder how dangerous that really is. regards, tom lane