diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index 860bbd40d4..a67938ac44 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -584,6 +584,12 @@ errfinish(const char *filename, int lineno, const char *funcname)
 		 * worthy of panic, depending on which subprocess returns it.
 		 */
 		proc_exit(1);
+
+		/*
+		 * Closes all of the calling process's open streams.
+		 * On Windows, close and remove all temporary files created by tmpfile function.
+		 */
+		fcloseall();
 	}
 
 	if (elevel >= PANIC)
@@ -596,6 +602,13 @@ errfinish(const char *filename, int lineno, const char *funcname)
 		 * children...
 		 */
 		fflush(NULL);
+
+		/*
+		 * Closes all of the calling process's open streams.
+		 * On Windows, close and remove all temporary files created by tmpfile function.
+		 */
+		fcloseall();
+
 		abort();
 	}
 