Skip to content

Commit fdd8269

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 5ed50f9 commit fdd8269

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
@@ -2461,7 +2461,9 @@ makeObjectName(const char *name1, const char *name2, const char *label)
24612461
* constraint names.)
24622462
*
24632463
* Note: it is theoretically possible to get a collision anyway, if someone
2464-
* else chooses the same name concurrently. This is fairly unlikely to be
2464+
* else chooses the same name concurrently. We shorten the race condition
2465+
* window by checking for conflicting relations using SnapshotDirty, but
2466+
* that doesn't close the window entirely. This is fairly unlikely to be
24652467
* a problem in practice, especially if one is holding an exclusive lock on
24662468
* the relation identified by name1. However, if choosing multiple names
24672469
* within a single command, you'd better create the new object and do
@@ -2477,15 +2479,45 @@ ChooseRelationName(const char *name1, const char *name2,
24772479
int pass = 0;
24782480
char *relname = NULL;
24792481
char modlabel[NAMEDATALEN];
2482+
SnapshotData SnapshotDirty;
2483+
Relation pgclassrel;
2484+
2485+
/* prepare to search pg_class with a dirty snapshot */
2486+
InitDirtySnapshot(SnapshotDirty);
2487+
pgclassrel = table_open(RelationRelationId, AccessShareLock);
24802488

24812489
/* try the unmodified label first */
24822490
strlcpy(modlabel, label, sizeof(modlabel));
24832491

24842492
for (;;)
24852493
{
2494+
ScanKeyData key[2];
2495+
SysScanDesc scan;
2496+
bool collides;
2497+
24862498
relname = makeObjectName(name1, name2, modlabel);
24872499

2488-
if (!OidIsValid(get_relname_relid(relname, namespaceid)))
2500+
/* is there any conflicting relation name? */
2501+
ScanKeyInit(&key[0],
2502+
Anum_pg_class_relname,
2503+
BTEqualStrategyNumber, F_NAMEEQ,
2504+
CStringGetDatum(relname));
2505+
ScanKeyInit(&key[1],
2506+
Anum_pg_class_relnamespace,
2507+
BTEqualStrategyNumber, F_OIDEQ,
2508+
ObjectIdGetDatum(namespaceid));
2509+
2510+
scan = systable_beginscan(pgclassrel, ClassNameNspIndexId,
2511+
true /* indexOK */ ,
2512+
&SnapshotDirty,
2513+
2, key);
2514+
2515+
collides = HeapTupleIsValid(systable_getnext(scan));
2516+
2517+
systable_endscan(scan);
2518+
2519+
/* break out of loop if no conflict */
2520+
if (!collides)
24892521
{
24902522
if (!isconstraint ||
24912523
!ConstraintNameExists(relname, namespaceid))
@@ -2497,6 +2529,8 @@ ChooseRelationName(const char *name1, const char *name2,
24972529
snprintf(modlabel, sizeof(modlabel), "%s%d", label, ++pass);
24982530
}
24992531

2532+
table_close(pgclassrel, AccessShareLock);
2533+
25002534
return relname;
25012535
}
25022536

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