diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 7d34218..370a927 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2522,8 +2522,22 @@ InstallXLogFileSegment(XLogSegNo *segno, char *tmppath,
 {
 	char		path[MAXPGPATH];
 	struct stat stat_buf;
+	TimeLineID	InstallTimeLineID = ThisTimeLineID;
 
-	XLogFilePath(path, ThisTimeLineID, *segno);
+	/*
+	 * Update InstallTimeLineID to the timeline we're currently replaying,
+	 * so that we install any recycled segments on that timeline.
+	 *
+	 * There is no guarantee that the WAL segments will be useful on the
+	 * current timeline; if recovery proceeds to a new timeline right
+	 * after this, the pre-allocated WAL segments on this timeline will
+	 * not be used, and will go wasted until recycled on the next
+	 * restartpoint. We'll live with that.
+	 */
+	if (RecoveryInProgress())
+	(void) GetXLogReplayRecPtr(&InstallTimeLineID);
+
+	XLogFilePath(path, InstallTimeLineID, *segno);
 
 	/*
 	 * We want to be sure that only one process does this at a time.
@@ -7456,18 +7470,6 @@ CreateRestartPoint(int flags)
 		KeepLogSeg(endptr, &_logSegNo);
 		_logSegNo--;
 
-		/*
-		 * Update ThisTimeLineID to the timeline we're currently replaying,
-		 * so that we install any recycled segments on that timeline.
-		 *
-		 * There is no guarantee that the WAL segments will be useful on the
-		 * current timeline; if recovery proceeds to a new timeline right
-		 * after this, the pre-allocated WAL segments on this timeline will
-		 * not be used, and will go wasted until recycled on the next
-		 * restartpoint. We'll live with that.
-		 */
-		(void) GetXLogReplayRecPtr(&ThisTimeLineID);
-
 		RemoveOldXlogFiles(_logSegNo, endptr);
 
 		/*
