From 3c316cd158b22c07d4ef1807068ff53b51cf402f Mon Sep 17 00:00:00 2001
From: Tomas Vondra <tomas@vondra.me>
Date: Mon, 29 Sep 2025 22:56:48 +0200
Subject: [PATCH v20250930 11/22] BufFileLoadBuffer - simpler FileRead handling

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

diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c
index 12c2e974783..932c10351bc 100644
--- a/src/backend/storage/file/buffile.c
+++ b/src/backend/storage/file/buffile.c
@@ -620,20 +620,19 @@ BufFileLoadBuffer(BufFile *file)
 												  file->curOffset + sizeof(nbytes),
 												  WAIT_EVENT_BUFFILE_READ);
 
-				/* Check if second read succeeded */
-				if (nread_orig != sizeof(original_size) && nread_orig > 0)
+				/*
+				 * Did we read the second (raw) length? We should not get an
+				 * EOF here, we've already read the first length.
+				 */
+				if ((nread_orig == 0) || (nread_orig != sizeof(original_size)))
 				{
+					/* also covers (nread_orig < 0) */
 					ereport(ERROR,
 							(errcode_for_file_access(),
 							 errmsg("could not read file \"%s\": %m",
 									FilePathName(thisfile))));
 				}
 
-				if (nread_orig <= 0)
-				{
-					file->nbytes = 0;
-					return;
-				}
 
 				/* Check if data is uncompressed (marker = -1) */
 				if (original_size == -1)
-- 
2.51.0

