diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 8f95f02..cc4b26b 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2124,9 +2124,21 @@ XLogBackgroundFlush(void)
 		flexible = false;		/* ensure it all gets written */
 	}
 
-	/* Done if already known flushed */
+	/*
+	 * If already known flushed, we're done. Just need to check if we
+	 * are holding an open file handle to a logfile that's no longer
+	 * in use, preventing the file from being deleted.
+	 */
 	if (XLByteLE(WriteRqstPtr, LogwrtResult.Flush))
+	{
+		if (openLogFile >= 0) {
+			if (!XLByteInPrevSeg(LogwrtResult.Write, openLogId, openLogSeg))
+			{
+				XLogFileClose();
+			}
+		}
 		return;
+	}
 
 #ifdef WAL_DEBUG
 	if (XLOG_DEBUG)
