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 1vBs9y-006ION-P3 for pgsql-hackers@arkaria.postgresql.org; Thu, 23 Oct 2025 10:03:06 +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 1vBs9x-007xAD-Dv for pgsql-hackers@arkaria.postgresql.org; Thu, 23 Oct 2025 10:03:04 +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 1vBs9w-007xA5-WF for pgsql-hackers@lists.postgresql.org; Thu, 23 Oct 2025 10:03:04 +0000 Received: from mail-yx1-xb129.google.com ([2607:f8b0:4864:20::b129]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vBs9t-003KUN-2k for pgsql-hackers@postgresql.org; Thu, 23 Oct 2025 10:03:02 +0000 Received: by mail-yx1-xb129.google.com with SMTP id 956f58d0204a3-63e2c7a3d10so660818d50.2 for ; Thu, 23 Oct 2025 03:03:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1761213781; x=1761818581; 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=oKDYJoKFTpGryf322biZm1ZrE38ONO6KiDd9c8GqzqU=; b=am2gmDFISWPMwqochMkUh0JTAjpII/UGf8R7kGy78M12y2CNG0NiiON/u+j4vyKY4k rtH2Xf10TKqbP15hUdvnyj9MkPH4kwumPXs8mil9h+Jcaaq4cpOTo/uJwdnbIqm4ISM4 /JeUQceJ93YxTAKK9rXth+mgQA5pkXmn070hbnotlFzcm9NoHN0zbk2nmATfO/3dn8Zh TFHT9XAOki+Rse+OvH8D5Rm0N6BaYzTVrIpAhbV3Sla2/flodWc6xmx9SkH+CmENF7Ke mYJG28u6Sho0xXoZTwd2NQOVg5IuP9fxXrskNF6M0lEeCuCJiOgWYq6mb7Dg7DRenuf2 hJZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761213781; x=1761818581; 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=oKDYJoKFTpGryf322biZm1ZrE38ONO6KiDd9c8GqzqU=; b=pCHDwBlmNok1lCbtLIoHtwg70tW1UsUv0FZVp4ghTR6b48F7oHqJFVynhwgkhaH559 1vrd3p7YO8pylh6Cv6ZUUbZAzqcOFi3ZZ4zPnTT9ghJNzKAj15USyvJ2vhCHjD2SzLlQ 0K57FX9nMedIlQx2H8hiRam+3RUSloay0S1EdQFLa4ypOI9CxNjQJAvB9RR1LWB6a9a4 YVLUwixK8PLaZbCeKG/n2SDSBCWRg0u0GiHI3Hkb8yw3xSML2pxRBnajJWeEIkrd5JJV Jwf2xrQwVxF47h2fOlrag/Bq+o7exDFkQ6fWEV8SBvrSI4DOkb3xcZ0TbDRJGAxrgvdH RwEA== X-Forwarded-Encrypted: i=1; AJvYcCW+WL6eODTL6cSTbi0EDgqJ3iykV1R2TSsgOTI52E77j6sTYVEuUvmzynKBA9pVlXzM5hEfbsmpq89a0UQ+@postgresql.org X-Gm-Message-State: AOJu0YxR/f2gvoEtS02NgFmawQ8DDuNQAvZ8xoD7Yya2dlixCT/3pLfd pGc89aBHZ2DS4nMzWN9ogxsLCotxOsLBqv+PxvAC5BnTKd+dZRKmawyaMH1gduwWrSJ6XAGOpFD Hhxgc8iprJAWXmK9411znSJ+JdcD65Vw= X-Gm-Gg: ASbGncvD1BCeMtnKSdr8cGURLwwUpBnVNQL6EmWdJsIVlbBno7tB6NmvkLMeOiU63Rm xLDd+pRYnKILLBh6UCtZpVKaoewbqgoZjSSxszYnksZ1bqkoPRauNmJYwRSdTFhkeTIbTiIzGEI pSrK7AT6Ahq4Ug5lRs1CJRrLYrgB2U+QXwr4N9oZ0nhJzOYwTHmM0y0r/mOAChZZeD4Z/GL7mal LfhaK5x/hGsAcfJHoOOKeuWRloE9CU0Me0Aj4vgIHkB1H8S7jGEjkMLAhZmcWos9yo8hka5 X-Google-Smtp-Source: AGHT+IFlllc3dFwNTZvfzxPDa2mbF637nltoWCYBy4SqgP0m1aC+HEy2IESEShEwYwZUD3NxM4CJ5CUT37StH/iJvqs= X-Received: by 2002:a05:690e:1914:b0:63e:2e7e:ca35 with SMTP id 956f58d0204a3-63e2e7ecf4emr12913516d50.37.1761213780888; Thu, 23 Oct 2025 03:03:00 -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> In-Reply-To: <14865EB6-0BF4-462B-9072-10BDAC10C052@gmail.com> From: Arseniy Mukhin Date: Thu, 23 Oct 2025 13:02:49 +0300 X-Gm-Features: AS18NWCfzAk9CHslhT_VKHBOKaU_XAKxY3_RYIpfWxGH6xxT12CEPW_dEJu8xG0 Message-ID: Subject: Re: Optimize LISTEN/NOTIFY To: Chao Li Cc: Joel Jacobson , Tom Lane , 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 Thu, Oct 23, 2025 at 11:17=E2=80=AFAM Chao Li w= rote: > > > > > On Oct 21, 2025, at 00:43, Arseniy Mukhin wrote: > > > > > > I managed to reproduce the race with v20-alt3. I tried to write a TAP > > test reproducing the issue, so it was easier to validate changes. > > Please find the attached TAP test. I added it to some random package > > for simplicity. > > > > With alt3, as we have acquired the notification lock after reading every = message to update the POS, I think we can do a little bit more optimization= : > > The notifier: in SignalBackend() > * Now we check if a listener=E2=80=99s pos equals to beforeWritePos, = then we do =E2=80=9Cdirectly advancement=E2=80=9D > * We can change to if a listener=E2=80=99s pos is between beforeWrite= Pos and afterWritePos, then we can do the advancement. > > The listener: in asyncQueueReadAllNotifications(): > * With alt3, we only lock and update pos > * We can do more. If current pos in shared memory is after that local= pos, then meaning some notifier has done an advancement, so it can stop re= ading. > I think this would be a reasonable optimization if it weren't for the race condition mentioned above. The problem is that if the local pos lags behind the shared memory pos, it could point to a truncated queue segment, so we shouldn't allow that. > I tried to run your TAP test on my MacBook, but failed: > > ``` > t/008_listen-pos-race.pl .. Dubious, test returned 32 (wstat 8192, 0x2000= ) > No subtests run > > Test Summary Report > ------------------- > t/008_listen-pos-race.pl (Wstat: 8192 (exited 32) Tests: 0 Failed: 0) > Non-zero exit status: 32 > Parse errors: No plan found in TAP output > Files=3D1, Tests=3D0, 3 wallclock secs ( 0.01 usr 0.01 sys + 0.10 cusr= 0.29 csys =3D 0.41 CPU) > Result: FAIL > ``` > > I didn=E2=80=99t spend time debugging the problem. If you can figure the = problem, maybe I can run the test from my side. > Thank you for trying the test. I think the test works for you as expected, it should fail with error and I have the same error status. Sorry, I failed to realize it could be confusing, probably it was better to fail on some assert instead, but I thought error is enough for temp reproducer. Please see 008_listen-pos-race_test.log for details. Best regards, Arseniy Mukhin