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.96) (envelope-from ) id 1vJEAg-00FLI8-2f for pgsql-hackers@arkaria.postgresql.org; Wed, 12 Nov 2025 16:58:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vJEAd-00CxG4-2u for pgsql-hackers@arkaria.postgresql.org; Wed, 12 Nov 2025 16:58:11 +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.96) (envelope-from ) id 1vJEAd-00CxFv-1O for pgsql-hackers@lists.postgresql.org; Wed, 12 Nov 2025 16:58:11 +0000 Received: from mail-yx1-xb12a.google.com ([2607:f8b0:4864:20::b12a]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vJEAX-006lGz-0e for pgsql-hackers@postgresql.org; Wed, 12 Nov 2025 16:58:10 +0000 Received: by mail-yx1-xb12a.google.com with SMTP id 956f58d0204a3-63f9beb26f7so995349d50.2 for ; Wed, 12 Nov 2025 08:58:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762966684; x=1763571484; darn=postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=5pn7HMbPvLGDeGyW41RSln98njmZFxqg1flnsutVKp8=; b=HnfB+y/vDu+m7D1cE85EDfFXZfXstQuwIDC+dQ5sFL1k0XAXFOJE1P3aN9C9hviTJ6 TemKC+9gBiONdQ3dvAkqfm14tIJ7jQodzat9RaA46qUkANX8mAQTnc1ihxPqbI8jnL7u 3X9DwmhvvgsKWc0zxPsNxgJOK4EdUQ92sombFTCvtvsWIstJ3NXwQXXmJ/ZIYqxx5303 hgzQRU3TU3tLOenZG3Ksy4FPiL2JQSQ/4toi28QZnJdmTcoMMMapp3bFq5sah9WWpV8G bwxeP+yiFMxelh57pK5xTNKsXuGmgaW/M4K9r4FTVJk4JZFHcGIkXKH1euHvLbqN2FK2 8tlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762966684; x=1763571484; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=5pn7HMbPvLGDeGyW41RSln98njmZFxqg1flnsutVKp8=; b=i4jlMSPiaMgA7aNss7y19vQUTR4SmbT+MkLasKzfMupyS4km2J1kbW1J1aLx5VJpbc h5kx6a6vVKVaYgbPt9qVCJmO5LtIWB9AwcP4jc9+U8f10SsXyygiHJ+SqWpze3pb0888 vve/FbWsao6p/PX3Vvk2USxLWyGCWXuV72FSGN2ijxlbR736N6zTv10vL3ry6aEg3Byz /5VRRM3bPn9pf484Uv4NA/O/r7/aEVlTtqvxhUz4+d5/Ib5Hdc6Plbf6HqjvIM5xdwUG stujAN08KkT1rGhNvFjyR9ktUbYZWuDiyvAk1l8c56wb64IQ5LKq6CrHiFpOR/qRX8a7 B4wQ== X-Gm-Message-State: AOJu0YzKDp4RwzW/4K/WAtXplQlAiHP7sIrVBI5F21NAG0Zrmj4hydma vATRNriRgQUTWBFDt+oev6BmbYc3tCXBkkoJCp1OkNA5SoorYBoergrLVy0MKX727GUm18O5Iy3 ebCu7ztsT2acdThFDXbTA+zc+/sBnpl0= X-Gm-Gg: ASbGnctGxH7To9fFiU3Eh8c1tYH4//xTSEUIMkhAFDkGU/pWqBJ3yp698911wOHfrkj hjOTJBY19fhR8Fqr67IXjS3QgQiAOvnbngMuZdTqJVQHLrlUbTOrz2vHV7YOP5wmTFztTe5pkjE H9d9AXKxd1JbpTNy6xMMQZpspc2HoDlD2GYUSyUUYkpOFZMtwJ6NnbUelulhuWdpvTLvxfZf/Oe tMXq9ecojmc+6xvugWsQ19JRcREEX9c+DNH0m9vx/oAEgRJ7VIf5+gk3w== X-Google-Smtp-Source: AGHT+IHOcvICrJP5cPHAY4hrf3S7LgsdBZJg9qK8eRrzJIwOKAXVZJrzOvrniAOvnY9u97WhfQVxvWom5OJEuAq8M7k= X-Received: by 2002:a05:690c:6206:b0:787:badd:4c with SMTP id 00721157ae682-7881361bc07mr54531537b3.27.1762966684107; Wed, 12 Nov 2025 08:58:04 -0800 (PST) MIME-Version: 1.0 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> <1F7227F5-C33D-4E2C-8511-33F1468590D0@gmail.com> <0a5a20d3-4621-46b3-b2ab-903f63a20dea@app.fastmail.com> <6F913129-ABEF-4004-AAF3-F22FC34!29AE8@gmail.com> <1547585.1760645808@sss.pgh.pa.us> <14865EB6-0BF4-462B-9072-10BDAC10C052@gmail.com> <0BCA1C2D-B92C-459E-B1A6-6D06BA4C62CF@gmail.com> <55d24cbb-e9ef-491f-a99b-b3dbd7cecdf9@app.fastmail.com> <38574cad-e90d-47b7-a015-753bb6bbc360@app.fastmail.com> <66631FB7-5BEA-4ED5-A694-9AD8B9CCFEE8@gmail.com> <4b7b49a5-5e1a-44a8-93e0-60457d15cb1d@app.fastmail.com> <82DEA2B6-6FC5-4A79-BDE3-1FD72F104A6E@gmail.com> <38de1036-d8cf-420c-b845-edb5a946b191@app.fastmail.com> <87E40BF8-8877-4DBD-9040-99AF8A4E6358@gmail.com> <7556f0d4-03fd-451a-bd34-5f62b424319a@app.fastmail.com> <290910DE-9A03-4AE6-B348-073D5DA96ACC@gmail.com> <4B243750-12BE-4C16-A769-A803268F40E3@gmail.com> <4605CAD6-69D5-4082-B96C-91FC0DE5399D@gmail.com> <294e1641-d658-4d43-8671-60e8ff860532@app.fastmail.com> <26d4dd6a-cfc5-4efc-9704-9cd3216ed712@app.fastmail.com> <7456ec96-7a9c-45a0-988e-ba1c7f9ec937@app.fastmail.com> In-Reply-To: <7456ec96-7a9c-45a0-988e-ba1c7f9ec937@app.fastmail.com> From: Arseniy Mukhin Date: Wed, 12 Nov 2025 19:57:51 +0300 X-Gm-Features: AWmQ_blFmLCJrsS30j68qQBrFE8I03-NOIn1AqH1uG6HarLQTVABQzRW0U8-6HY Message-ID: Subject: Re: Optimize LISTEN/NOTIFY To: Joel Jacobson Cc: pgsql-hackers Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, On Tue, Nov 11, 2025 at 7:35=E2=80=AFPM Joel Jacobson w= rote: > > On Sat, Nov 8, 2025, at 16:04, Joel Jacobson wrote: > > On Sat, Nov 8, 2025, at 13:59, Joel Jacobson wrote: > >> On Fri, Nov 7, 2025, at 19:59, Joel Jacobson wrote: > >> This in turn could cause a listening backend to remain behind, if ther= e > >> would be no more notifies, so it unfortunately seems like we will alwa= ys > >> need to signal when a backend isAdvancing, and therefore have no use o= f > >> the advancingPos field. > > Having thought about this, I don't think this is actually a problem, > since this isn't any worse than what we currently have in master. > Listening backends can currently end up stationary behind QUEUE_HEAD, in > exactly this situation, when they don't read up until QUEUE_HEAD in > asyncQueueReadAllNotifications. In this case, we currently rely on > another NOTIFY to wake them up, so v24 wouldn't be any worse. > > My apologies for again making the mistake of mixing in robustness > improvements into this patch. I must keep in mind this is solely an > optimization patch. > > I'm therefore attaching v24 again, but renamed to v26. Thank you for the new version! I read direct advancement part of v26, one point about it: The comment in SignalBackend says: * Listening backends that are not advancing and are stationary a= t * a position somewhere in the range we just wrote, can safely be * direct advanced to the new queue head, since we know that they * are not interested in our messages. */ IIUC it's impossible for the listener to stop somewhere in between queueHeadBeforeWrite and queueHeadAfterWrite. If the listener has managed to read the first notification from the notifier, it means the notifier transaction is complete and the listener should stop only after reading all notifications (so we should always see pos =3D queueHeadAfterWrite or further). So If I haven't missed anything, I think we can use QUEUE_POS_EQUAL as direct advancement condition: if (!QUEUE_BACKEND_IS_ADVANCING(i) && QUEUE_POS_EQUAL(pos, queueHeadBeforeWrite)) { QUEUE_BACKEND_POS(i) =3D queueHeadAfterWrite; } Best regards, Arseniy Mukhin