Skip to content

Commit 677da8c

Browse files
committed
Use access() to check file existence in GetNewRelFileNode()
Previous code used BasicOpenFile() and close() just to check for a file collision, while there is no need to hold open a file descriptor but that's an overkill here. Author: Paul Guo Reviewed-by: Peter Eisentraut, Michael Paquier Discussion: https://postgr.es/m/CABQrizcUtiHaquxK=d4etBX8GF9kbZB50Nt1gO9_aN-e9SptyQ@mail.gmail.com
1 parent 0903bbd commit 677da8c

File tree

1 file changed

+4
-11
lines changed

1 file changed

+4
-11
lines changed

src/backend/catalog/catalog.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,6 @@ GetNewRelFileNode(Oid reltablespace, Relation pg_class, char relpersistence)
397397
{
398398
RelFileNodeBackend rnode;
399399
char *rpath;
400-
int fd;
401400
bool collides;
402401
BackendId backend;
403402

@@ -445,26 +444,20 @@ GetNewRelFileNode(Oid reltablespace, Relation pg_class, char relpersistence)
445444

446445
/* Check for existing file of same name */
447446
rpath = relpath(rnode, MAIN_FORKNUM);
448-
fd = BasicOpenFile(rpath, O_RDONLY | PG_BINARY);
449447

450-
if (fd >= 0)
448+
if (access(rpath, F_OK) == 0)
451449
{
452450
/* definite collision */
453-
close(fd);
454451
collides = true;
455452
}
456453
else
457454
{
458455
/*
459456
* Here we have a little bit of a dilemma: if errno is something
460457
* other than ENOENT, should we declare a collision and loop? In
461-
* particular one might think this advisable for, say, EPERM.
462-
* However there really shouldn't be any unreadable files in a
463-
* tablespace directory, and if the EPERM is actually complaining
464-
* that we can't read the directory itself, we'd be in an infinite
465-
* loop. In practice it seems best to go ahead regardless of the
466-
* errno. If there is a colliding file we will get an smgr
467-
* failure when we attempt to create the new relation file.
458+
* practice it seems best to go ahead regardless of the errno. If
459+
* there is a colliding file we will get an smgr failure when we
460+
* attempt to create the new relation file.
468461
*/
469462
collides = false;
470463
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy