* ALTER TABLE ADD CONSTRAINT * * Return value is the address of the new constraint; if no constraint was * added, InvalidObjectAddress is returned. */
| 10704 | * added, InvalidObjectAddress is returned. |
| 10705 | */ |
| 10706 | static ObjectAddress |
| 10707 | ATExecAddConstraint(List **wqueue, AlteredTableInfo *tab, Relation rel, |
| 10708 | Constraint *newConstraint, bool recurse, bool is_readd, |
| 10709 | LOCKMODE lockmode) |
| 10710 | { |
| 10711 | ObjectAddress address = InvalidObjectAddress; |
| 10712 | |
| 10713 | Assert(IsA(newConstraint, Constraint)); |
| 10714 | |
| 10715 | /* |
| 10716 | * Currently, we only expect to see CONSTR_CHECK and CONSTR_FOREIGN nodes |
| 10717 | * arriving here (see the preprocessing done in parse_utilcmd.c). Use a |
| 10718 | * switch anyway to make it easier to add more code later. |
| 10719 | */ |
| 10720 | switch (newConstraint->contype) |
| 10721 | { |
| 10722 | case CONSTR_CHECK: |
| 10723 | address = |
| 10724 | ATAddCheckConstraint(wqueue, tab, rel, |
| 10725 | newConstraint, recurse, false, is_readd, |
| 10726 | lockmode); |
| 10727 | break; |
| 10728 | |
| 10729 | case CONSTR_FOREIGN: |
| 10730 | |
| 10731 | /* |
| 10732 | * Assign or validate constraint name |
| 10733 | */ |
| 10734 | if (newConstraint->conname) |
| 10735 | { |
| 10736 | if (ConstraintNameIsUsed(CONSTRAINT_RELATION, |
| 10737 | RelationGetRelid(rel), |
| 10738 | newConstraint->conname)) |
| 10739 | ereport(ERROR, |
| 10740 | (errcode(ERRCODE_DUPLICATE_OBJECT), |
| 10741 | errmsg("constraint \"%s\" for relation \"%s\" already exists", |
| 10742 | newConstraint->conname, |
| 10743 | RelationGetRelationName(rel)))); |
| 10744 | } |
| 10745 | else |
| 10746 | newConstraint->conname = |
| 10747 | ChooseConstraintName(RelationGetRelationName(rel), |
| 10748 | ChooseForeignKeyConstraintNameAddition(newConstraint->fk_attrs), |
| 10749 | "fkey", |
| 10750 | RelationGetNamespace(rel), |
| 10751 | NIL); |
| 10752 | |
| 10753 | address = ATAddForeignKeyConstraint(wqueue, tab, rel, |
| 10754 | newConstraint, |
| 10755 | recurse, false, |
| 10756 | lockmode); |
| 10757 | break; |
| 10758 | |
| 10759 | default: |
| 10760 | elog(ERROR, "unrecognized constraint type: %d", |
| 10761 | (int) newConstraint->contype); |
| 10762 | } |
| 10763 |
no test coverage detected