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 1vE0GH-002pB7-L6 for pgsql-hackers@arkaria.postgresql.org; Wed, 29 Oct 2025 07:06:25 +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 1vE0GG-00GqEn-K3 for pgsql-hackers@arkaria.postgresql.org; Wed, 29 Oct 2025 07:06:23 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1vE0GG-00GqEe-9X for pgsql-hackers@lists.postgresql.org; Wed, 29 Oct 2025 07:06:23 +0000 Received: from mail-pl1-x62f.google.com ([2607:f8b0:4864:20::62f]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vE0GC-004pvu-2n for pgsql-hackers@postgresql.org; Wed, 29 Oct 2025 07:06:23 +0000 Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-29292eca5dbso89866645ad.0 for ; Wed, 29 Oct 2025 00:06:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1761721577; x=1762326377; darn=postgresql.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=8CbcgTuBICZD1rmAUOSpilOEJyF/MozAWMlHn8q3Qh4=; b=Zcn6z7IrVF7UottQLFv+xbbMxLuI6fSCDrK6nn+75ls2ddj5KozYHkHxj/IsCCfBel 3cfSDvNsxHy6OEwbL0nUCLLfgEnTe7W7DKJ9QIpB4WxHuIsKCYm4zvFmgyLaFws/L9JQ aH3akImoWf35WivxSPivWED0fOk3Bhi9HfhJNcCQf4lj5t8SpiyC5DNTfjjE7H/KLtym 6BRWjOg9I+Ju1hLN7MBBnHdcXM/yaTyvUVyVVSYhfzX9GvC1e9nX3YCotF3qp7Ndv1ka fSnAaU989Y30KTcnQLeKgTFiApZdH48OHX/Y15VDiCSTGejASLUm9SX1N92P6wZVFsxS 4/pA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761721577; x=1762326377; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8CbcgTuBICZD1rmAUOSpilOEJyF/MozAWMlHn8q3Qh4=; b=jeEMK/o6zGYH0rqKtNyvuAgXchQ7nIobu0Fv2rFAfuZZKpwMzzo9vnTG1nqCHcAOnd eX7cBUgk86R0Gz4FEgAJ0KniEQmVQa8VvEbm8TDhamzM4p0WDynjKTnLiTGkx2PRXu8t N2T1NvJOFuK9vU0ViteqEkkWcxPahuQ5lo4masLOw1D67m3HtgYUAl2VENkrpvzPBRLz uRcTyiy1868a1mjEMl4S+Bb6zMOgP41u7xIMRcZPXgj9tPi/rFYbWCL/7PwOHPqjdiGf C0gq2Hnz/MLK2AbnnyesAJGH/ffAlXOPk/7bxqxiWStMPjIl6bxsNB517SAuOmFs+bKk CcYA== X-Gm-Message-State: AOJu0YyM51bANhFXKB1ba7l82AZve6OeLy8DVoINxZ02uVcgMsoSlone PNy6lmQK3BbxyfRVMVzsdWKTkE6vEj5Uq7nFpCraBX/j59fNMrNeuQTYxvQRU+xjMFs= X-Gm-Gg: ASbGnctLNdaiui1Hrhqi6sdXZajbV8VJfSYzFFhcvr3fmgYqlignwZwllXzXoeuPfsK R9ZjnLZe+WSD9+DS+r2pbUQXv/Es1Q6V1yvrDpp9dIjvx1e1VGOLW3RPedxFrdoJ0A0xqJ5iTmC Iof5kAm7Zl8Vc0sy30XvR5PCd2Q2Hz9i2YeKvp0kuGzQ6VHic9VcbxnWue4haI0pThvgXIm/6FN UI2wdSI64mI6IGjCOvD4Fm7U2JV1gzUL6yzWdbE8KZH/twWMSSjTv/HgY3EJLXuRQOie5KG77LJ /JZrMrs4DfAPvJz1KpnApdGUU6c3MSnW8tU5Z6TOMLeZGvZ7cCRGPq/O1zwlZoxjS7eEFtT3BNu 09cK3v4tD1tZP7zXrW+nHVcx6OEsGG7dHxfQWD4bEtaUqj8NeQ3sKTjb6krlid19xsZWHAWo5SE VFRrjBgeg6s1TbaPZh4BeF X-Google-Smtp-Source: AGHT+IEuUx4ZNKym5uAr1bY7IHf+ej4DyAJ/OB2lfe0T0QuijgXgT7j7S1n64NV035EN3XFDCW7hGg== X-Received: by 2002:a17:903:1250:b0:269:d978:7ec0 with SMTP id d9443c01a7336-294deec53dbmr23643375ad.28.1761721577327; Wed, 29 Oct 2025 00:06:17 -0700 (PDT) Received: from smtpclient.apple ([196.247.24.158]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-29498cf4b0csm138499075ad.10.2025.10.29.00.06.15 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 29 Oct 2025 00:06:16 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.700.81\)) Subject: Re: Optimize LISTEN/NOTIFY From: Chao Li In-Reply-To: Date: Wed, 29 Oct 2025 15:05:42 +0800 Cc: pgsql-hackers Content-Transfer-Encoding: quoted-printable Message-Id: 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> To: Joel Jacobson X-Mailer: Apple Mail (2.3826.700.81) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On Oct 29, 2025, at 05:45, Joel Jacobson wrote: >=20 > On Tue, Oct 28, 2025, at 07:46, Chao Li wrote: >>>> But anyway, we should run some load tests to verify every solution = to=20 >>>> see how much they really improve. Do you already have or plan to = work=20 >>>> on a load test script? >>>=20 >>> Yes, I'm currently working on a combined benchmark / correctness = test suite. >>>=20 >>=20 >> Cool. Then we can run the benchmark and decide. >=20 > I found a concurrency bug in v21 that could cause missed wakeup when a > backend would UNLISTEN on the last channel, which called > asyncQueueUnregister, and if wakeupPending was at that time already = set, > then it wouldn't get reset, since in ProcessIncomingNotify we return > early if (listenChannels =3D=3D NIL), so we would never clear = wakeupPending > which happens in asyncQueueReadAllNotifications. >=20 > Fixed by clearing wakeupPending in asyncQueueUnregister: >=20 > @@ -1597,6 +1597,7 @@ asyncQueueUnregister(void) > /* Mark our entry as invalid */ > QUEUE_BACKEND_PID(MyProcNumber) =3D InvalidPid; > QUEUE_BACKEND_DBOID(MyProcNumber) =3D InvalidOid; > + QUEUE_BACKEND_WAKEUP_PENDING(MyProcNumber) =3D false; > /* and remove it from the list */ > if (QUEUE_FIRST_LISTENER =3D=3D MyProcNumber) > QUEUE_FIRST_LISTENER =3D QUEUE_NEXT_LISTENER(MyProcNumber); >=20 > = /Joel<0001-optimize_listen_notify-v22.patch><0002-optimize_listen_notify-v= 22.patch> I think the current implementation still has a race problem. Let=E2=80=99s say notifier N1 notifies listener=E2=80=99s L1 to read = message. L1 starts to read: it acquires the look, gets reading range, then = releases the lock, start performs reading without holding the lock. Notifier N2 comes, N2 doesn=E2=80=99t have anything L1 is interested in. = N2 now holds the look, when it checks "if (QUEUE_POS_EQUAL(pos, = queueHeadBeforeWrite))=E2=80=9D, here comes the race. Because the lock = is in N2=E2=80=99s hand, L1 cannot get the lock to update its pos, so = "if (QUEUE_POS_EQUAL(pos, queueHeadBeforeWrite))=E2=80=9D will not be = satisfied, so direct advancement won=E2=80=99t happen. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/