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 1uKJOL-0014uA-IY for pgsql-bugs@arkaria.postgresql.org; Wed, 28 May 2025 16:12:34 +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 1uKJOJ-001Liv-Pb for pgsql-bugs@arkaria.postgresql.org; Wed, 28 May 2025 16:12:31 +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 1uKJOJ-001Lin-At for pgsql-bugs@lists.postgresql.org; Wed, 28 May 2025 16:12:31 +0000 Received: from mail.postgrespro.ru ([93.174.132.70]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1uKJOE-000Zcr-1W for pgsql-bugs@lists.postgresql.org; Wed, 28 May 2025 16:12:28 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mx2023; t=1748448745; bh=oHcbe0u/IVNOXbyIEqj69gnrEtqKN2GY/jDcPuR7vyA=; h=Message-ID:Date:User-Agent:From:Subject:To:Cc:References: In-Reply-To:From; b=5j/ADgSLajsTkA9QJICd1PzGeFKYyuB7+9oM758wSbs0pdkJqbq/mrZaRLDafw18E 9bpbOxNOKD4YMj+7Oyyl9fAUXZlnGeH9huvsO2RXVYcLXkHzqmBN0us583uZ0d0y97 uz+/hIuTMEiFzKF90s2WSjqCju6QTk7GsMS15WXofBXFLxp417Z/l4S4+chD0Dk2KW CqAXmiae6PziQBw/OxheJ2GO4/QCu37rDJlVd48Ejf9oCwFfUFtabS7asWR7Kryl6Y bED3e6dgY8VScU+L4Jj3kvSHY66+x2FHA1pl301ShIdkDr4svn6YT5qxpxUkmRPRPS nEiVqprOyCVAA== Received: from [10.4.13.170] (unknown [93.174.131.141]) (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) (Client did not present a certificate) (Authenticated sender: a.makhmutov@postgrespro.ru) by mail.postgrespro.ru (Postfix/465) with ESMTPSA id B146C6066B; Wed, 28 May 2025 19:12:25 +0300 (MSK) Content-Type: multipart/mixed; boundary="------------SSsu0LYVMtCnDZDtow005KO0" Message-ID: Date: Wed, 28 May 2025 19:12:25 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird From: Alexey Makhmutov Subject: Re: Standby server with cascade logical replication could not be properly stopped under load To: Michael Paquier Cc: Bertrand Drouvot , shveta malik , Ajin Cherian , pgsql-bugs@lists.postgresql.org References: <52138028-7246-421c-9161-4fa108b88070@postgrespro.ru> Content-Language: en-US In-Reply-To: X-KSMG-AntiPhishing: NotDetected X-KSMG-AntiSpam-Interceptor-Info: not scanned X-KSMG-AntiSpam-Status: not scanned, disabled by settings X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 2.1.0.7854, bases: 2025/05/28 14:52:00 #27573436 X-KSMG-AntiVirus-Status: NotDetected, skipped X-KSMG-LinksScanning: not scanned, disabled by settings X-KSMG-Message-Action: skipped X-KSMG-Rule-ID: 1 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------SSsu0LYVMtCnDZDtow005KO0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 5/28/25 10:35, Michael Paquier wrote: > Wouldn't it be better to extend GetStandbyFlushRecPtr() so as the > caller can directly decide if they want to include in the maths the > last LSN flushed by the WAL receiver, depending on the code path where > the routine is called? That makes for a larger patch, of course, but > it removes the need to look at MyWalSnd inside > GetStandbyFlushRecPtr(). Well, this is a great question, because it made me thinking whether we need to change behavior of THIS function at all. Extending 'GetStandbyFlushRecPtr' to properly support logical replication will just make it equal to the 'GetXLogReplayRecPtr'. So, basically we just need to replace invocation of 'GetStandbyFlushRecPtr' with 'GetXLogReplayRecPtr' and this should solve the problem. The only invoker of 'GetStandbyFlushRecPtr' outside of walsender.c is slot synchronization routine, which expects to get 'physical' position anyway, so it does not need to be changed. All other invocations are performed from the 'walsender.c' file itself. Obviously, both 'StartReplication' (physical) and 'XLogSendPhysical' functions need current logic, so we can skip them. The 'XLogSendLogical' need to be changed to use 'GetXLogReplayRecPtr' (to get consistent behavior with WalSndWaitForWal and actually fix the original problem discussed in this thread). This leaves us with only single function to consider: 'IdentifySystem'. This function could be called both in case of physical and logical replication, but I assume that we need to return flushed WAL position in it as well, as it's described in the documentation for this command. This also seems to be the right behavior, as primary goal for 'identify' command is to inform client about the potential replication start point and we definitely can start replication from the flushed position (as we already has this record), just with some delay (once our recovery get us to this point). But maybe somebody with more knowledge of the logical replication expectation could confirm this. The fix seems to be very simple in this case (assuming the logic above is correct): we only replace 'GetStandbyFlushRecPtr' call in 'XLogSendLogical' with the 'GetXLogReplayRecPtr' and that's it. Attached is the modified patch. It could be applied on top of 16, 17 and master versions. As for the test: do you want this case with shutdown to be added to the existing test plan for standby logical recovery? The condition to trigger the problem is just to have a flushed and replayed positions pointing to different values during standby shutdown. I could try to add it to the test suit, but it could take some time. Thanks, Alexey --------------SSsu0LYVMtCnDZDtow005KO0 Content-Type: text/x-patch; charset=UTF-8; name="0001-Use-only-replayed-position-as-target-flush-point-4-pg-master.patch" Content-Disposition: attachment; filename*0="0001-Use-only-replayed-position-as-target-flush-point-4-pg-m"; filename*1="aster.patch" Content-Transfer-Encoding: base64 RnJvbSAxZDkxZTA2N2ZhZDBiYzY4YjU1ZDZkNzcxY2MwMzA3ODY4MWRlNzgwIE1vbiBTZXAg MTcgMDA6MDA6MDAgMjAwMQpGcm9tOiBBbGV4ZXkgTWFraG11dG92IDxhLm1ha2ht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