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 1vJQwh-006JyD-2G for pgsql-hackers@arkaria.postgresql.org; Thu, 13 Nov 2025 06:36:38 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vJQwf-00GnR0-12 for pgsql-hackers@arkaria.postgresql.org; Thu, 13 Nov 2025 06:36:37 +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 1vJQwe-00GnQr-2r for pgsql-hackers@lists.postgresql.org; Thu, 13 Nov 2025 06:36:37 +0000 Received: from mail-yw1-x112d.google.com ([2607:f8b0:4864:20::112d]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vJQwd-006rqe-13 for pgsql-hackers@postgresql.org; Thu, 13 Nov 2025 06:36:36 +0000 Received: by mail-yw1-x112d.google.com with SMTP id 00721157ae682-780fe76f457so4614777b3.0 for ; Wed, 12 Nov 2025 22:36:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1763015794; x=1763620594; 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=Xyu6VGJk7XgxpTG0/5WISfuyTYkxyx0HudqTpjD8iN0=; b=Kj7ssRU28AGAk6kG4AZeKrMbYUcqivB3UYVzZzdvGeeXh4FRgn5f2SmLbH9uAg9nI3 oNiNybrfRTrkmlNxggf7dH0KFXyGFm6ZiWahY8wkbXxSoizAMROI8IdpFoL89XUvHT8f aLGMgYcIQt+POoXh3ZF8CM3etDYLIwfQVojdljG7EzFsKazCR5+UjxolfeAO563iTG2P pLeX1+Y6wgc5SMQDuXnaxBGQt0FcO6kfPbJqVFJeV0Tlpk/ZSp6pyvG+Qwhx4+OxMkBF 1y7/zQ1DQgfiCUisKoWX5kkDDN+YQHV6MGIrH0nDa9X79R5pXHjAmB6h6T06FUfQKsEs +72A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763015794; x=1763620594; 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=Xyu6VGJk7XgxpTG0/5WISfuyTYkxyx0HudqTpjD8iN0=; b=hsJOa8bqxDN3E+7qE5o9KQfoUoZHI59b3grsLoKwEfh4QJ4YjdZQoTtmBYChOUqmVq oErKxtnZfSFbz5teSwsVqKK6xG4VWyJ/P+j5vPvGF7QZZLU25/OzgHqrBCg5q/dT6Yph kSbxRyRuTRomBX8cTZd4+ipDg4yPrnK+zImXiAyz9FwnxgBcbqN3HrqsmjlLwBOls1SU k2X1gb3gHhTpbAP8KY80NTVOxn9ErfNfxfyLW0lODic8xYprndMb27Nimd9semKatb4I wv6niWusO6ELHFhoJRjMWMwxr9wg5BOPfxOANGURMasj+L4gojrzXAYmR8AkJdrL5kuM tZiw== X-Gm-Message-State: AOJu0Yyyt+48ooYVX3H+xkRTm6DvYCoxoVNQudHOtoQtucFpchziWfCz zR/SPorczS1Iio2Wjg3P2zHqTJ0DtLd1k/pr53Uj0R8uq2Kpz5Acfdpq4XNLSfvAcZZA7LlWdxd c+qQqccFMdLSP3efLCM+3jZARHxHKwP8= X-Gm-Gg: ASbGnctZz+tYOlbMeEDB423IH1uXobADatB+P5htAjhccj+ORX9kkUnIuv73rAyNEda CvJFlC0pfIDyrggaPmeOrNaHxHMvu+coSVQfCmjCVj5uoj+udJkGRlCm8r9S3nX1uhQdKHWOXy5 Hv51NSjFOthC5ypO4JziqcD8ZWURC+oAHDng2+WPT4SCm0tGKsfeo151oxeqcIY9BWaZZnbbHJV f/1JsLFl1aZpAu6fROOHzDrv+rLx7gz9zmW2tBcpeVNs+qoWt3aI5yD1NkaoA== X-Google-Smtp-Source: AGHT+IE8bHtzUj5S3H6TmH2frCt70BLDNS4B7J7ClTLYrnAp9Q1XsfIt+zfqz+BtU9Juo+SzR6i8zKXTJDomb34uVrI= X-Received: by 2002:a05:690c:830b:b0:786:3ca8:6bd9 with SMTP id 00721157ae682-788135ddab9mr79850807b3.4.1763015794478; Wed, 12 Nov 2025 22:36:34 -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> <0253b822-e8fd-4067-ab24-23493c115a2a@app.fastmail.com> In-Reply-To: <0253b822-e8fd-4067-ab24-23493c115a2a@app.fastmail.com> From: Arseniy Mukhin Date: Thu, 13 Nov 2025 09:36:22 +0300 X-Gm-Features: AWmQ_bl6cqXhT91sDpDgnSArOc1E5xefJPZDfrdLalwNxq-eHKCw00fnTISTMBU 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 On Thu, Nov 13, 2025 at 9:28=E2=80=AFAM Joel Jacobson w= rote: > > On Wed, Nov 12, 2025, at 17:57, Arseniy Mukhin wrote: > > 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; > > } > > I added some logging just to test the hypothesis: > > @@ -2072,6 +2082,12 @@ SignalBackends(void) > { > Assert(!QUEUE_POS_PRECEDES(pos, queueHeadBeforeWrite)); > > + if (!QUEUE_POS_EQUAL(pos, queueHeadBeforeWrite)) > + elog(LOG, "Direct advancement: PID %d from pos (%lld,= %d) to queueHeadAfterWrite (%lld,%d)", > + pid, > + (long long) QUEUE_POS_PAGE(pos), QUEUE_POS_OFFSE= T(pos), > + (long long) QUEUE_POS_PAGE(queueHeadAfterWrite),= QUEUE_POS_OFFSET(queueHeadAfterWrite)); > + > QUEUE_BACKEND_POS(i) =3D queueHeadAfterWrite; > } > } > > And I'm getting a lot of such log entries when benchmarking > `./pg_async_notify_test --listeners 1 --notifiers 1 --channels 50` > > I think this confirms that listeners can actually stop somewhere in betwe= en > queueHeadBeforeWrite and queueHeadAfterWrite. Ahh, yes, I think you are right. I missed that notifiers update the head when they move to the next page. Thank you for the detailed example and sorry for taking your time with it. I agree that QUEUE_POS_PRECEDES(pos, queueHeadAfterWrite) is correct and covers more cases where we can do direct advancement. Best regards, Arseniy Mukhin