From f0617e62e2ec09b96e1e62f1173b2e2d1456f2d4 Mon Sep 17 00:00:00 2001
From: Sergey Solovev <ashen.blade@ya.ru>
Date: Thu, 16 Jul 2026 12:18:37 +0300
Subject: [PATCH] Do not flush BufFile for regular temp files

When creating BufFile using BufFileCreateTemp regular temp file is
created which is deleted on close. But when we call BufFileClose then we
should not waste resources dumping data to file if it is going to be
deleted right after it.
---
 src/backend/storage/file/buffile.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 5c59913646b..c365d984044 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -414,8 +414,9 @@ BufFileClose(BufFile *file)
 {
 	int			i;
 
-	/* flush any unwritten data */
-	BufFileFlush(file);
+	/* flush any unwritten data if it will not be deleted on close */
+	if (file->fileset != NULL)
+		BufFileFlush(file);
 	/* close and delete the underlying file(s) */
 	for (i = 0; i < file->numFiles; i++)
 		FileClose(file->files[i]);
-- 
2.43.0

