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.96) (envelope-from ) id 1wh7gC-00013i-1r for pgsql-hackers@arkaria.postgresql.org; Tue, 07 Jul 2026 15:25:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wh7gA-001Iyr-1W for pgsql-hackers@arkaria.postgresql.org; Tue, 07 Jul 2026 15:25:47 +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.96) (envelope-from ) id 1wh7gA-001Iya-07 for pgsql-hackers@lists.postgresql.org; Tue, 07 Jul 2026 15:25:46 +0000 Received: from mail-ed1-x529.google.com ([2a00:1450:4864:20::529]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wh7g8-000000000Jx-2N1R for pgsql-hackers@lists.postgresql.org; Tue, 07 Jul 2026 15:25:45 +0000 Received: by mail-ed1-x529.google.com with SMTP id 4fb4d7f45d1cf-698ab9aae16so7602548a12.3 for ; Tue, 07 Jul 2026 08:25:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1783437942; cv=none; d=google.com; s=arc-20260327; b=TTWI5GRJJctjPaXW2amocSjTx9Y2U6vc9qWfZQEN3WTCn9qLuhgB4c9xjYQhlCPovJ XexHBHARcLGyozu8Y6ltRVVEbTyUvkeZS82PnGBvtPYOJDCTPSYEV6wlvNkwh4S1K5+x 01U+7tOEBm+AwWw41RUTxF29o+Oiw5jzGDPlVclDP/VhEtMJccB1yUMXVL7ywITehKed fhref7DT+sNuq0PgD0UxnikD7HZWJn5d5+qSFsFTcskxoSQi7yqDcwEQT4hbcgkkYVlZ a79CksIZMb6JJ38SF3IbsqmhNmf9LFIoL2xj8/vzZo/n/uQd8Qhhx4+O76yjamjiANx+ dYpQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=PpU7xpEE90hVjjvnsQF4hAzQ5sYF1xUjhkC8zmg+m3o=; fh=n3Lh2rntepbG8aq0MQ+cP92+AwOAezpbOR/FyKNJLdU=; b=g8ZPlbD70I+0rO2J+/hryQOW5sKzv6MiOuJtuSuf0dY8P9Yw/gMT3qGQ5zK4dYIyar i0+2auuQbALPhx/1yANmc9XhtVVGHcTR3NPLlwypCBtsLTptBrSAhpFxAjOFN1d74o0O vi5NuZfQtBXNESFW4WSg10wJxOGvhVQ8M15IKSR4gUfER/V0kTqEaTFKL6EDSyhB2RHn wIqFp/nCgyei2pekOuEJuN2kVmumxPaERysJjE500AO+KzRlkD6d9U23ddzd0q7EfXWa mHLQ3djp4Kka0pK3GUSCPKnNCpeiHGg0QWvIgr9TJzS135R/lJJgMrNbz3WilnGRKtx/ jlUg==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783437942; x=1784042742; darn=lists.postgresql.org; h=content-transfer-encoding:content-type:cc:to:subject:message-id :date:from:in-reply-to:references:mime-version:from:to:cc:subject :date:message-id:reply-to:content-type; bh=PpU7xpEE90hVjjvnsQF4hAzQ5sYF1xUjhkC8zmg+m3o=; b=CpzDLeg2S6fkBfNkRZ9s5YtoQ5dPXVSjg+Wxf2TLmS5FrDkWm8hHvDZYQUafuR2e/z U67XMrTG6pPij2wiimr3jlIFEVTC6AIGo5GFNao3RE26VzNxmq2qUUoJ1Eirx0GU6Auq /CH+d1USG/RQ9kLDFcuu/MFnoSqnsCIeIT5bla57k2D+8mfozk3Ct76QExGJ3MmVCuxw ClM3YbvlCe0U3/gNpF0aQdx/Lb0x4UYDZe+VWsy2/MGhIrT3zFezPhdHGjFhdCQuJOCi J3HvemuQlDY8tWT21GvXF4QLHpcGTSGCf+SPajwjR2h8QXYPiY66t3ft//C7eefob0pP pfmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783437942; x=1784042742; h=content-transfer-encoding:content-type:cc:to:subject:message-id :date:from:in-reply-to:references:mime-version:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=PpU7xpEE90hVjjvnsQF4hAzQ5sYF1xUjhkC8zmg+m3o=; b=MNivqtBz0mcm1mN8tfMg1SJVqT0ukPGZT1ttjK0ndmLb0PiGu2H29MnMbluKFVuYuQ 1jT0/MIwXxvfDzgpXNCjWI4sUP2Lgk470gK2+nT+Gblt2qrsc2NlCb6E12P4YB4eNQUX 2gVe0EgE4vtdxVG52yLgWHhtc85gpuo+CA59syMAK6edcrJbs3wo3j68ntBZjhoZn305 I0XAj53Ri6aUuHJO/9Xb0myaRdbDL0iY4mn5GGEE0EkNWTA3dQDeAQecSZUC8Lyu7hg2 AeetH5ozghI5sQoFqSMVklnZ0Pk/pvvFT7JJB9vhA0N6CmrTRZbCUQrWECGWwzHsVGHf 6+Wg== X-Forwarded-Encrypted: i=1; AHgh+RqUOQ/p9NjsqMpfwoK9PYdoRHqakQwN0qGM9yvmwisjK7rt1Oxircrjhpm7s6AZO7/trESDLi27m/cof+Q3@lists.postgresql.org X-Gm-Message-State: AOJu0Ywr6zyQBtsBtXLOV66lRZRM8MNcbtZaZWht5S1al47KZbcRx5jR IlyvYQ92M25HsXyJTpl5vxC3ThuqTm4j6TF2/SaR9A+eKFHt18lIdDjJnDAExPFgx5VjUGOVsEI +Vy0u/qlh/ldsw2pn92Haa6D8AZF9eXE= X-Gm-Gg: AfdE7cl08Cn+eeH/TargAEi+0E13SGr3AwOxUfY1AoCVi5QQyjN/Ui5FgEFQZdytq9Y CZMDMerTLxA1fUCxmV0CGzQwwUSCqnz9X7sKn36tyWVqwM8baYCFFsx1fx8/K3clceLpl8IlXSr fnsBAtu6zexodqxaP+eQTecuJudpU88ihRo2UJwcphfnambRBofua4rNip/uu/zC8XEHp1jS/0R hnWniPCJu5xB21xAvHcNmcR1UpkCdJA+wZVu0FD2sUMVBLdWUAO0b7KUd8rAqNZNWvFK0w6oyEM 0RoEcRD7qiH+GlfwFzUoQ/821tliS93KySETfjojQN3NGST32aEAh7tuNwoh8I7eZYK7eBU/4hT ARK8t6vG8uqr5DwGlta7/TEDI X-Received: by 2002:a05:6402:a2cf:10b0:699:12e8:1c54 with SMTP id 4fb4d7f45d1cf-69a856a7ca5mr2356250a12.5.1783437942135; Tue, 07 Jul 2026 08:25:42 -0700 (PDT) MIME-Version: 1.0 References: <1959506.1775527693@sss.pgh.pa.us> In-Reply-To: From: Alexander Korotkov Date: Tue, 7 Jul 2026 18:25:28 +0300 X-Gm-Features: AVVi8Ccdz2DaiqkLLw-ro60kR_aGFX6UtvMSuh9vC_9p9ufE9DWfFy-3q0W0qFE Message-ID: Subject: Re: Implement waiting for wal lsn replay: reloaded To: Heikki Linnakangas Cc: Xuneng Zhou , Andres Freund , Tom Lane , Peter Eisentraut , Thomas Munro , =?UTF-8?Q?=C3=81lvaro_Herrera?= , Chao Li , pgsql-hackers , Michael Paquier , jian he , Tomas Vondra , Yura Sokolov 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, Heikki! On Mon, Jul 6, 2026 at 2:04=E2=80=AFPM Heikki Linnakangas = wrote: > On 01/05/2026 05:44, Xuneng Zhou wrote: > > On Wed, Apr 29, 2026 at 5:01=E2=80=AFAM Alexander Korotkov > > wrote: > > > >> LGTM, I've added some comments for new functions in 0006. I propose > >> to push this patchset. Probably something is still missing and we > >> will have to go back to this. But it seems to make a lot of aspects > >> much better. > > > > I reviewed the patchset and found a potential issue in the test for > > patch 5, similar to the log-checking problem in the cascading timeline- > > switch test. I've applied a minor fix to address it. Other parts LGTM. > I happened to look around this code now. To recap, the code in the main > WAL redo loop now looks like this: > > > > > /* > > * Apply the record > > */ > > ApplyWalRecord(xlogreader, record, &replayTLI); > > > > /* > > * Wake up processes waiting for standby replay, = write, or flush > > * LSN to reach current replay position. Replay = implies that the > > * WAL was already written and flushed to disk, s= o write and flush > > * waiters can be woken at the replay position to= o. > > */ > > WaitLSNWakeup(WAIT_LSN_TYPE_STANDBY_REPLAY, > > XLogRecoveryCtl->lastRe= playedEndRecPtr); > > WaitLSNWakeup(WAIT_LSN_TYPE_STANDBY_WRITE, > > XLogRecoveryCtl->lastRe= playedEndRecPtr); > > WaitLSNWakeup(WAIT_LSN_TYPE_STANDBY_FLUSH, > > XLogRecoveryCtl->lastRe= playedEndRecPtr); > > That's not wrong, but I've got some comments: > > 1. It's reading XLogRecoveryCtl->lastReplayedEndRecPtr without a lock or > atomics. That's ok, no other process modifies lastReplayedEndRecPtr, but > it feels a little dirty. > > 2. We're now doing three extra function calls on every WAL record. This > is a very hot path, and most of the time, we'll just take the fast path > in WaitLSNWakeup to return without doing anything. Andres and others > assumed up-thread that it's negligible (we used to have pre-checks here > in the caller), but I wonder if you did any performance testing? > > 3. There are other "wakeup" calls inside ApplyWalRecord(), to wake up > walsenders and walreceivers. They could perhaps use the same wait-lsn > machinery now, but that's v20 material. However, I think these > WaitLSNWakeup() calls should also be moved inside ApplyWalRecord(), so > that we'd have all the wakeup actions in one place. > > 4. Once you move those calls inside ApplyWalRecord(), like this: > > > @@ -1979,20 +1979,30 @@ ApplyWalRecord(XLogReaderState *xlogreader, XLo= gRecord *record, TimeLineID *repl > > /* > > * Update lastReplayedEndRecPtr after this record has been succ= essfully > > * replayed. > > */ > > SpinLockAcquire(&XLogRecoveryCtl->info_lck); > > XLogRecoveryCtl->lastReplayedReadRecPtr =3D xlogreader->ReadRec= Ptr; > > XLogRecoveryCtl->lastReplayedEndRecPtr =3D xlogreader->EndRecPt= r; > > XLogRecoveryCtl->lastReplayedTLI =3D *replayTLI; > > SpinLockRelease(&XLogRecoveryCtl->info_lck); > > > > + /* > > + * Wake up processes waiting for standby replay, write, or flus= h LSN to > > + * reach current replay position. Replay implies that the WAL = was already > > + * written and flushed to disk, so write and flush waiters can = be woken at > > + * the replay position too. > > + */ > > + WaitLSNWakeup(WAIT_LSN_TYPE_STANDBY_REPLAY, xlogreader->EndRecP= tr); > > + WaitLSNWakeup(WAIT_LSN_TYPE_STANDBY_WRITE, xlogreader->EndRecPt= r); > > + WaitLSNWakeup(WAIT_LSN_TYPE_STANDBY_FLUSH, xlogreader->EndRecPt= r); > > + > > /* ------ > > * Wakeup walsenders: > > * > > * On the standby, the WAL is flushed first (which will only wa= ke up > > * physical walsenders) and then applied, which will only wake = up logical > > * walsenders. > > It becomes clear that you don't actually need the memory barrier inside > WaitLSNWakeup(). Not sure if they're needed for other callers, but here > we have just released a spinlock, which acts as a memory barrier. It > might not be worth relaxing, but it does seem a little silly. > > > If nothing else, I'd like to move those calls into ApplyWalRecord() for > clarity (point 3 above). What do you think? Thank you for bringing this up. +1 for moving calls into ApplyWalRecord() Regarding the overhead. I've tried to apply 158.6 MB of pgbench generated WAL on my local Macbook M3 Pro. * With calls, sec: 1.95/1.86/1.91/1.85/1.90 * Without calls, sec: 1.83/1.87/1.96/1.90/1.85 So, the difference is less than the measurement error. Do you prefer to commit the movement of the calls yourself, or do you prefer me to do it? ------ Regards, Alexander Korotkov Supabase