Skip to content

[pull] master from postgres:master #981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions src/backend/commands/indexcmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -2592,7 +2592,9 @@ makeObjectName(const char *name1, const char *name2, const char *label)
* constraint names.)
*
* Note: it is theoretically possible to get a collision anyway, if someone
* else chooses the same name concurrently. This is fairly unlikely to be
* else chooses the same name concurrently. We shorten the race condition
* window by checking for conflicting relations using SnapshotDirty, but
* that doesn't close the window entirely. This is fairly unlikely to be
* a problem in practice, especially if one is holding an exclusive lock on
* the relation identified by name1. However, if choosing multiple names
* within a single command, you'd better create the new object and do
Expand All @@ -2608,15 +2610,45 @@ ChooseRelationName(const char *name1, const char *name2,
int pass = 0;
char *relname = NULL;
char modlabel[NAMEDATALEN];
SnapshotData SnapshotDirty;
Relation pgclassrel;

/* prepare to search pg_class with a dirty snapshot */
InitDirtySnapshot(SnapshotDirty);
pgclassrel = table_open(RelationRelationId, AccessShareLock);

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

for (;;)
{
ScanKeyData key[2];
SysScanDesc scan;
bool collides;

relname = makeObjectName(name1, name2, modlabel);

if (!OidIsValid(get_relname_relid(relname, namespaceid)))
/* is there any conflicting relation name? */
ScanKeyInit(&key[0],
Anum_pg_class_relname,
BTEqualStrategyNumber, F_NAMEEQ,
CStringGetDatum(relname));
ScanKeyInit(&key[1],
Anum_pg_class_relnamespace,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(namespaceid));

scan = systable_beginscan(pgclassrel, ClassNameNspIndexId,
true /* indexOK */ ,
&SnapshotDirty,
2, key);

collides = HeapTupleIsValid(systable_getnext(scan));

systable_endscan(scan);

/* break out of loop if no conflict */
if (!collides)
{
if (!isconstraint ||
!ConstraintNameExists(relname, namespaceid))
Expand All @@ -2628,6 +2660,8 @@ ChooseRelationName(const char *name1, const char *name2,
snprintf(modlabel, sizeof(modlabel), "%s%d", label, ++pass);
}

table_close(pgclassrel, AccessShareLock);

return relname;
}

Expand Down
3 changes: 0 additions & 3 deletions src/makefiles/pgxs.mk
Original file line number Diff line number Diff line change
Expand Up @@ -376,10 +376,7 @@ endif
ifdef REGRESS
# things created by various check targets
rm -rf $(pg_regress_clean_files)
ifeq ($(PORTNAME), win)
rm -f regress.def
endif
endif # REGRESS
ifdef TAP_TESTS
rm -rf tmp_check/
endif
Expand Down