diff --git a/src/backend/access/transam/twophase.c b/src/backend/access/transam/twophase.c
index 6e6678cfff69df7a50b27a2a02064a523a6086ea..49b6429218786a292baa7bf0827447934f2cde75 100644
--- a/src/backend/access/transam/twophase.c
+++ b/src/backend/access/transam/twophase.c
@@ -2375,6 +2375,7 @@ PrepareRedoRemove(TransactionId xid, bool giveWarning)
 {
 	GlobalTransaction gxact = NULL;
 	int			i;
+	bool		found = false;
 
 	Assert(RecoveryInProgress());
 
@@ -2386,6 +2387,7 @@ PrepareRedoRemove(TransactionId xid, bool giveWarning)
 		if (gxact->xid == xid)
 		{
 			Assert(gxact->inredo);
+			found = true;
 			break;
 		}
 	}
@@ -2394,7 +2396,7 @@ PrepareRedoRemove(TransactionId xid, bool giveWarning)
 	/*
 	 * Just leave if there is nothing, this is expected during WAL replay.
 	 */
-	if (gxact == NULL)
+	if (!found)
 		return;
 
 	/*