pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Chao Li <li.evan.chao@gmail.com>
To: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Subject: Fix error handling in pg_flush_data() and FilePrefetch()
Date: Fri, 30 Oct 2026 11:03:44 +0800
Message-ID: <F1916C7A-F152-4AF3-87A9-40492D8B3670@gmail.com> (raw)

Hi,

When I was trying to understand what was going on in thread [1], I came across posix_fadvise() and was wondering how it is used. So I searched for it in the source tree and spotted two related issues.

1
```
		rc = posix_fadvise(fd, offset, nbytes, POSIX_FADV_DONTNEED);

		if (rc != 0)
		{
			/* don't error out, this is just a performance optimization */
			ereport(WARNING,
					(errcode_for_file_access(),
					 errmsg("could not flush dirty data: %m")));
		}
```

This code uses %m to report an error from posix_fadvise(). However, posix_fadvise() doesn't set errno on failure. Instead, it returns the error code directly. See [2].

Therefore, both %m may use an unrelated errno value. As a simple fix, we can assign rc to errno before calling ereport().

2
```
/*
 * FilePrefetch - initiate asynchronous read of a given range of the file.
 *
 * Returns 0 on success, otherwise an errno error code (like posix_fadvise()).
 *
 * posix_fadvise() is the simplest standardized interface that accomplishes
 * this.
 */
int
FilePrefetch(File file, pgoff_t offset, pgoff_t amount, uint32 wait_event_info)
{
...
		returnCode = FileAccess(file);
		if (returnCode < 0)
			return returnCode;
```

FilePrefetch() is intended to return an error code, just like posix_fadvise(). However, FileAccess() returns -1 and sets errno on failure:
```
/* returns 0 on success, -1 on re-open failure (with errno set) */
static int
FileAccess(File file)
```

So here we should return errno instead of returnCode.

The attached patch fixes these two issues. As these are eye-caught issues, I don't have a repro for either of them. I don’t even think my local build exercises the affected code paths. So I'll put the patch on the CommitFest and let CFBot test it.


[1] https://postgr.es/m/CAHg+QDf=eFFnOvmaPJQrXNiXaTOhhJhvO=DzmVUi=VGFRh5B0A@mail.gmail.com
[2] https://man7.org/linux/man-pages/man3/posix_fadvise.3p.html

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachments:

v1-0001-Fix-error-handling-in-pg_flush_data-and-FilePrefe.patch (application/octet-stream, 1.7K) download From a7a6c5a23d3908c1ac0416249f725bb61b79e29c Mon Sep 17 00:00:00 2001 From: "Chao Li (Evan)" <lic@highgo.com> Date: Fri, 30 Oct 2026 10:58:59 +0800 Subject: [PATCH v1] Fix error handling in pg_flush_data() and FilePrefetch() posix_fadvise() returns an error code directly instead of setting errno. However, pg_flush_data() uses %m and errcode_for_file_access() to report errors without setting errno, potentially reporting an unrelated error. Fix this by assigning the returned error code to errno before reporting the error. FilePrefetch() is documented to return an errno error code on failure, but it returns -1 when FileAccess() fails. Fix this by returning errno instead. Author: Chao Li <lic@highgo.com> --- src/backend/storage/file/fd.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/backend/storage/file/fd.c b/src/backend/storage/file/fd.c index 190c9974494..8c5edc8e17a 100644 --- a/src/backend/storage/file/fd.c +++ b/src/backend/storage/file/fd.c @@ -683,9 +683,10 @@ retry: */ rc = posix_fadvise(fd, offset, nbytes, POSIX_FADV_DONTNEED); - if (rc != 0) { + /* for compatibility with %m printing etc */ + errno = rc; /* don't error out, this is just a performance optimization */ ereport(WARNING, (errcode_for_file_access(), @@ -2078,7 +2079,7 @@ FilePrefetch(File file, pgoff_t offset, pgoff_t amount, uint32 wait_event_info) returnCode = FileAccess(file); if (returnCode < 0) - return returnCode; + return errno; retry: pgstat_report_wait_start(wait_event_info); @@ -2102,7 +2103,7 @@ retry: returnCode = FileAccess(file); if (returnCode < 0) - return returnCode; + return errno; ra.ra_offset = offset; ra.ra_count = amount; -- 2.50.1 (Apple Git-155) =

view thread (4+ messages)  latest in thread

Message-ID: <F1916C7A-F152-4AF3-87A9-40492D8B3670@gmail.com>
Permalink:  ../F1916C7A-F152-4AF3-87A9-40492D8B3670@gmail.com/
Also on:    postgresql.org/message-id/F1916C7A-F152-4AF3-87A9-40492D8B3670@gmail.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: li.evan.chao@gmail.com
  Subject: Re: Fix error handling in pg_flush_data() and FilePrefetch()
  In-Reply-To: <F1916C7A-F152-4AF3-87A9-40492D8B3670@gmail.com>
  References: <F1916C7A-F152-4AF3-87A9-40492D8B3670@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox