1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-20 15:22:23 +03:00

Include all columns in default names for foreign key constraints

When creating a name for a foreign key constraint when none is
specified, use all column names instead of only the first one, similar
to how it is already done for index names.

Author: Paul Martinez <hellopfm@gmail.com>
Reviewed-by: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
Discussion: https://www.postgresql.org/message-id/flat/CAF+2_SFjky6XRfLNRXpkG97W6PRbOO_mjAxqXzAAimU=c7w7_A@mail.gmail.com
This commit is contained in:
Peter Eisentraut
2019-03-13 14:15:37 +01:00
parent bbb96c3704
commit f177660ab0
7 changed files with 152 additions and 76 deletions

View File

@ -402,6 +402,7 @@ static ObjectAddress ATExecAddConstraint(List **wqueue,
AlteredTableInfo *tab, Relation rel,
Constraint *newConstraint, bool recurse, bool is_readd,
LOCKMODE lockmode);
static char *ChooseForeignKeyConstraintNameAddition(List *colnames);
static ObjectAddress ATExecAddIndexConstraint(AlteredTableInfo *tab, Relation rel,
IndexStmt *stmt, LOCKMODE lockmode);
static ObjectAddress ATAddCheckConstraint(List **wqueue,
@ -7191,7 +7192,7 @@ ATExecAddConstraint(List **wqueue, AlteredTableInfo *tab, Relation rel,
else
newConstraint->conname =
ChooseConstraintName(RelationGetRelationName(rel),
strVal(linitial(newConstraint->fk_attrs)),
ChooseForeignKeyConstraintNameAddition(newConstraint->fk_attrs),
"fkey",
RelationGetNamespace(rel),
NIL);
@ -7210,6 +7211,45 @@ ATExecAddConstraint(List **wqueue, AlteredTableInfo *tab, Relation rel,
return address;
}
/*
* Generate the column-name portion of the constraint name for a new foreign
* key given the list of column names that reference the referenced
* table. This will be passed to ChooseConstraintName along with the parent
* table name and the "fkey" suffix.
*
* We know that less than NAMEDATALEN characters will actually be used, so we
* can truncate the result once we've generated that many.
*
* XXX see also ChooseExtendedStatisticNameAddition and
* ChooseIndexNameAddition.
*/
static char *
ChooseForeignKeyConstraintNameAddition(List *colnames)
{
char buf[NAMEDATALEN * 2];
int buflen = 0;
ListCell *lc;
buf[0] = '\0';
foreach(lc, colnames)
{
const char *name = strVal(lfirst(lc));
if (buflen > 0)
buf[buflen++] = '_'; /* insert _ between names */
/*
* At this point we have buflen <= NAMEDATALEN. name should be less
* than NAMEDATALEN already, but use strlcpy for paranoia.
*/
strlcpy(buf + buflen, name, NAMEDATALEN);
buflen += strlen(buf + buflen);
if (buflen >= NAMEDATALEN)
break;
}
return pstrdup(buf);
}
/*
* Add a check constraint to a single table and its children. Returns the
* address of the constraint added to the parent relation, if one gets added,