diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 377afb8732..06d3ce676e 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -9088,6 +9088,13 @@ CreateCheckPoint(int flags)
 	if (!shutdown && XLogStandbyInfoActive())
 		LogStandbySnapshot();
 
+	/*
+	 * ThisTimeLineID may have moved so far so we need to restore it so that
+	 * the checkpoint record goes to the right timeline.  (Currently
+	 * CheckPointTwoPhase() is known to have a chance to change it.)
+	 */
+	ThisTimeLineID = checkPoint.ThisTimeLineID;
+
 	START_CRIT_SECTION();
 
 	/*
