Skip to content

Commit 5377f5e

Browse files
knizhnikKonstantin Knizhnik
andauthored
Use smgrexists() instead of access() to enforce uniqueness of generated relfilenumber (#439)
* Use smgrexists() instead of access() to enforce uniqueness of generated relfilenumber * Remove unused local variable --------- Co-authored-by: Konstantin Knizhnik <knizhnik@neon.tech>
1 parent b810fdf commit 5377f5e

File tree

1 file changed

+11
-35
lines changed

1 file changed

+11
-35
lines changed

src/backend/catalog/catalog.c

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -501,10 +501,10 @@ GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
501501
RelFileNumber
502502
GetNewRelFileNumber(Oid reltablespace, Relation pg_class, char relpersistence)
503503
{
504-
RelFileLocatorBackend rlocator;
505-
char *rpath;
504+
RelFileLocator locator;
506505
bool collides;
507506
BackendId backend;
507+
SMgrRelation srel;
508508

509509
/*
510510
* If we ever get here during pg_upgrade, there's something wrong; all
@@ -528,53 +528,29 @@ GetNewRelFileNumber(Oid reltablespace, Relation pg_class, char relpersistence)
528528
}
529529

530530
/* This logic should match RelationInitPhysicalAddr */
531-
rlocator.locator.spcOid = reltablespace ? reltablespace : MyDatabaseTableSpace;
532-
rlocator.locator.dbOid =
533-
(rlocator.locator.spcOid == GLOBALTABLESPACE_OID) ?
531+
locator.spcOid = reltablespace ? reltablespace : MyDatabaseTableSpace;
532+
locator.dbOid =
533+
(locator.spcOid == GLOBALTABLESPACE_OID) ?
534534
InvalidOid : MyDatabaseId;
535535

536-
/*
537-
* The relpath will vary based on the backend ID, so we must initialize
538-
* that properly here to make sure that any collisions based on filename
539-
* are properly detected.
540-
*/
541-
rlocator.backend = backend;
542-
543536
do
544537
{
545538
CHECK_FOR_INTERRUPTS();
546539

547540
/* Generate the OID */
548541
if (pg_class)
549-
rlocator.locator.relNumber = GetNewOidWithIndex(pg_class, ClassOidIndexId,
542+
locator.relNumber = GetNewOidWithIndex(pg_class, ClassOidIndexId,
550543
Anum_pg_class_oid);
551544
else
552-
rlocator.locator.relNumber = GetNewObjectId();
545+
locator.relNumber = GetNewObjectId();
553546

554547
/* Check for existing file of same name */
555-
rpath = relpath(rlocator, MAIN_FORKNUM);
556-
557-
if (access(rpath, F_OK) == 0)
558-
{
559-
/* definite collision */
560-
collides = true;
561-
}
562-
else
563-
{
564-
/*
565-
* Here we have a little bit of a dilemma: if errno is something
566-
* other than ENOENT, should we declare a collision and loop? In
567-
* practice it seems best to go ahead regardless of the errno. If
568-
* there is a colliding file we will get an smgr failure when we
569-
* attempt to create the new relation file.
570-
*/
571-
collides = false;
572-
}
573-
574-
pfree(rpath);
548+
srel = smgropen(locator, backend, relpersistence);
549+
collides = smgrexists(srel, MAIN_FORKNUM);
550+
smgrclose(srel);
575551
} while (collides);
576552

577-
return rlocator.locator.relNumber;
553+
return locator.relNumber;
578554
}
579555

580556
/*

0 commit comments

Comments
 (0)