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 1vFIQM-00AGed-Di for pgsql-hackers@arkaria.postgresql.org; Sat, 01 Nov 2025 20:42:09 +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 1vFIQJ-007EYp-LP for pgsql-hackers@arkaria.postgresql.org; Sat, 01 Nov 2025 20:42: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 1vFIQJ-007EYh-08 for pgsql-hackers@lists.postgresql.org; Sat, 01 Nov 2025 20:42:06 +0000 Received: from mail-yw1-x1133.google.com ([2607:f8b0:4864:20::1133]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vFIQF-004wed-1V for pgsql-hackers@postgresql.org; Sat, 01 Nov 2025 20:42:04 +0000 Received: by mail-yw1-x1133.google.com with SMTP id 00721157ae682-71d6014810fso36037157b3.0 for ; Sat, 01 Nov 2025 13:42:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762029722; x=1762634522; 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=6Ee+svShMoEXU8pZ1fN6PWJ0g0ZjVivSjJEmRnPGg78=; b=LE/9XK0EETBNROOeDUAQpm54aqQUcZW8xmAHBsjnji1yxQ8ddVMXotpQ9YtIKbZbwf tCKIUkCfezZQ0niICRHCpt+YEwTvj+1h1+8Zuzz0/G9+MvUCigNnWqQiDvzoUCocDmNe UTEBcYpoiIVYjn1hXxvZ60NjqP0aMrlYPBwe6KWeZFMZSCt9jbpbBZIpdqsnd18i+8lN qiR/lA8NVFDiBNcxe6OkFGY15IDtdw5dfRz4Abr0qjSjibCosV2Pn3okkGgrQ9ufmGKB jkL60dWVg00m0uIQ8E4Aa47R5IHp9kOx9miHUjYPI8z2FF/XzpdewI84G8HSxzuvA07l fj4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762029722; x=1762634522; 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=6Ee+svShMoEXU8pZ1fN6PWJ0g0ZjVivSjJEmRnPGg78=; b=qDDyKAkVeLLPxlbO47gZ/B/rfKrOWY//TxunQKK5aDwjn/5LdqpQeEu9f+cWyTdDcC h7uZ7QIlJ0bgzN3dA5/cRdG+8W/7bAtp7rfJ+iq/P8TgD0UaidUKkNHnbb2VgrH/i7Ni nqLiLso256n/2bsA6woNBwdNGojCQwW/tbWhzsuTxBTwAm7bbKUzar5907GH2A4zYcMj mUqU8roXDb1WoK2Nhdiq3ICKGFxwFrRzBGIpqfxPGGhDPfpINCy4h76rCfW4NOSyPA9M ttvAVBiPrD6ThgIrlugR2NGeLdlgV3xefuJQtRhmhDo7cj9oSWy1s+z1Rrs/VUVQingN pgDA== X-Forwarded-Encrypted: i=1; AJvYcCWbNHj7kpvL5gez3Di8NuWBojt4i59cnIaHlsrQiaz+p72XwxhOeW4/se2gCgMHxLvbGVA/Sj0YBhnslyS/@postgresql.org X-Gm-Message-State: AOJu0YwSBDLstQOovgRZA928Bb6bu8ESrFrLa9N7uGfHFYAcGJCGBfko TKUSew1sPaP7Yo2bphQ/JWcwmI5c80bX9ck1ccX2z1v1jEACd2TLG1uxTOo0nvb/Cc5cwLyIU+3 H9cU3pZr9Ot+gDeFzGe0BU5aaTPaqInc= X-Gm-Gg: ASbGnctzNj/yVlNvlype0CbYxpK33s2g0vRwXVB70yIFfaVz4ITD+cCCcNpPl/ZUwa8 +x/Q/bIEejv1KE5xYQAKEEefa5bR+fXRH1HNUAicNJ/+U/um/suJIc8KwFYemFw3sXN2TnYV5Uq 85RDuDPCOU9Ac+x1jyNMUmPd0CrFGgqBI+gtYxZyPodJq6hhSl4NNyKI9KakJG385n9Eiiod5Nd ThzrjGc0GiW41boHUu6RNEBkb1y9xz4sTq+xEenosVWwEB6cYBFTM6n8+UAwqqwoNSlr1PO X-Google-Smtp-Source: AGHT+IFpnKHLUt3aoKheyCdoNPDfLXY5wKmjRI+asIpIOCmCeLe2FNF/ibpMpVU7f9vFIcoe71OiON2zwwAXes7TplU= X-Received: by 2002:a05:690c:7288:b0:77c:afc9:b5b8 with SMTP id 00721157ae682-786484c3f07mr66092187b3.41.1762029722554; Sat, 01 Nov 2025 13:42:02 -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> <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> In-Reply-To: <290910DE-9A03-4AE6-B348-073D5DA96ACC@gmail.com> From: Arseniy Mukhin Date: Sat, 1 Nov 2025 23:41:51 +0300 X-Gm-Features: AWmQ_bmm7XjXnosXkJ_aHy81yx7ShwF8Evp_bM6NrNnFPor4OZieh9M2lYCOFEg Message-ID: Subject: Re: Optimize LISTEN/NOTIFY To: Chao Li 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 Hi, On Mon, Oct 27, 2025 at 2:25=E2=80=AFAM Joel Jacobson w= rote: > > On Sun, Oct 26, 2025, at 07:33, Joel Jacobson wrote: > > On Sun, Oct 26, 2025, at 05:11, Chao Li wrote: > >> I figured out a way to resolve the race condition for alt3: > >> > >> * add an awakening flag for every listener, this flag is only set by > >> listeners > >> * add an advisory pos for every listener, similar to alt1 > >> * if a listener is awaken, notify only updates the listener=E2=80=99s = advisory > >> pos; otherwise directly advance its position. > >> * If a running listener see current pos is behind advisory pos, then > >> stop reading > ... > > This sounds promising, similar to what I had in mind. I was thinking > > about the idea of using the advisoryPos only when the listening backend > > is known to be running (which felt like it would need another shared > > boolean field), and to move its pos field directly only when it's not > > running, since if it's running we don't need to optimize for context > > switching, since it's by definition already running. > > Write-up of changes since v20: > Thank you for working on this! There are few points about 'direct advancement' part: > Two new fields have been added to QueueBackendStatus: > + QueuePosition advisoryPos; /* safe skip-ahead position */ > + bool advancingPos; /* backend is reading the queue */ > > ... > > In SignalBackends, if a backend that is uninterested in our > notifications, has a shared pos that is at the old queue head, then we > will check if it's not currently advancing its pos, in which case we can > set its shared pos to the new queue head, i.e. "direct advance" it, > otherwise, if it's currently advancing its pos, and if its advisory pos > is behind our new queue head, we will update its advisory pos to our new > queue head. > > In asyncQueueReadAllNotifications, we start by setting wakupPending to > false and advisoryPos to true, to indicate that we've woken up, and that > we will now start advancing the pos. We also check if the pos is behind > the advisory pos, and if so use the advisory pos to update the pos. > > In asyncQueueReadAllNotifications's PG_FINALLY block, we reset > advancingPos to false, and detect if the advisoryPos was set by > SignalBackends while we were processing messages on the queue, and if > so, and if the advisoryPos is ahead of our pos, we update our shared pos > with the advisoryPos, and otherwise update the shared pos with the new > pos. Looks like the bug with truncating of the queue is gone, advancingPos does the trick, great. Maybe I missed something, but I failed to find an example where we can take advantage of advisoryPos: SignalBackends(void) ... if (QUEUE_POS_EQUAL(pos, queueHeadBeforeWrite)) { ... if (!QUEUE_BACKEND_ADVANCING_POS(i)) QUEUE_BACKEND_POS(i) =3D queueHeadAfterWrite; else if (QUEUE_POS_PRECEDES(advisoryPos, queueHeadAfterWrite)) QUEUE_BACKEND_ADVISORY_POS(i) =3D queueHeadAfterWrite; } We update advisoryPos if: 1) listener's advancingPos is true 2) listener's pos equals queueHeadBeforeWrite (1) means the listener is currently reading. (2) means notifications that the listener is currently reading belong to us (or it's even possible that the listener is reading notifications that were added in the queue after ours). And since the listener is reading, it will only see updated advancingPos in the PG_FINALLY, where listener's pos will already be >=3D queueHeadAfterWrite (as result of reading). This condition seems to be redundant. I would say it should always be true, otherwise it would mean that somebody allowed the listener to skip our notification. else if (QUEUE_POS_PRECEDES(advisoryPos, queueHeadAfterWrite)) Best regards, Arseniy Mukhin