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 1utRvB-007Px8-0T for pgsql-bugs@arkaria.postgresql.org; Tue, 02 Sep 2025 14:23: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 1utRv9-003x38-9X for pgsql-bugs@arkaria.postgresql.org; Tue, 02 Sep 2025 14:23:39 +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 1utRkg-003wLZ-9q for pgsql-bugs@lists.postgresql.org; Tue, 02 Sep 2025 14:12:50 +0000 Received: from forward502a.mail.yandex.net ([2a02:6b8:c0e:500:1:45:d181:d502]) by makus.postgresql.org with smtp (Exim 4.96) (envelope-from ) id 1utRka-000Ace-0f for pgsql-bugs@lists.postgresql.org; Tue, 02 Sep 2025 14:12:49 +0000 Received: from mail-nwsmtp-smtp-production-main-67.vla.yp-c.yandex.net (mail-nwsmtp-smtp-production-main-67.vla.yp-c.yandex.net [IPv6:2a02:6b8:c1d:6148:0:640:ada2:0]) by forward502a.mail.yandex.net (Yandex) with ESMTPS id 1DD1981C22; Tue, 02 Sep 2025 17:12:40 +0300 (MSK) Received: by mail-nwsmtp-smtp-production-main-67.vla.yp-c.yandex.net (smtp/Yandex) with ESMTPSA id ECgETBlMuqM0-gaqbMyVA; Tue, 02 Sep 2025 17:12:39 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arenadata.io; s=mail; t=1756822359; bh=lc/dNxsgNOuvn40iVveCwLoDBuTfCVidVJsMZBaXMXM=; h=In-Reply-To:Date:References:To:From:Subject:Message-ID; b=e9jsbBvijiFhZWaWKcK9y9Lg2ch+RVWHXHyp76PJMUPQzQoKn6MzeaLwwbbNNFwKy jpJplIKsr1ZrwxH6FUh71wsWDtDywZBZ5pR2SgY3xv+4YsQirqsuOeMLyARzPgqS+f vRAA3gYoRq78KiCuWbZJLuNmPlFXJXGaLVlh33EE= Authentication-Results: mail-nwsmtp-smtp-production-main-67.vla.yp-c.yandex.net; dkim=pass header.i=@arenadata.io Message-ID: Subject: Re: BUG #18575: Sometimes pg_rewind mistakenly assumes that nothing needs to be done. From: Maxim Michkov To: Heikki Linnakangas , "pgsql-bugs@lists.postgresql.org" Date: Tue, 02 Sep 2025 17:12:14 +0300 In-Reply-To: References: <18575-1b9b3b60e2a480de@postgresql.org> <092dd515-b7b4-4fd0-8407-ceca2f02f6ec@iki.fi> <3382141723103572@mail.yandex.ru> <188271723109777@mail.yandex.com> <3a626a21-319b-44e9-a2b5-94b8a93f9cd8@iki.fi> <31111723111906@mail.yandex.com> <1760421723118253@mail.yandex.com> <2171536a-8227-4e53-ac47-33b69093b61d@iki.fi> Content-Type: multipart/mixed; boundary="=-CONP9yvcx3c/SoZ0MWte" User-Agent: Evolution 3.56.2 MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --=-CONP9yvcx3c/SoZ0MWte Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, 2024-08-09 at 18:26 +0300, Heikki Linnakangas wrote: > 3. When pg_rewind has nothing to do, the target server is left=20 > unmodified, in a state such that when you restart it, it will replay > all=20 > the WAL it has locally in pg_wal first, before connecting to the=20 > primary. Even though the target is a direct ancestor of the source > and=20 > hence it *can* follow the WAL to the source's position without=20 > rewinding, it doesn't mean that it *will* actually do so. >=20 > The attached=C2=A0 changes it so that it updates the control file in that= =20 > case, setting minRecoveryPoint and minRecoveryPointTLI to point to > the=20 > source's current WAL position. That way, when you start it up, it > will=20 > follow the timeline history to reach that point. (This requires > fixing=20 > issue 2, because otherwise it still won't follow the history > correctly=20 > to reach the minRecoveryPointTLI) Hello, I am currently researching a very similar issue. It seems like your patches work correctly (thank you for those), but I've found a weird edge case for patch 0004. Basically, if you don't do anything to the primary after promotion (so that its last checkpoint is *exactly* at the switchpoint), pg_rewind with patch 0004 sets minRecoveryPoint to the exact LSN of the switchpoint. After that the standby won't start, producing errors like `requested timeline 2 does not contain minimum recovery point 0/4711F38 on timeline 2`. This happens because of this check in xlogrecovery.c: ``` if (!XLogRecPtrIsInvalid(ControlFile->minRecoveryPoint) && tliOfPointInHistory(ControlFile->minRecoveryPoint - 1, expectedTLEs) !=3D ControlFile->minRecoveryPointTLI) ereport(FATAL, /* ... */); ``` Because it checks TLI of minRecoveryPoint - 1, it expects to see the timeline before the switch (timeline 1), but we actually want it to go to timeline 2. It seems like minRecoveryPoint is supposed to indicate minimum allowed end of WAL, so when setting it to ensure some WAL is processed (like the checkpoint WAL on another timeline) we have to set it to LSN+1 instead. Do you think this is the correct fix, or if instead -1 in xlogrecovery.c should be removed? Attached is a patch to 010 TAP test that reproduces this behavior. --=-CONP9yvcx3c/SoZ0MWte Content-Disposition: attachment; filename="0001-Tap-test-for-minRecoveryPoint-matching-switchpoint.patch" Content-Type: text/x-patch; name="0001-Tap-test-for-minRecoveryPoint-matching-switchpoint.patch"; charset="UTF-8" Content-Transfer-Encoding: base64 RnJvbSA1NDk3NmI1ZjVmODM4Y2NhYWVhNTgzNWFmZTc4OTg3MjY3Njg1ZTY0IE1vbiBTZXAgMTcg MDA6MDA6MDAgMjAwMQpGcm9tOiBNYWtzaW0gTWljaGtvdiA8bS5taWNoa292QGFyZW5hZGF0YS5p bz4KRGF0ZTogVHVlLCAyIFNlcCAyMDI1IDE0OjA4OjE0ICswMDAwClN1YmplY3Q6IFtQQVRDSF0g VGFwIHRlc3QgZm9yIG1pblJlY292ZXJ5UG9pbnQgbWF0Y2hpbmcgc3dpdGNocG9pbnQKCi0tLQog c3JjL2Jpbi9wZ19yZXdpbmQvdC8wMTBfdGFyZ2V0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