Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xMcuk-000000027WV-4AKK for pgsql-hackers@arkaria.postgresql.org; Fri, 30 Oct 2026 03:04:23 +0000 Received: from localhost ([127.0.0.1]:36544 helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xMcuk-00000007iKy-14mH for pgsql-hackers@arkaria.postgresql.org; Fri, 30 Oct 2026 03:04:22 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]:43902) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xMcuj-00000007iKo-2rks for pgsql-hackers@lists.postgresql.org; Fri, 30 Oct 2026 03:04:21 +0000 Received: from mail-dl1-x122a.google.com ([2607:f8b0:4864:20::122a]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1xMcuh-00000001d5B-2QtF for pgsql-hackers@lists.postgresql.org; Fri, 30 Oct 2026 03:04:20 +0000 Received: by mail-dl1-x122a.google.com with SMTP id a92af1059eb24-156cbe59622so139203c88.0 for ; Thu, 29 Oct 2026 20:04:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1793329459; x=1793934259; darn=lists.postgresql.org; h=to:date:message-id:subject:mime-version:content-type:from:from:to :cc:subject:date:message-id:reply-to:content-type; bh=P71F1SztU3sngXT/YxcOOnr9nu4R23cfHV/gjlOBbcQ=; b=Mgl13FEprXXjpnLJQLI9gP+IVzlKbsfCA6NsQAF/dZWdKvYHxMb2QDDBwuOl7f3RKz AB91DQdd3ztwXrme7odLwpYGb7qpzeWeCCWytT0/Z2yB9RbW/RSDuWoMaZFmJMnbcLeW YzNw9K0WHvUVf2toFD26ZCy0TT2/0gyaFzhZ0dgl+2o+rPw8mOiuXSIhLaQtmfGU98dE tuiWe++CpQqAsNIbZFIceMqbtd2xDgWBa9S9QG9j5zZuZBxSUOSMXI3tiyFkxKNLEapt ntibYF6ZHWiEOP4ESo80fJjQ3T64mNORtNrYLrXChfFNXOoPW7nCFJ4qPhZ9/FMiEGTz YNag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793329459; x=1793934259; h=to:date:message-id:subject:mime-version:content-type:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=P71F1SztU3sngXT/YxcOOnr9nu4R23cfHV/gjlOBbcQ=; b=0ugHXvxufl6pHdyYiJ040DZKIc6OJhsnE3n0zeedAo5GphZoKVl/M6biw6mNWVeO/b t9dzkmvi7UEeoixfD4JJqo1QGff9yiehTLDipB2da6i93a2mUVIOtzb+DpfRhYf0pEVV 7PvjmKh5Mo0Uy5SLoEjnmhH+1bcjpFrqjsixjNdtMWutwCxHYlyjV2md/qpm9VImSc+i bGUjbh5JCow/6HOyWsHEpM+6x+MBBcRJYzey651d63VfJnEaWmGor2Uapbu5rPl9NxZW Y0b/SD5HIhzKhoTb68jLPyW+VH83GEdJPI1+EJ8fYJgRyMQHXHuVtugrnoIATTNfwXN7 5pPQ== X-Gm-Message-State: AFq9FYIefBnLs3FfVOxi/I1vprxewhc2t1cKCqzSuMRoFIwcSmOG8K1O V25L7oWsHK8QTLyE/HOXD6HMN/6sW/LrJYS1yH+dF5CBD41JTru9RX2mj8LsIA== X-Gm-Gg: AYBFou0n45M3a8LMAl44+7vHSlO2ohVYKbCcxqmuuj3hKn46/zSnkygqKOLcTO2Ae2I Gw4+SG6l0Jw+kFREVnFl4pNK0bmZ6cnbqM/AQYFbo2DWPcXzFnbAVx+n8PAKOZC25jm9/9jVjlq hPIwe6X1YGUJ4w9sSkB2N/Pu0jmIoNBEsWYalUHCEGxYv7XsfUykNV7TDQSQEz3YDUXfu/sgL1m YLr0/r2Lxdr6IMx2yBIOdyqcB+f2JD9t8aW4Ei1BCSdjKLjLddz6tIOLVIGufoRILqwRN0N9Y9C JxI1r7S9WDMYDRlvP8KidOPAoXIkNdi9zZCG3W2wchCXp9FdFkJlQ+gYOqQqH0ZRE4lpnO6C3lo f4NIRQZFejvzYnfYM3DYAwBRAOzjXGT4uMEJa3KRlpUJOghrjMe0KWF9WGpgGTBg4qw5tBkLTLK WFASSBh5j3pPq56P2tBM4KXY7topdTbFSjNBRmJ2kU8N/1zFA4Sjxlk7tsiQHUlKeuGUhXJMb9z Av5qPIJxzyk4RQeAiWwG5Jj+DoYYKgW7vxBiFhpJl6NM5LGKH8yHM60AMA/OKegsyXUho4KYHtn t4LmpSfbNHCshm2LOlSMCfvgK37nbECjr7Btjw8= X-Received: by 2002:a05:7022:7e0c:b0:15c:1679:1cc7 with SMTP id a92af1059eb24-1ac5f181878mr1327020c88.37.1793329458208; Thu, 29 Oct 2026 20:04:18 -0700 (PDT) Received: from smtpclient.apple ([185.135.79.161]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-1ac1b0a628bsm2622725c88.22.2026.10.29.20.04.16 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 29 Oct 2026 20:04:17 -0700 (PDT) From: Chao Li Content-Type: multipart/mixed; boundary="Apple-Mail=_614AEDD1-C5D3-41C1-8CDD-69722B93A103" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3864.700.51.1.1\)) Subject: Fix error handling in pg_flush_data() and FilePrefetch() Message-Id: Date: Fri, 30 Oct 2026 11:03:44 +0800 To: PostgreSQL Hackers X-Mailer: Apple Mail (2.3864.700.51.1.1) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_614AEDD1-C5D3-41C1-8CDD-69722B93A103 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 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 =3D posix_fadvise(fd, offset, nbytes, = POSIX_FADV_DONTNEED); if (rc !=3D 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 =3D 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=E2=80=99t 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=3DeFFnOvmaPJQrXNiXaTOhhJhvO=3DDzmVUi=3DVGFRh5= B0A@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/ --Apple-Mail=_614AEDD1-C5D3-41C1-8CDD-69722B93A103 Content-Disposition: attachment; filename=v1-0001-Fix-error-handling-in-pg_flush_data-and-FilePrefe.patch Content-Type: application/octet-stream; x-unix-mode=0644; name="v1-0001-Fix-error-handling-in-pg_flush_data-and-FilePrefe.patch" Content-Transfer-Encoding: quoted-printable =46rom=20a7a6c5a23d3908c1ac0416249f725bb61b79e29c=20Mon=20Sep=2017=20= 00:00:00=202001=0AFrom:=20"Chao=20Li=20(Evan)"=20=0A= Date:=20Fri,=2030=20Oct=202026=2010:58:59=20+0800=0ASubject:=20[PATCH=20= v1]=20Fix=20error=20handling=20in=20pg_flush_data()=20and=20= FilePrefetch()=0A=0Aposix_fadvise()=20returns=20an=20error=20code=20= directly=20instead=20of=20setting=0Aerrno.=20However,=20pg_flush_data()=20= uses=20%m=20and=20errcode_for_file_access()=0Ato=20report=20errors=20= without=20setting=20errno,=20potentially=20reporting=20an=0Aunrelated=20= error.=20Fix=20this=20by=20assigning=20the=20returned=20error=20code=20= to=0Aerrno=20before=20reporting=20the=20error.=0A=0AFilePrefetch()=20is=20= documented=20to=20return=20an=20errno=20error=20code=20on=20failure,=0A= but=20it=20returns=20-1=20when=20FileAccess()=20fails.=20Fix=20this=20by=20= returning=20errno=0Ainstead.=0A=0AAuthor:=20Chao=20Li=20=0A= ---=0A=20src/backend/storage/file/fd.c=20|=207=20++++---=0A=201=20file=20= changed,=204=20insertions(+),=203=20deletions(-)=0A=0Adiff=20--git=20= a/src/backend/storage/file/fd.c=20b/src/backend/storage/file/fd.c=0A= index=20190c9974494..8c5edc8e17a=20100644=0A---=20= a/src/backend/storage/file/fd.c=0A+++=20b/src/backend/storage/file/fd.c=0A= @@=20-683,9=20+683,10=20@@=20retry:=0A=20=09=09=20*/=0A=20=0A=20=09=09rc=20= =3D=20posix_fadvise(fd,=20offset,=20nbytes,=20POSIX_FADV_DONTNEED);=0A-=0A= =20=09=09if=20(rc=20!=3D=200)=0A=20=09=09{=0A+=09=09=09/*=20for=20= compatibility=20with=20%m=20printing=20etc=20*/=0A+=09=09=09errno=20=3D=20= rc;=0A=20=09=09=09/*=20don't=20error=20out,=20this=20is=20just=20a=20= performance=20optimization=20*/=0A=20=09=09=09ereport(WARNING,=0A=20=09=09= =09=09=09(errcode_for_file_access(),=0A@@=20-2078,7=20+2079,7=20@@=20= FilePrefetch(File=20file,=20pgoff_t=20offset,=20pgoff_t=20amount,=20= uint32=20wait_event_info)=0A=20=0A=20=09=09returnCode=20=3D=20= FileAccess(file);=0A=20=09=09if=20(returnCode=20<=200)=0A-=09=09=09= return=20returnCode;=0A+=09=09=09return=20errno;=0A=20=0A=20retry:=0A=20=09= =09pgstat_report_wait_start(wait_event_info);=0A@@=20-2102,7=20+2103,7=20= @@=20retry:=0A=20=0A=20=09=09returnCode=20=3D=20FileAccess(file);=0A=20=09= =09if=20(returnCode=20<=200)=0A-=09=09=09return=20returnCode;=0A+=09=09=09= return=20errno;=0A=20=0A=20=09=09ra.ra_offset=20=3D=20offset;=0A=20=09=09= ra.ra_count=20=3D=20amount;=0A--=20=0A2.50.1=20(Apple=20Git-155)=0A=0A= --Apple-Mail=_614AEDD1-C5D3-41C1-8CDD-69722B93A103--