checking return value from unlink in write_relcache_init_file

Started by Zhihong Yuover 5 years ago6 messageshackers
Beta feature

Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.

appliessuccessCI history

You can run a PostgreSQL built from this patch straight from Docker, with no checkout and no build:

docker run --rm -p 5432:5432 ghcr.io/hackorum-dev/postgres-patch:t44328
psql -h localhost -U postgres

Built from patchset v6 (message #6), September 20, 2026 at 11:46 AM.

Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:

git clone --branch t44328_6 https://github.com/hackorum-dev/postgres.git

In a checkout you already have, add the fork once:

git remote add hackorum https://github.com/hackorum-dev/postgres.git

then, for this patchset and every later one:

git fetch hackorum t44328_6 && git checkout t44328_6

Patchset v6 (message #6) is on t44328_6

Jump to latest
#1Zhihong Yu
zyu@yugabyte.com

Hi,
I was looking at write_relcache_init_file() where an attempt is made to
unlink the tempfilename.

However, the return value is not checked.
If the tempfilename is not removed (the file exists), I think we should log
a warning and proceed.

Please comment on the proposed patch.

Thanks

Attachments:

check-unlink-return-init-file.patchapplication/octet-stream; name=check-unlink-return-init-file.patchDownload+11-1
#2Justin Pryzby
pryzby@telsasoft.com
In reply to: Zhihong Yu (#1)
Re: checking return value from unlink in write_relcache_init_file

On Thu, Jun 03, 2021 at 03:44:13PM -0700, Zhihong Yu wrote:

Hi,
I was looking at write_relcache_init_file() where an attempt is made to
unlink the tempfilename.

However, the return value is not checked.
If the tempfilename is not removed (the file exists), I think we should log
a warning and proceed.

Please comment on the proposed patch.

-       unlink(tempfilename);           /* in case it exists w/wrong permissions */
+       /* in case it exists w/wrong permissions */
+       if (unlink(tempfilename) && errno != ENOENT)
+       {
+               ereport(WARNING,
+                               (errcode_for_file_access(),
+                                errmsg("could not unlink relation-cache initialization file \"%s\": %m",
+                                               tempfilename),
+                                errdetail("Continuing anyway, but there's something wrong.")));
+               return;
+       }
+

fp = AllocateFile(tempfilename, PG_BINARY_W);

The comment here is instructive: the unlink is in advance of AllocateFile(),
and if the file exists with wrong permissions, then AllocateFile would itself fail,
and then issue a warning:

errmsg("could not create relation-cache initialization file \"%s\": %m",
tempfilename),
errdetail("Continuing anyway, but there's something wrong.")));

In that context, I don't think it's needed to check the return of unlink().

--
Justin

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Zhihong Yu (#1)
Re: checking return value from unlink in write_relcache_init_file

Zhihong Yu <zyu@yugabyte.com> writes:

Please comment on the proposed patch.

If the unlink fails, there's only really a problem if the subsequent
open() fails to overwrite the file --- and that stanza is perfectly
capable of complaining for itself. So I think the code is fine and
there's no need for a separate message about the unlink. Refusing to
proceed, as you've done here, is strictly worse than what we have.

regards, tom lane

#4Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Tom Lane (#3)
Re: checking return value from unlink in write_relcache_init_file

On 2021-Jun-03, Tom Lane wrote:

If the unlink fails, there's only really a problem if the subsequent
open() fails to overwrite the file --- and that stanza is perfectly
capable of complaining for itself. So I think the code is fine and
there's no need for a separate message about the unlink. Refusing to
proceed, as you've done here, is strictly worse than what we have.

It does seem to deserve a comment explaining this.

--
�lvaro Herrera Valdivia, Chile
"Pensar que el espectro que vemos es ilusorio no lo despoja de espanto,
s�lo le suma el nuevo terror de la locura" (Perelandra, C.S. Lewis)

#5Tom Lane
tgl@sss.pgh.pa.us
In reply to: Alvaro Herrera (#4)
Re: checking return value from unlink in write_relcache_init_file

Alvaro Herrera <alvherre@alvh.no-ip.org> writes:

On 2021-Jun-03, Tom Lane wrote:

If the unlink fails, there's only really a problem if the subsequent
open() fails to overwrite the file --- and that stanza is perfectly
capable of complaining for itself. So I think the code is fine and
there's no need for a separate message about the unlink. Refusing to
proceed, as you've done here, is strictly worse than what we have.

It does seem to deserve a comment explaining this.

Agreed, the existing comment there is a tad terse.

regards, tom lane

#6Zhihong Yu
zyu@yugabyte.com
In reply to: Tom Lane (#5)
Re: checking return value from unlink in write_relcache_init_file

On Thu, Jun 3, 2021 at 6:16 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:

Alvaro Herrera <alvherre@alvh.no-ip.org> writes:

On 2021-Jun-03, Tom Lane wrote:

If the unlink fails, there's only really a problem if the subsequent
open() fails to overwrite the file --- and that stanza is perfectly
capable of complaining for itself. So I think the code is fine and
there's no need for a separate message about the unlink. Refusing to
proceed, as you've done here, is strictly worse than what we have.

It does seem to deserve a comment explaining this.

Agreed, the existing comment there is a tad terse.

regards, tom lane

Hi,
Here is the patch with a bit more comment on the unlink() call.

Cheers

Attachments:

t44328_6
comment-for-not-checking-unlink-return.patchapplication/octet-stream; name=comment-for-not-checking-unlink-return.patchDownload+6-1