Skip to content

Commit 4b66cb1

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 1230be1 commit 4b66cb1

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
@@ -2358,7 +2358,9 @@ makeObjectName(const char *name1, const char *name2, const char *label)
23582358
* constraint names.)
23592359
*
23602360
* Note: it is theoretically possible to get a collision anyway, if someone
2361-
* else chooses the same name concurrently. This is fairly unlikely to be
2361+
* else chooses the same name concurrently. We shorten the race condition
2362+
* window by checking for conflicting relations using SnapshotDirty, but
2363+
* that doesn't close the window entirely. This is fairly unlikely to be
23622364
* a problem in practice, especially if one is holding an exclusive lock on
23632365
* the relation identified by name1. However, if choosing multiple names
23642366
* within a single command, you'd better create the new object and do
@@ -2374,15 +2376,45 @@ ChooseRelationName(const char *name1, const char *name2,
23742376
int pass = 0;
23752377
char *relname = NULL;
23762378
char modlabel[NAMEDATALEN];
2379+
SnapshotData SnapshotDirty;
2380+
Relation pgclassrel;
2381+
2382+
/* prepare to search pg_class with a dirty snapshot */
2383+
InitDirtySnapshot(SnapshotDirty);
2384+
pgclassrel = table_open(RelationRelationId, AccessShareLock);
23772385

23782386
/* try the unmodified label first */
23792387
StrNCpy(modlabel, label, sizeof(modlabel));
23802388

23812389
for (;;)
23822390
{
2391+
ScanKeyData key[2];
2392+
SysScanDesc scan;
2393+
bool collides;
2394+
23832395
relname = makeObjectName(name1, name2, modlabel);
23842396

2385-
if (!OidIsValid(get_relname_relid(relname, namespaceid)))
2397+
/* is there any conflicting relation name? */
2398+
ScanKeyInit(&key[0],
2399+
Anum_pg_class_relname,
2400+
BTEqualStrategyNumber, F_NAMEEQ,
2401+
CStringGetDatum(relname));
2402+
ScanKeyInit(&key[1],
2403+
Anum_pg_class_relnamespace,
2404+
BTEqualStrategyNumber, F_OIDEQ,
2405+
ObjectIdGetDatum(namespaceid));
2406+
2407+
scan = systable_beginscan(pgclassrel, ClassNameNspIndexId,
2408+
true /* indexOK */ ,
2409+
&SnapshotDirty,
2410+
2, key);
2411+
2412+
collides = HeapTupleIsValid(systable_getnext(scan));
2413+
2414+
systable_endscan(scan);
2415+
2416+
/* break out of loop if no conflict */
2417+
if (!collides)
23862418
{
23872419
if (!isconstraint ||
23882420
!ConstraintNameExists(relname, namespaceid))
@@ -2394,6 +2426,8 @@ ChooseRelationName(const char *name1, const char *name2,
23942426
snprintf(modlabel, sizeof(modlabel), "%s%d", label, ++pass);
23952427
}
23962428

2429+
table_close(pgclassrel, AccessShareLock);
2430+
23972431
return relname;
23982432
}
23992433

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