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 1v97ZM-00AHCw-2p for pgsql-hackers@arkaria.postgresql.org; Wed, 15 Oct 2025 19:53:55 +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 1v97ZK-0083SL-VP for pgsql-hackers@arkaria.postgresql.org; Wed, 15 Oct 2025 19:53:53 +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 1v97ZK-0083SC-Lm for pgsql-hackers@lists.postgresql.org; Wed, 15 Oct 2025 19:53:53 +0000 Received: from mail-yw1-x1131.google.com ([2607:f8b0:4864:20::1131]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1v97ZI-001xV9-0D for pgsql-hackers@postgresql.org; Wed, 15 Oct 2025 19:53:52 +0000 Received: by mail-yw1-x1131.google.com with SMTP id 00721157ae682-71d71bcab6fso64396197b3.0 for ; Wed, 15 Oct 2025 12:53:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1760558031; x=1761162831; 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=sbgWE7PGPbpLmmTrPzAknRBiSovpjjRB3P5mmqDEUww=; b=AZB4pFnQBeadguspTqWYeNqT72+7SD/hXPu7t2OfxBOoVHNamqI+MjZujrrT+LOtdl GjzEnwk/Q9rkP913bIu56JXUzwq5hXJ9OSCR9Yui9FNldElarbaQT4KVX3bTs3TCTfl4 q/UT6V3VGthDzelSbozVF0WlIrjms17oF6ZWktmjlcXZ11id4I5WqRaNGFvYdppaUT0t hUmpwC0WeH/QKwauFGLDhNRdt1y5E09IHPjvfcyhgjh3Jg/AEPS+8rDlJtMnU/UZXP9f jAw34u0dd//NM2cgIsR2f4lbOHjcRRciViVh0SWBhI1w9AnJ0W4U6I+N80/VYvKhqI+T kkNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1760558031; x=1761162831; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=sbgWE7PGPbpLmmTrPzAknRBiSovpjjRB3P5mmqDEUww=; b=jQmYYd9j+ioLSr+xga2Ne+KADT4uYQ5aAsU0lPgHj4Iv78lZ0RhM0HGIDzfhaFZm3B OJINtiz/84A18W8TKlCj5DrXeIvYHuyw6/O3PBTbqka5RPdN36K6ITXXSeccf9IDjsaZ KOPAYs1D07oCfaINlxEZ20ZmrbWhY4froawOInjVetvZ6YE+OD88B2iOvdp2CRKFUKch epqk3kprLGirVgwiXgzxlpGyVik1tlEENuCoS0xPAG9RXhJg0DM7Gghy0XgE5AQRx87k IDrIvNr/hHsCyiFU4AH2ytNqVEcsZg3eoMonc6BbktIa6op/ghtJxuaQDTHaDfDPKYjJ jDJw== X-Forwarded-Encrypted: i=1; AJvYcCUXPLH5wmheMQw77PtH9xcH+YmUEHaRI1v63dY2byJfZ8a0crRBLBWKRWIfN4rsvdb3Jbc+w4jr4q+1j9jF@postgresql.org X-Gm-Message-State: AOJu0YxDqRiXr5REmCdYmn7IIuSFWlaJ7U2S46V9M3UVG34tYALVbs7j 2qQyk0lTr0wVVZM/r3kSS0vdVu8rO92InxdkNAD+qeCJI//RyeedHLGW27ZuMJXExh4q9ynBa1f 9J1XXgyTo3zYk0EWHn1+AIAFdroyX6QMk1bhV+h5qRw== X-Gm-Gg: ASbGncuFwmRaXddD5FLHgpuGYtI9mc8yY5ZrLBVE0vkkjMx8FFGjcZ020hISXCSzLcQ iQdv5Q6qqhAKMdn9EZR3HNDBbSTZEe/2cRjmVLQzEuHJfTzQXlDz+j5d3Nnm/K/TDRhfA2QA/h3 a1Ta8ZodK43re931sIbGTLM4rCdks0iDLWKgEjA3J/Q4gIFk73j8N7KXrNulp+LmJWevNCun+q1 Eh9yDLOlTooku+wDBtznMkYaw== X-Google-Smtp-Source: AGHT+IFGhuj0ueijT5YPoHliPSZVhjhRutLMVDUkNB1o0QazJTpJUmXRE6eZtQhsgTwvZ+X9sHxl9biwmwQxP+ANvyg= X-Received: by 2002:a53:da8a:0:b0:62a:e5e3:b1f with SMTP id 956f58d0204a3-63ccb885a9cmr18532571d50.18.1760558030845; Wed, 15 Oct 2025 12:53:50 -0700 (PDT) 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> <2a30bcf1-aee2-4b07-a302-11e4b350adaf@app.fastmail.com> <1126737.1760537761@sss.pgh.pa.us> In-Reply-To: <1126737.1760537761@sss.pgh.pa.us> From: Arseniy Mukhin Date: Wed, 15 Oct 2025 22:53:38 +0300 X-Gm-Features: AS18NWB52qaXUau6rGH-L5RFdQe0aWbNtQO8j050q5JzxT6fMf4WHDQneaI5wS4 Message-ID: Subject: Re: Optimize LISTEN/NOTIFY To: Tom Lane Cc: Joel Jacobson , 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 Wed, Oct 15, 2025 at 5:16=E2=80=AFPM Tom Lane wrote: > > 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 po= sition > * before handling each individual message, but that seems like t= oo 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 Advisory queue position field sounds good IMHO. Listeners are still solely responsible for advancing their positions so they still need to wake up to do it, but they will only do so if there are relevant notifications, or if they are too far behind. In any case they will be able to jump over all irrelevant stuff. Best regards, Arseniy Mukhin