diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml
index 4dd9d029e6..4ef68c308e 100644
--- a/doc/src/sgml/func.sgml
+++ b/doc/src/sgml/func.sgml
@@ -18649,9 +18649,9 @@ postgres=# select pg_start_backup('label_goes_here');
    </para>
 
    <para>
-    When executed on a primary, the function also creates a backup history file
-    in the write-ahead log
-    archive area. The history file includes the label given to
+    The function also creates a backup history file in the write-ahead log
+    archive area when archiving is enabled.
+    The history file includes the label given to
     <function>pg_start_backup</function>, the starting and ending write-ahead log locations for
     the backup, and the starting and ending times of the backup.  The return
     value is the backup's ending write-ahead log location (which again
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 02974f0e52..7e0c5da0e2 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -10729,11 +10729,7 @@ do_pg_stop_backup(char *labelfile, bool waitforarchive, TimeLineID *stoptli_p)
 	XLogRecPtr	startpoint;
 	XLogRecPtr	stoppoint;
 	TimeLineID	stoptli;
-	pg_time_t	stamp_time;
-	char		strfbuf[128];
-	char		histfilepath[MAXPGPATH];
 	char		startxlogfilename[MAXFNAMELEN];
-	char		stopxlogfilename[MAXFNAMELEN];
 	char		lastxlogfilename[MAXFNAMELEN];
 	char		histfilename[MAXFNAMELEN];
 	char		backupfrom[20];
@@ -10939,12 +10935,6 @@ do_pg_stop_backup(char *labelfile, bool waitforarchive, TimeLineID *stoptli_p)
 	 * location. Note that it can be greater than the exact backup end
 	 * location if the minimum recovery point is updated after the backup of
 	 * pg_control. This is harmless for current uses.
-	 *
-	 * XXX currently a backup history file is for informational and debug
-	 * purposes only. It's not essential for an online backup. Furthermore,
-	 * even if it's created, it will not be archived during recovery because
-	 * an archiver is not invoked. So it doesn't seem worthwhile to write a
-	 * backup history file during recovery.
 	 */
 	if (backup_started_in_recovery)
 	{
@@ -10989,9 +10979,21 @@ do_pg_stop_backup(char *labelfile, bool waitforarchive, TimeLineID *stoptli_p)
 		 * valid as soon as archiver moves out the current segment file.
 		 */
 		RequestXLogSwitch(false);
+	}
 
-		XLByteToPrevSeg(stoppoint, _logSegNo, wal_segment_size);
-		XLogFileName(stopxlogfilename, stoptli, _logSegNo, wal_segment_size);
+	/*
+	 * Write the backup history file if archiving is enabled.
+	 *
+	 * XXX currently a backup history file is for informational and debug
+	 * purposes only. It's not essential for an online backup.
+	 */
+	if ((!backup_started_in_recovery && XLogArchivingActive()) ||
+		(backup_started_in_recovery && XLogArchivingAlways()))
+	{
+		char		strfbuf[128];
+		char		histfilepath[MAXPGPATH];
+		char		stopxlogfilename[MAXFNAMELEN];
+		pg_time_t	stamp_time;
 
 		/* Use the log timezone here, not the session timezone */
 		stamp_time = (pg_time_t) time(NULL);
@@ -10999,9 +11001,8 @@ do_pg_stop_backup(char *labelfile, bool waitforarchive, TimeLineID *stoptli_p)
 					"%Y-%m-%d %H:%M:%S %Z",
 					pg_localtime(&stamp_time, log_timezone));
 
-		/*
-		 * Write the backup history file
-		 */
+		XLByteToPrevSeg(stoppoint, _logSegNo, wal_segment_size);
+		XLogFileName(stopxlogfilename, stoptli, _logSegNo, wal_segment_size);
 		XLByteToSeg(startpoint, _logSegNo, wal_segment_size);
 		BackupHistoryFilePath(histfilepath, stoptli, _logSegNo,
 							  startpoint, wal_segment_size);
