From 076badafbba8e8b9824f6a1633c2cadf9e3a7655 Mon Sep 17 00:00:00 2001
From: Tomas Vondra <tomas@vondra.me>
Date: Mon, 29 Sep 2025 22:52:22 +0200
Subject: [PATCH v20250930 10/22] BufFileLoadBuffer - simpler FileRead handling

---
 src/backend/storage/file/buffile.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 4d9e2e6b3b0..12c2e974783 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -589,17 +589,20 @@ BufFileLoadBuffer(BufFile *file)
 						 file->curOffset,
 						 WAIT_EVENT_BUFFILE_READ);
 
-		/* Check if first read succeeded */
-		if (nread != sizeof(nbytes) && nread > 0)
+		/* did we read the length of the next buffer? */
+		if (nread == 0)
 		{
+			/* eof, nothing to do */
+		}
+		else if (nread != sizeof(nbytes))
+		{
+			/* unexpected number of bytes, also covers (nread < 0) */
 			ereport(ERROR,
 					(errcode_for_file_access(),
 					 errmsg("could not read file \"%s\": %m",
 							FilePathName(thisfile))));
 		}
-
-		/* if not EOF let's continue */
-		if (nread > 0)
+		else
 		{
 			/* A long life buffer limits number of memory allocations */
 			char	   *buff = file->cBuffer;
-- 
2.51.0

