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 1v93YU-009HNV-SM for pgsql-hackers@arkaria.postgresql.org; Wed, 15 Oct 2025 15:36:46 +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 1v93YS-006yOA-Dd for pgsql-hackers@arkaria.postgresql.org; Wed, 15 Oct 2025 15:36:43 +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 1v93YR-006yNz-Lp for pgsql-hackers@lists.postgresql.org; Wed, 15 Oct 2025 15:36:43 +0000 Received: from fhigh-b8-smtp.messagingengine.com ([202.12.124.159]) by makus.postgresql.org with smtp (Exim 4.96) (envelope-from ) id 1v93YO-001vHt-2C for pgsql-hackers@postgresql.org; Wed, 15 Oct 2025 15:36:41 +0000 Received: from phl-compute-04.internal (phl-compute-04.internal [10.202.2.44]) by mailfhigh.stl.internal (Postfix) with ESMTP id 2B8257A00EC; Wed, 15 Oct 2025 11:36:39 -0400 (EDT) Received: from phl-imap-03 ([10.202.2.93]) by phl-compute-04.internal (MEProxy); Wed, 15 Oct 2025 11:36:39 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=compiler.org; h= cc:cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm2; t=1760542599; x=1760628999; bh=f1AcFI5XJvtg+CaUL7+fPxWCY7S+GAQ3DH3Rm+bDZck=; b= J4HMUp/vT3L9WHSio7h2enmyXNnxj+8KjuhRcqSi0vKKLu3hnQIxeAtKzMSD1lCx AVYajUPH+rNlhM2OMXS9yTaOOl2r5Sk0tagoh/av/dRVsDpni+Y4MqyVNVmw3pg6 GnvjdNxLHmkqEXX3Q3eObQR68WnYnMNn2CajoBiYaE9ExU4GFaPTDKcx8pVIEiWd vzBUy9EimoOOFT+Hoa17G+i4B2Phy/w7eVXBKEwMEsyw2CqBVRkNiwFeBJ2V6TtI TY/loVdY9YoAP00VZr11B+2kCCCQXAXK+gdvfRLRk/rIt+1JTSHKxlThRHnCos2K LDdVL9XZSjXLCBzlAKwzpQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1760542599; x= 1760628999; bh=f1AcFI5XJvtg+CaUL7+fPxWCY7S+GAQ3DH3Rm+bDZck=; b=w w2n8Kc/twty1fGnYkGgTzJWDtaVeOKBgF7pM4YYiZg4P5NotSGkfMDuUIIsvRvDV HQKR9vES5k0KIWgLRj9bbCR8XoiriDjGgPTMN3WlT1lYlHaLVSrNOfBg4iPN3lAo pQTY8i/CcVbiNbhA9bnuy9slkmWIJmPkeK2olG2fd/nXrO1VdcEJ0fUDFcALLyff 1qNLhXEuuG88ufrZybTp6XXVLDb+c7f8Kzeyibp/ZaxrW8Iaxr1QlKkIlYxPOk3J bIRJElSiK8yjE+eT8c3LD/FJ9cnUeeyv+G3dmOsg/q6THI2pNOEDRpVK62tHQ2Na 7LKkGKk5jynJVWO7HLsIw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdeggdduvdefjeekucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucenucfjughrpefoggffhffvvefkjghfufgtgfesthhqre dtredtjeenucfhrhhomhepfdflohgvlhculfgrtghosghsohhnfdcuoehjohgvlhestgho mhhpihhlvghrrdhorhhgqeenucggtffrrghtthgvrhhnpefggfeuheefheeufeegkeeife ffhffgvefftdduuefhfeelteegleehhfdtieevgfenucevlhhushhtvghrufhiiigvpedt necurfgrrhgrmhepmhgrihhlfhhrohhmpehjohgvlhestghomhhpihhlvghrrdhorhhgpd hnsggprhgtphhtthhopeefpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehlihdr vghvrghnrdgthhgrohesghhmrghilhdrtghomhdprhgtphhtthhopehpghhsqhhlqdhhrg gtkhgvrhhssehpohhsthhgrhgvshhqlhdrohhrghdprhgtphhtthhopehtghhlsehsshhs rdhpghhhrdhprgdruhhs X-ME-Proxy: Feedback-ID: ic6394509:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id 6FD0118E0054; Wed, 15 Oct 2025 11:36:38 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface MIME-Version: 1.0 X-ThreadId: AZ7XRsRLdGms Date: Wed, 15 Oct 2025 17:36:11 +0200 From: "Joel Jacobson" To: "Chao Li" , "Tom Lane" Cc: pgsql-hackers Message-Id: <0a5a20d3-4621-46b3-b2ab-903f63a20dea@app.fastmail.com> In-Reply-To: <1F7227F5-C33D-4E2C-8511-33F1468590D0@gmail.com> 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> Subject: Re: Optimize LISTEN/NOTIFY 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 05:19, Chao Li wrote: > * B enters PreCommit_Notify(), it gets the NotifyQueueLock first, it=20 > records headBeforeWrite =3D 1 and writes to 3, and records headAfterWr= ite=20 > =3D 3. > * Now QueueHead is 3. > * C enters PreCommit_Notify(), it records headBeforeWrite =3D 3 and=20 > writes to 5, and records headAfterWrite =3D 5. No, when C enters PreCommit_Notify, it will be waiting on the heavyweight lock, currently held by B, which B will hold until it commits. It will then see headBeforeWrite =3D 3. > * Now QueueHead is 5 > * C starts to run AtCommit_Notify(), as A=E2=80=99s head is 1, doesn=E2= =80=99t equal=20 > to C=E2=80=99s headBeforeWrite, C won=E2=80=99t advance A=E2=80=99s he= ad. > * A starts to run AtCommit_Notify(), A=E2=80=99s head equals to B=E2=80= =99s=20 > beforeHeadWrite, B will advance A=E2=80=99s head to 3. No, like explained above, B cannot be running here, it must have committed already (or aborted) since C was waiting on the heavyweight lock held by B. The example therefore seems invalid to me. > I agree with Tom that GetPendingNotifyChannels() is too heavy and unne= cessary. > > In PreCommit_Notify(), we can maintain a local hash table to record=20 > pending nofications=E2=80=99 channel names. dahash also supports hash = table in=20 > local memory. I'm confused, I assume you mean "dynahash" since there is no "dahash" in the sources? I see dynahash has local-to-a-backend support, but I don't see why we would need a hash table for this, we just iterate over it once in SignalBackends, I think the local list is fine. The latest version gets rid of GetPendingNotifyChannels() and replaces it with the local list pendingNotifyChannels. > And the local static numChannelsListeningOn is also not needed. We can=20 > get the count from the local hash. No, you're mixing up the data structures. The local hash you suggested was for pending notify channels, but numChannelsListeningOn was needed when we didn't have listenChannels. Now that I've reverted back to listenChannels, I also replaced `(numChannelsListeningOn =3D=3D 0)` with `(listenChannels =3D=3D NIL)`. > WRT to v6, I got a few new comments: ... > In this comment, you refer to =E2=80=9CchannelHash=E2=80=9D and =E2=80= =9Cthe shared channel=20 > hash table=E2=80=9D, they are the same thing, but easy to make readers= to=20 > misunderstand. Right, will try to improve this in the next version. > pg_listening_channels(PG_FUNCTION_ARGS) > { > FuncCallContext *funcctx; > + List *listenChannels; ... > listenChannels is only used within the =E2=80=9Cif=E2=80=9D, so it=E2=80= =99s definition can be=20 > moved into the =E2=80=9Cif=E2=80=9D. Comment not applicable since local variable listenChannels has now been removed from pg_listening_channels, now using the original static listenChannels instead. > + /* Check for lagging backends when the queue spans multiple pages */ > + if (queue_length > 0) ... > I wonder why this check is needed. If queue_length is 0, can we return=20 > immediately from SignalBackends()? This check has been removed in the latest version. /Joel