diff --git a/src/backend/replication/walreceiver.c b/src/backend/replication/walreceiver.c
index 60de3be92c..81dde27372 100644
--- a/src/backend/replication/walreceiver.c
+++ b/src/backend/replication/walreceiver.c
@@ -173,6 +173,7 @@ WalReceiverMain(void)
 	XLogRecPtr	startpoint;
 	TimeLineID	startpointTLI;
 	TimeLineID	primaryTLI;
+	XLogSegNo	startsegno;
 	bool		first_stream;
 	WalRcvData *walrcv = WalRcv;
 	TimestampTz last_recv_timestamp;
@@ -313,6 +314,32 @@ WalReceiverMain(void)
 	if (sender_host)
 		pfree(sender_host);
 
+	/*
+	 * There's a case walreceiver terminated before notifying the last
+	 * finished segment. Make sure the last finished segment is archived
+	 * immediately.
+	 */
+	XLByteToSeg(startpoint, startsegno, wal_segment_size);
+	if (startsegno > 1)
+	{
+		char 		xlogfname[MAXFNAMELEN];
+		TimeLineID	prevsegTLI;
+		XLogRecPtr	prevsegEndRecPtr;
+		List	   *tles;
+
+		/*
+		 * The previous segment may be in the previous timeline.  Track
+		 * timelines to find the segment on the correct timeline.
+		 */
+		tles = readTimeLineHistory(startpointTLI);
+		prevsegEndRecPtr =
+			startpoint - XLogSegmentOffset(startpoint, wal_segment_size) - 1;
+		prevsegTLI = tliOfPointInHistory(prevsegEndRecPtr, tles);
+		XLogFileName(xlogfname, prevsegTLI, startsegno - 1,
+					 wal_segment_size);
+		XLogArchiveCheckDone(xlogfname);
+	}
+		
 	first_stream = true;
 	for (;;)
 	{
