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 1vDC2M-0053Re-Qu for pgsql-hackers@arkaria.postgresql.org; Mon, 27 Oct 2025 01:28:42 +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 1vDC2K-005jGK-EQ for pgsql-hackers@arkaria.postgresql.org; Mon, 27 Oct 2025 01:28:39 +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 1vDC2K-005jGB-10 for pgsql-hackers@lists.postgresql.org; Mon, 27 Oct 2025 01:28:39 +0000 Received: from mail-pj1-x1030.google.com ([2607:f8b0:4864:20::1030]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vDC2G-004OW5-0r for pgsql-hackers@postgresql.org; Mon, 27 Oct 2025 01:28:38 +0000 Received: by mail-pj1-x1030.google.com with SMTP id 98e67ed59e1d1-33bb1701ca5so3753223a91.3 for ; Sun, 26 Oct 2025 18:28:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1761528514; x=1762133314; 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=fI84BiThurFkwqn/RKQF/kgM9dztH0URYn5VcSRxypA=; b=J4MzF2NrXMVAJqdDhwR78titXqePkua6LvE0d6ilbhECS/0F/H74oqO/LtjwDuoQBf cfgLz2Q4HeDnNXBt1nmrj2+cfwVFNASUVHmjPhN5cYiR/roB0PgA3ItbX665fEhn2gia qhPtfc22LPhgIa2J7qkPKrbr61t3PwIxjinIXY3xsnSIismqqMdBvDBtu1EuCp4xrF1C DV3k1yl/JybT5VylZyUeG8iwCMfSmTfu3s1lquFSTjkFssBY+gGzP1D4vdakDujI1Jc5 aTloOPbVGBtst0PA/oBSKOjT06ylTn2zRKt8goIHH3GLMb0/+ZBbOU19flTEUH7dpXbG ccRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761528514; x=1762133314; 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=fI84BiThurFkwqn/RKQF/kgM9dztH0URYn5VcSRxypA=; b=bzDBgxqLC5/8GTrYCT8ZvCKfflxXPKJRhp5X+o9/QW5kvtAn8iLjmH4S6x9VdNPS8K KYO6BB1YzfkiRthl41Yg8CwmMTeI/grXOjaiKXaN7RqJL+WKxqqdZx8jwl6fn89eKfEL Fu0PTjctR+KVOPYtBWQxwiddmDt8SgXt+aYOUI6OgBHh9PCXjJgvA8kkZyOmVZue+Ohb AxVrMJ4W796EmjhXqJ89YVChWHMXUhSSFoy9X5UR6A/zygBHaC89D0nv1Mg/Fvvlj0W+ qICFmQmuJmXX8F5g878Xp6OP+2OmoIJHQSunHaqhT4zwAmn7c1nqEVOWs5K+wvP13PmK 8i2g== X-Gm-Message-State: AOJu0Ywc9GGGvtARcI89tuiee+CpdPMKIvN/T8xFV1FPeGnFv7xy9fCm h6gomkJ4QmxvU3OOM+d25EQ0IYxQcM99fUdEO/UYyo2Ax7sLn2dYUxNXEh2Jw+eU4nM= X-Gm-Gg: ASbGncv8W9fjrEn50uZlrqzV+WPhqkiDK1/k5dBYNpfGo+EOHLMKVgMlGuqiz7ibsHo dOurvVuWp9wNkTCNn6Jd7/8hekz4/8AvmSkg4oq4VAokxWPh5jUZta+ov1T8sfEo27h4oyPQlVa qrhcBGLhMDKVv/GLTKpsOZrG/ui1Bqdw1284CVw/fAzptM+5mBrPdCBXiShoY4vEkJ9p79+n2jE UnhODUnBK1xva10xrB3xsDzxndhwu1vFhT2xP4nJyGG4wzBe8zJt4xnso6B+IUw6+x4tGSkmfA9 0aHF7EqoAJduzOu7J5GtaIpufeGCVBuGqYQnt+C2AJ+hWAwqGIbivDOfFdPi2yqqS/oIOTpfCFe +xJl4EPTx6YBxSQzZTlljKTSZcVE3ZUCCo9SNQgkhI0iVMXHqEiFVV4DxxnYge++2hide69c552 /C0rD0ktqsv+r/wW05vb+Fow== X-Google-Smtp-Source: AGHT+IFXMXCHGcdXL8d4sl55dJfnmx8PIyujzX7miHS38oXZNes65RiD5GvZnD00Me0CWANSidOBIQ== X-Received: by 2002:a17:90a:ec8b:b0:32e:a10b:ce48 with SMTP id 98e67ed59e1d1-33bcf86c996mr48588339a91.12.1761528514013; Sun, 26 Oct 2025 18:28:34 -0700 (PDT) Received: from smtpclient.apple ([170.178.170.211]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-33fed80aa48sm6507774a91.13.2025.10.26.18.28.32 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 26 Oct 2025 18:28:33 -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: <38574cad-e90d-47b7-a015-753bb6bbc360@app.fastmail.com> Date: Mon, 27 Oct 2025 09:27:58 +0800 Cc: pgsql-hackers Content-Transfer-Encoding: quoted-printable Message-Id: <66631FB7-5BEA-4ED5-A694-9AD8B9CCFEE8@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> <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> 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 27, 2025, at 07:24, Joel Jacobson wrote: >=20 > Write-up of changes since v20: >=20 > Two new fields have been added to QueueBackendStatus: > + QueuePosition advisoryPos; /* safe skip-ahead position */ > + bool advancingPos; /* backend is reading the queue */ >=20 > These are used SignalBackends and asyncQueueReadAllNotifications to > handle the empheral state of the shared queue position, since we don't > take a lock while advancing it in asyncQueueReadAllNotifications. >=20 > In SignalBackends, we now don't signal laggers in other databases, > instead we will signal any listening backend that could possibly be > behind the old queue head, since we can't know if such backend is > interested in the notifications before the old queue head. Realistic > benchmarks will be needed to determine if this happens often enough to > warrant a more complex optimization, such as the ranges idea suggested > by Arseniy Mukhin. >=20 > 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. >=20 > 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. >=20 > 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. >=20 > = /Joel<0001-optimize_listen_notify-v21.patch><0002-optimize_listen_notify-v= 21.patch> I did a quick review on v21 only focusing on the =E2=80=9Cdirect = advancement=E2=80=9D logic. In v21, you added advisoryPos and advancingPos which is same as my = proposed solution. But you missed an important point from mine. Let=E2=80=99s say listener L1 is doing a slow advancing, because the = last notifier pushed a bunch of notifications and L1 is interesting in = them, say current QUEUE_HEAD is QH1. So, L1 is reading till reaching = QH1. Now notifier N1 comes. To N1, posBeforeWrite is QH1, and say = posAfterWrite is QH2. In this case, as L1 is reading, if N1 knows that = L1 will read till QH1, then N1 can still set L1=E2=80=99s advisoryPos to = QH2, right? =46rom this perspective, we need to add a new field = adviancingTillPos to QueueBackendStatus. (This field was also missing = from my proposed patch). Then notifier N2 comes after N1. To N2, posBeforeWrite is QH2, and say = posAfterWrite is QH3. As L1 is still reading, and it=E2=80=99s = advisoryPos is QH2, so N2 can also advance L1=E2=80=99s advisoryPos to = QH3. Finally, L1 finished reading and reached QH1. Now it sees advisoryPos is = QH3, then it can directly bump its pos to QH3. Do you think this logic is valid? Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/