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 1v92IT-008xNZ-2T for pgsql-hackers@arkaria.postgresql.org; Wed, 15 Oct 2025 14:16:08 +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 1v92IR-006j2u-Qk for pgsql-hackers@arkaria.postgresql.org; Wed, 15 Oct 2025 14:16:06 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1v92IR-006j2l-Gz for pgsql-hackers@lists.postgresql.org; Wed, 15 Oct 2025 14:16:06 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1v92IO-001ugz-33 for pgsql-hackers@postgresql.org; Wed, 15 Oct 2025 14:16:05 +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 59FEG1BX1126738; Wed, 15 Oct 2025 10:16:01 -0400 From: Tom Lane To: Arseniy Mukhin cc: Joel Jacobson , pgsql-hackers Subject: Re: Optimize LISTEN/NOTIFY In-reply-to: References: <6899c044-4a82-49be-8117-e6f669765f7e@app.fastmail.com> <165530.1752362320@sss.pgh.pa.us> <02a7cd37-e2fc-4212-8b19-f8c239c95fb8@app.fastmail.com> <96f00bf1-cc9d-4520-9d02-9e14e7767c88@app.fastmail.com> <30c2aa7d-dd6c-4b68-a2e4-f217a1a34acf@app.fastmail.com> <0b4d402a-9ac2-4aa8-acf8-8231dbe579ea@app.fastmail.com> <3095599.1758644879@sss.pgh.pa.us> <0dc6a2cc-5216-4dc1-9dd2-430cafc6095b@app.fastmail.com> <52CC167F-763B-4ECA-B0B4-DAB381816828@gmail.com> <9186C6D0-F7A9-482A-9183-89E530B57E36@gmail.com> <1073593.1759423179@sss.pgh.pa.us> <4bd5e6c4-6fa7-44bb-869d-59a32a331fa8@app.fastmail.com> <85828f29-e72e-4400-94f3-9a69bc8dc239@app.fastmail.com> <2495353.1759860890@sss.pgh.pa.us> <8aeae418-92a6-4bbd-9c06-9574c79e59f7@app.fastmail.com> <2531672.1759868124@sss.pgh.pa.us> <474efa78-337c-41cd-a73a-f845a0115109@app.fastmail.com> <2749343.1759949176@sss.pgh.pa.us> <8bfca2be-1ec0-4e15-aafb-0b7b661fe936@app.fastmail.com> <9eba307f-f2fb-48f0-9507-2e197f39ef9e@app.fastmail.com> <8c71183a-0d28-4bcf-a806-78446ff95404@app.fastmail.com> <1009807.1760476747@sss.pgh.pa.us> <2a30bcf1-aee2-4b07-a302-11e4b350adaf@app.fastmail.com> Comments: In-reply-to Arseniy Mukhin message dated "Wed, 15 Oct 2025 14:19:40 +0300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1126736.1760537761.1@sss.pgh.pa.us> Date: Wed, 15 Oct 2025 10:16:01 -0400 Message-ID: <1126737.1760537761@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Arseniy Mukhin writes: > I think "Direct advancement" is a good idea. But the way it's > implemented now has a concurrency bug. Listeners store its current > position in the local variable 'pos' during the reading in > asyncQueueReadAllNotifications() and don't hold NotifyQueueLock. It > means that some notifier can directly advance the listener's position > while the listener has an old value in the local variable. The same > time we use listener positions to find out the limit we can truncate > the queue in asyncQueueAdvanceTail(). Good catch! I think we can perhaps salvage the idea if we invent a separate "advisory" queue position field, which tells its backend "hey, you could skip as far as here if you want", but is not used for purposes of SLRU truncation. Alternatively, split the queue pos into "this is where to read next" and "this is as much as I'm definitively done with", where the second field gets advanced at the end of asyncQueueReadAllNotifications. Not sure which view would be less confusing (in the end I guess they're nearly the same thing, differently explained). A different line of thought could be to get rid of asyncQueueReadAllNotifications's optimization of moving the queue pos only once, per * (We could alternatively retake NotifyQueueLock and move the position * before handling each individual message, but that seems like too much * lock traffic.) Since we only need shared lock to advance our own queue pos, maybe that wouldn't be too awful. Not sure. regards, tom lane