From 348920f0898fd51f97b73830ec0673dc9d4c48b9 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Sat, 30 Nov 2019 15:16:32 +1300
Subject: [PATCH 2/3] Report I/O errors from BufFileFlush() via ereport().

Previously we ignored the I/O errors while flushing data in BufFileRead()
(following BufFileWrite()), BufFileClose() and BufFileSeek().
---
 src/backend/storage/file/buffile.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 136f80c64d..1706f9d4d5 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -99,7 +99,7 @@ static BufFile *makeBufFile(File firstfile);
 static void extendBufFile(BufFile *file);
 static void BufFileLoadBuffer(BufFile *file);
 static void BufFileDumpBuffer(BufFile *file);
-static int	BufFileFlush(BufFile *file);
+static void BufFileFlush(BufFile *file);
 static File MakeNewSharedSegment(BufFile *file, int segment);
 
 /*
@@ -540,8 +540,7 @@ BufFileRead(BufFile *file, void *ptr, size_t size)
 
 	if (file->dirty)
 	{
-		if (BufFileFlush(file) != 0)
-			return 0;			/* could not flush... */
+		BufFileFlush(file);
 		Assert(!file->dirty);
 	}
 
@@ -629,19 +628,19 @@ BufFileWrite(BufFile *file, void *ptr, size_t size)
 /*
  * BufFileFlush
  *
- * Like fflush()
+ * Like fflush(), except that I/O errors are reported with ereport().
  */
-static int
+static void
 BufFileFlush(BufFile *file)
 {
 	if (file->dirty)
 	{
 		BufFileDumpBuffer(file);
 		if (file->dirty)
-			return EOF;
+			ereport(ERROR,
+					(errcode_for_file_access(),
+					 errmsg("could not write to temporary file: %m")));
 	}
-
-	return 0;
 }
 
 /*
@@ -707,8 +706,7 @@ BufFileSeek(BufFile *file, int fileno, off_t offset, int whence)
 		return 0;
 	}
 	/* Otherwise, must reposition buffer, so flush any dirty data */
-	if (BufFileFlush(file) != 0)
-		return EOF;
+	BufFileFlush(file);
 
 	/*
 	 * At this point and no sooner, check for seek past last segment. The
-- 
2.23.0

