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 1wgh7Z-006L2z-2X for pgsql-hackers@arkaria.postgresql.org; Mon, 06 Jul 2026 11:04:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wgh7Y-00H9zS-1u for pgsql-hackers@arkaria.postgresql.org; Mon, 06 Jul 2026 11:04:16 +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 1wgh7Y-00H9zF-0a for pgsql-hackers@lists.postgresql.org; Mon, 06 Jul 2026 11:04:16 +0000 Received: from lahtoruutu.iki.fi ([185.185.170.37]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1wgh7W-00000001otg-0TwX for pgsql-hackers@lists.postgresql.org; Mon, 06 Jul 2026 11:04:15 +0000 Received: from [10.0.2.15] (unknown [130.41.208.2]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: hlinnaka) by lahtoruutu.iki.fi (Postfix) with ESMTPSA id 4gv1h92CXdz49Q4q; Mon, 06 Jul 2026 14:04:09 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1783335851; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wY9tzQmeaU/dJ3674H+1zm7QNuaGUPzCFc7JFrRz7f4=; b=sQK9ZVBSoYZAfmoJZN59o3NX6JCxAJSrJE5H6ByGkowXGRhVGx+ueeakKWqUlbfy2lSmdh +vcUizhWpqvDUaM6I583R49Ilx7X/SXTig2avTRcv5bwICPjEKn8klH9X14V1Veij3Qm+w ssbpeyTgFzecpNI6r+KKDGl04w1RTYDdQ8tB+8PeX4kjxomlmdv4Ij6iY4WkAIz5H/IfCq KhGDFe7Ts0/y61686BCStsPZhKjg5cWKOSCnM54bOUPsE8MHoAJhZTwCKEwrxP3rIPkZIv XRctyQt1ZRamign5UmwA91elbh/3OWYCuk/bHG8N2tcUvwWruNcvsbF6drF1RQ== ARC-Seal: i=1; a=rsa-sha256; d=iki.fi; s=lahtoruutu; cv=none; t=1783335851; b=daMes92RyKSDe8xPtvM6gHUTecrZyjiRHYeF2yMuscf/va3EC7LFueI2e5ruSZHetB75r3 8eOcK+muuVqqwLYF3nS+/ssv+g2AEdGvbelRFOu51we45RYSAwEwOp5wwfZQhfG7d3dvz9 P/0lUUB9haTBRxzbnnnLpk8Y+gKH1jfEaGThlGX2NjkjXXlaLTRJXeABf/95/TYo9S+Zc2 CywIp8E21btgosAbWRD4eOZjidzUUlQURl9ziKYXHUxFPw825bgih/hNyPMSJe5377ALb9 lQWJisiweOU6umaM4s4yU6ArljrAB963EYxvS4/FbpE863/7ToSliMI++9ncSg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1783335851; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wY9tzQmeaU/dJ3674H+1zm7QNuaGUPzCFc7JFrRz7f4=; b=Ocov6VOEzDE1RMoln9t5JrAjciXNq6l5fXinylrAhs3mHRm86BXyouYREMpgZGIn1ZsrTQ 7BFSazSY8eR4TMUxJU8ZOFy1CEk74PBO2wwpY6H2MLM5c36BJVi1ai5jZhcSil+t9IO3pa TzC8LT0eJNmNL+dcqBD6n6eK2SD2gARVsLMXoCx4CGMIgt3wxGlzD517Lj9/7P8JiMUL2h ZjwzwOEVI91k19XYnH/SrId2dfnlhJKY03ubHw4uP68IBsfWqVPTAU9nUghBWKCqY6oe/V Jy776Tw3jgZZV1SXtu0XaYygWiLj9IzSe724jTzv+NNhh6FwEXVlqnFMVh3/vA== ARC-Authentication-Results: i=1; ORIGINATING; auth=pass smtp.auth=hlinnaka smtp.mailfrom=hlinnaka@iki.fi Message-ID: Date: Mon, 6 Jul 2026 14:04:08 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Implement waiting for wal lsn replay: reloaded To: Xuneng Zhou , Alexander Korotkov Cc: 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 References: <1959506.1775527693@sss.pgh.pa.us> Content-Language: en-US From: Heikki Linnakangas In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 01/05/2026 05:44, Xuneng Zhou wrote: > On Wed, Apr 29, 2026 at 5:01 AM 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, so write and flush > * waiters can be woken at the replay position too. > */ > WaitLSNWakeup(WAIT_LSN_TYPE_STANDBY_REPLAY, > XLogRecoveryCtl->lastReplayedEndRecPtr); > WaitLSNWakeup(WAIT_LSN_TYPE_STANDBY_WRITE, > XLogRecoveryCtl->lastReplayedEndRecPtr); > WaitLSNWakeup(WAIT_LSN_TYPE_STANDBY_FLUSH, > XLogRecoveryCtl->lastReplayedEndRecPtr); 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, XLogRecord *record, TimeLineID *repl > /* > * Update lastReplayedEndRecPtr after this record has been successfully > * replayed. > */ > SpinLockAcquire(&XLogRecoveryCtl->info_lck); > XLogRecoveryCtl->lastReplayedReadRecPtr = xlogreader->ReadRecPtr; > XLogRecoveryCtl->lastReplayedEndRecPtr = xlogreader->EndRecPtr; > XLogRecoveryCtl->lastReplayedTLI = *replayTLI; > SpinLockRelease(&XLogRecoveryCtl->info_lck); > > + /* > + * 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, so write and flush waiters can be woken at > + * the replay position too. > + */ > + WaitLSNWakeup(WAIT_LSN_TYPE_STANDBY_REPLAY, xlogreader->EndRecPtr); > + WaitLSNWakeup(WAIT_LSN_TYPE_STANDBY_WRITE, xlogreader->EndRecPtr); > + WaitLSNWakeup(WAIT_LSN_TYPE_STANDBY_FLUSH, xlogreader->EndRecPtr); > + > /* ------ > * Wakeup walsenders: > * > * On the standby, the WAL is flushed first (which will only wake 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? - Heikki