From d45d0c95a0ddee51563b5e94ea046ce4c5cf1baf Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@paquier.xyz>
Date: Thu, 26 Dec 2024 14:06:01 +0900
Subject: [PATCH v1 1/2] Fix failure with incorrect epoch for future 2PC files
 at recovery

A two-phase file in the future would be able to trigger an assertion
failure in AdjustToFullTransactionId() as the epoch counter would
underflow if the checkpoint record uses an epoch of 0.

In non-assert builds, this would create a WARNING message referring to a
2PC file with an epoch of "FFFFFFFF" (or UINT32_MAX), as an effect of
the underflow calculation.

Issue introduced by 5a1dfde8334b.

Reported-by: Vitaly Davydov
Discussion: https://postgr.es/m/13b5b6-676c3080-4d-531db900@47931709
Backpatch-through: 17
---
 src/backend/access/transam/twophase.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/src/backend/access/transam/twophase.c b/src/backend/access/transam/twophase.c
index 49be1df91c..d2649264f9 100644
--- a/src/backend/access/transam/twophase.c
+++ b/src/backend/access/transam/twophase.c
@@ -947,10 +947,9 @@ AdjustToFullTransactionId(TransactionId xid)
 
 	nextXid = XidFromFullTransactionId(nextFullXid);
 	epoch = EpochFromFullTransactionId(nextFullXid);
-	if (unlikely(xid > nextXid))
+	if (unlikely(xid > nextXid) && epoch > 0)
 	{
 		/* Wraparound occurred, must be from a prev epoch. */
-		Assert(epoch > 0);
 		epoch--;
 	}
 
-- 
2.45.2

