diff --git a/src/backend/access/transam/twophase.c b/src/backend/access/transam/twophase.c
index 7982e16abc..f1ba5ac1af 100644
--- a/src/backend/access/transam/twophase.c
+++ b/src/backend/access/transam/twophase.c
@@ -2380,6 +2380,7 @@ PrepareRedoRemove(TransactionId xid, bool giveWarning)
 {
 	GlobalTransaction gxact = NULL;
 	int			i;
+	bool		found = false;
 
 	Assert(RecoveryInProgress());
 
@@ -2391,6 +2392,7 @@ PrepareRedoRemove(TransactionId xid, bool giveWarning)
 		if (gxact->xid == xid)
 		{
 			Assert(gxact->inredo);
+			found = true;
 			break;
 		}
 	}
@@ -2399,7 +2401,7 @@ PrepareRedoRemove(TransactionId xid, bool giveWarning)
 	/*
 	 * Just leave if there is nothing, this is expected during WAL replay.
 	 */
-	if (gxact == NULL)
+	if (!found)
 		return;
 
 	/*
