diff --git a/src/backend/access/transam/xlogrecovery.c b/src/backend/access/transam/xlogrecovery.c
index cb07694aea..18c14d1fbf 100644
--- a/src/backend/access/transam/xlogrecovery.c
+++ b/src/backend/access/transam/xlogrecovery.c
@@ -4223,6 +4223,13 @@ XLogFileReadAnyTLI(XLogSegNo segno, int emode, XLogSource source)
 				return fd;
 			}
 		}
+
+		/*
+		 * If recovery_target_timeline is "latest", we don't want to read this
+		 * segment belongs to older timelines.
+		 */
+		if (recoveryTargetTimeLineGoal == RECOVERY_TARGET_TIMELINE_LATEST)
+			break;
 	}
 
 	/* Couldn't find it.  For simplicity, complain about front timeline */
