Skip to content

Commit 27af8b9

Browse files
committed
Use SnapshotDirty when checking for conflicting index names.
While choosing an autogenerated name for an index, look for pre-existing relations using a SnapshotDirty snapshot, instead of the previous behavior that considered only committed-good pg_class rows. This allows us to detect and avoid conflicts against indexes that are still being built. It's still possible to fail due to a race condition, but the window is now just the amount of time that it takes DefineIndex to validate all its parameters, call smgrcreate(), and enter the index's pg_class row. Formerly the race window covered the entire time needed to create and fill an index, which could be very long if the table is large. Worse, if the conflicting index creation is part of a larger transaction, it wouldn't be visible till COMMIT. So this isn't a complete solution, but it should greatly ameliorate the problem, and the patch is simple enough to be back-patchable. It might at some point be useful to do the same for pg_constraint entries (cf. ChooseConstraintName, ConstraintNameExists, and related functions). However, in the absence of field complaints, I'll leave that alone for now. The relation-name test should be good enough for index-based constraints, while foreign-key constraints seem to be okay since they require exclusive locks to create. Bug: #18959 Reported-by: Maximilian Chrzan <maximilian.chrzan@here.com> Author: Tom Lane <tgl@sss.pgh.pa.us> Reviewed-by: Dilip Kumar <dilipbalaut@gmail.com> Discussion: https://postgr.es/m/18959-f63b53b864bb1417@postgresql.org Backpatch-through: 13
1 parent 983b363 commit 27af8b9

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

src/backend/commands/indexcmds.c

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2411,7 +2411,9 @@ makeObjectName(const char *name1, const char *name2, const char *label)
24112411
* constraint names.)
24122412
*
24132413
* Note: it is theoretically possible to get a collision anyway, if someone
2414-
* else chooses the same name concurrently. This is fairly unlikely to be
2414+
* else chooses the same name concurrently. We shorten the race condition
2415+
* window by checking for conflicting relations using SnapshotDirty, but
2416+
* that doesn't close the window entirely. This is fairly unlikely to be
24152417
* a problem in practice, especially if one is holding an exclusive lock on
24162418
* the relation identified by name1. However, if choosing multiple names
24172419
* within a single command, you'd better create the new object and do
@@ -2427,15 +2429,45 @@ ChooseRelationName(const char *name1, const char *name2,
24272429
int pass = 0;
24282430
char *relname = NULL;
24292431
char modlabel[NAMEDATALEN];
2432+
SnapshotData SnapshotDirty;
2433+
Relation pgclassrel;
2434+
2435+
/* prepare to search pg_class with a dirty snapshot */
2436+
InitDirtySnapshot(SnapshotDirty);
2437+
pgclassrel = table_open(RelationRelationId, AccessShareLock);
24302438

24312439
/* try the unmodified label first */
24322440
strlcpy(modlabel, label, sizeof(modlabel));
24332441

24342442
for (;;)
24352443
{
2444+
ScanKeyData key[2];
2445+
SysScanDesc scan;
2446+
bool collides;
2447+
24362448
relname = makeObjectName(name1, name2, modlabel);
24372449

2438-
if (!OidIsValid(get_relname_relid(relname, namespaceid)))
2450+
/* is there any conflicting relation name? */
2451+
ScanKeyInit(&key[0],
2452+
Anum_pg_class_relname,
2453+
BTEqualStrategyNumber, F_NAMEEQ,
2454+
CStringGetDatum(relname));
2455+
ScanKeyInit(&key[1],
2456+
Anum_pg_class_relnamespace,
2457+
BTEqualStrategyNumber, F_OIDEQ,
2458+
ObjectIdGetDatum(namespaceid));
2459+
2460+
scan = systable_beginscan(pgclassrel, ClassNameNspIndexId,
2461+
true /* indexOK */ ,
2462+
&SnapshotDirty,
2463+
2, key);
2464+
2465+
collides = HeapTupleIsValid(systable_getnext(scan));
2466+
2467+
systable_endscan(scan);
2468+
2469+
/* break out of loop if no conflict */
2470+
if (!collides)
24392471
{
24402472
if (!isconstraint ||
24412473
!ConstraintNameExists(relname, namespaceid))
@@ -2447,6 +2479,8 @@ ChooseRelationName(const char *name1, const char *name2,
24472479
snprintf(modlabel, sizeof(modlabel), "%s%d", label, ++pass);
24482480
}
24492481

2482+
table_close(pgclassrel, AccessShareLock);
2483+
24502484
return relname;
24512485
}
24522486

0 commit comments

Comments
 (0)