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 1v9UFT-001I80-O1 for pgsql-hackers@arkaria.postgresql.org; Thu, 16 Oct 2025 20:06: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 1v9UFS-00FGjM-Ds for pgsql-hackers@arkaria.postgresql.org; Thu, 16 Oct 2025 20:06: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 1v9UFR-00FGjC-PX for pgsql-hackers@lists.postgresql.org; Thu, 16 Oct 2025 20:06:53 +0000 Received: from fhigh-a4-smtp.messagingengine.com ([103.168.172.155]) by makus.postgresql.org with smtp (Exim 4.96) (envelope-from ) id 1v9UFO-0029lE-2W for pgsql-hackers@postgresql.org; Thu, 16 Oct 2025 20:06:51 +0000 Received: from phl-compute-04.internal (phl-compute-04.internal [10.202.2.44]) by mailfhigh.phl.internal (Postfix) with ESMTP id C63B11400128; Thu, 16 Oct 2025 16:06:49 -0400 (EDT) Received: from phl-imap-03 ([10.202.2.93]) by phl-compute-04.internal (MEProxy); Thu, 16 Oct 2025 16:06:49 -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=1760645209; x=1760731609; bh=dLjlHoEoNqVBrZI4ehWdehJxhx+/m20NKHXH7/kWhls=; b= P6xXAC6asgneQI4D2g9FEOMUNGmO1dcuGHmye1Pv/uXQai9BnIqkJiy/dlCLFa7F wSajZ0njLv8N1mi25NJZ0//cvEaqgbOl5uP3hov5Z7ho/jHnzKOtR8C6ZeUiA8Zr 1bK0lJZaDU1l0qv98A8DQGBX0KGGPAyG5luJnMKfctaiNoIlJVyZLPGN3ekp9R0Q 1DGlsXZ/DFBzxIOOFkTuiJarskpnEfSwhETJewujvfEtZc2a/VSnlvQeH1/ZBHig N6oV1O7O8rmGbi3XjXcRQCo1Ymeet7vxTS8ifSeinZ2USQZAFpXCEzKF4d3V/epe BQmg3D6TIuKIh73xY9/ZVg== 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=1760645209; x= 1760731609; bh=dLjlHoEoNqVBrZI4ehWdehJxhx+/m20NKHXH7/kWhls=; b=M dK3/yJqvbcFNN+I+rf5/DwnMFW8kvKPSm+sJDLvYgoiZ48I+1XPS00HjVXf5bccz /W0LptFDkMVJLQFix1vMVEjFlJ1o406txBm+1lhJUW7PXx6+eRZ3uJWq2jE0RkEI GuX844UZG0jS9OQedyevLhTdoare6839CXCP1SWROceK/198dsn+RAimKazg4AUh QxHXGcvrnLLv7+066cQCAD/7qC2VESH6rci4f7nL55Qmzogh4vDXjJvqMHRDRAuq rmNkExybQdEov0hu5gJ131F5HGOxNZ8zRGQENUr4lPrPZxleJtAiBPTD3NzxClZ3 cFIVRyo/nEK33FnzDozMw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdeggdduvdejvddtucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucenucfjughrpefoggffhffvvefkjghfufgtgfesthhqre dtredtjeenucfhrhhomhepfdflohgvlhculfgrtghosghsohhnfdcuoehjohgvlhestgho mhhpihhlvghrrdhorhhgqeenucggtffrrghtthgvrhhnpedugeejgefgteetiefgffegtd efleffffekuefhleduuefgffeitdefledtvdeufeenucffohhmrghinhepghhithhhuhgs rdgtohhmnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomh epjhhovghlsegtohhmphhilhgvrhdrohhrghdpnhgspghrtghpthhtohepfedpmhhouggv pehsmhhtphhouhhtpdhrtghpthhtoheplhhirdgvvhgrnhdrtghhrghosehgmhgrihhlrd gtohhmpdhrtghpthhtohepphhgshhqlhdqhhgrtghkvghrshesphhoshhtghhrvghsqhhl rdhorhhgpdhrtghpthhtohepthhglhesshhsshdrphhghhdrphgrrdhush X-ME-Proxy: Feedback-ID: ic6394509:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id 5496318E0054; Thu, 16 Oct 2025 16:06:49 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface MIME-Version: 1.0 X-ThreadId: AZ7XRsRLdGms Date: Thu, 16 Oct 2025 22:06:29 +0200 From: "Joel Jacobson" To: "Chao Li" Cc: "Tom Lane" , pgsql-hackers Message-Id: In-Reply-To: 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-F22FC3429AE8@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 Thu, Oct 16, 2025, at 20:16, Joel Jacobson wrote: > On Thu, Oct 16, 2025, at 04:54, Chao Li wrote: >>> On Oct 15, 2025, at 23:36, Joel Jacobson wrote: >>> The latest version gets rid of GetPendingNotifyChannels() >>> and replaces it with the local list pendingNotifyChannels. >> >> Sorry for the typo, Yes, I meant to dynahash=E2=80=9D that you have a= lready=20 >> been using it. > ... >> My suggestion of using dynahah was for the same purpose. Because=20 >> list_member_ptr() iterates through all list nodes until find the=20 >> target, so this code is still O(n^2). >> >> Using a hash will make it faster. I used to work on project Concourse=20 >> [1]. The system is heavily using the LISTEN/NOTIFY mechanism. There=20 >> would be thousands of channels at runtime. In that case, hash search=20 >> would be much faster than linear search. >> >> [1] https://github.com/concourse/concourse > > Building pendingNotifyChannels is O(N^2) yes, but how large N is > realistic here? > > Note that pendingNotifyChannels is only the unique channels for the > notifications in the *current transaction*. At Concourse, did you real= ly > do thousands of NOTIFY, with unique channel names, within the same > transaction? I tested doing LISTEN ch1; LISTEN ch2; ... LISTEN ch100000; in one backend, and then \timing on BEGIN; NOTIFY ch1; NOTIFY ch2; ... NOTIFY ch100000; COMMIT; in another backend. Timing for the final COMMIT of the 100k NOTIFY: 2.127 ms (master) 1428.441 ms (0002-optimize_listen_notify-v19.patch) I agree this looks like a real problem, since I guess it's not completely unthinkable someone might have some kind of trigger on a table, that could fire off NOTIFY for each row, possibly causing hundreds of thousands of notifies in the same db txn. I tried changing pendingNotifyChannels from a list to dynahash, which improved the timing, down to 15.169 ms. Once we have decided which of the three alternatives to go forward with, I will add the dynahash code for pendingNotifyChannels. Nice catch, thanks. /Joel