Skip to content

Commit 1e24ea1

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 b2ae077 commit 1e24ea1

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

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

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

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

2533+
table_close(pgclassrel, AccessShareLock);
2534+
25012535
return relname;
25022536
}
25032537

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