diff options
Diffstat (limited to 'src/backend')
-rw-r--r-- | src/backend/catalog/pg_constraint.c | 9 | ||||
-rw-r--r-- | src/backend/commands/tablecmds.c | 10 |
2 files changed, 13 insertions, 6 deletions
diff --git a/src/backend/catalog/pg_constraint.c b/src/backend/catalog/pg_constraint.c index 70528679e57..2d5ac1ea813 100644 --- a/src/backend/catalog/pg_constraint.c +++ b/src/backend/catalog/pg_constraint.c @@ -495,6 +495,8 @@ ConstraintNameExists(const char *conname, Oid namespaceid) * name1, name2, and label are used the same way as for makeObjectName(), * except that the label can't be NULL; digits will be appended to the label * if needed to create a name that is unique within the specified namespace. + * If the given label is empty, we only consider names that include at least + * one added digit. * * 'others' can be a list of string names already chosen within the current * command (but not yet reflected into the catalogs); we will not choose @@ -523,8 +525,11 @@ ChooseConstraintName(const char *name1, const char *name2, conDesc = table_open(ConstraintRelationId, AccessShareLock); - /* try the unmodified label first */ - strlcpy(modlabel, label, sizeof(modlabel)); + /* try the unmodified label first, unless it's empty */ + if (label[0] != '\0') + strlcpy(modlabel, label, sizeof(modlabel)); + else + snprintf(modlabel, sizeof(modlabel), "%s%d", label, ++pass); for (;;) { diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 265b1c397fb..2705cf11330 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -10712,14 +10712,16 @@ addFkConstraint(addFkConstraintSides fkside, /* * Caller supplies us with a constraint name; however, it may be used in - * this partition, so come up with a different one in that case. + * this partition, so come up with a different one in that case. Unless + * truncation to NAMEDATALEN dictates otherwise, the new name will be the + * supplied name with an underscore and digit(s) appended. */ if (ConstraintNameIsUsed(CONSTRAINT_RELATION, RelationGetRelid(rel), constraintname)) - conname = ChooseConstraintName(RelationGetRelationName(rel), - ChooseForeignKeyConstraintNameAddition(fkconstraint->fk_attrs), - "fkey", + conname = ChooseConstraintName(constraintname, + NULL, + "", RelationGetNamespace(rel), NIL); else conname = constraintname; |