diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 217ac622c1..3569f86e49 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9177,16 +9177,6 @@ CloneFkReferencing(List **wqueue, Relation parentRel, Relation partRel) /* No dice. Set up to create our own constraint */ fkconstraint = makeNode(Constraint); - if (ConstraintNameIsUsed(CONSTRAINT_RELATION, - RelationGetRelid(partRel), - NameStr(constrForm->conname))) - fkconstraint->conname = - ChooseConstraintName(RelationGetRelationName(partRel), - ChooseForeignKeyConstraintNameAddition(fkconstraint->fk_attrs), - "fkey", - RelationGetNamespace(partRel), NIL); - else - fkconstraint->conname = pstrdup(NameStr(constrForm->conname)); fkconstraint->fk_upd_action = constrForm->confupdtype; fkconstraint->fk_del_action = constrForm->confdeltype; fkconstraint->deferrable = constrForm->condeferrable; @@ -9201,6 +9191,16 @@ CloneFkReferencing(List **wqueue, Relation parentRel, Relation partRel) fkconstraint->fk_attrs = lappend(fkconstraint->fk_attrs, makeString(NameStr(att->attname))); } + if (ConstraintNameIsUsed(CONSTRAINT_RELATION, + RelationGetRelid(partRel), + NameStr(constrForm->conname))) + fkconstraint->conname = + ChooseConstraintName(RelationGetRelationName(partRel), + ChooseForeignKeyConstraintNameAddition(fkconstraint->fk_attrs), + "fkey", + RelationGetNamespace(partRel), NIL); + else + fkconstraint->conname = pstrdup(NameStr(constrForm->conname)); indexOid = constrForm->conindid; constrOid = diff --git a/src/test/regress/input/constraints.source b/src/test/regress/input/constraints.source index c325b2753d..d7996a5d83 100644 --- a/src/test/regress/input/constraints.source +++ b/src/test/regress/input/constraints.source @@ -410,6 +410,25 @@ INSERT INTO parted_uniq_tbl VALUES (1); -- OK now, fail at commit COMMIT; DROP TABLE parted_uniq_tbl; +-- test naming a constraint in a partition when a conflict exists +CREATE TABLE parted_fk_naming ( + id bigint NOT NULL default 1, + id_abc bigint, + CONSTRAINT dummy_constr FOREIGN KEY (id_abc) + REFERENCES parted_fk_naming (id), + PRIMARY KEY (id) +) +PARTITION BY LIST (id); +CREATE TABLE parted_fk_naming_1 ( + id bigint NOT NULL default 1, + id_abc bigint, + PRIMARY KEY (id), + CONSTRAINT dummy_constr CHECK (true) +); +ALTER TABLE parted_fk_naming ATTACH PARTITION parted_fk_naming_1 FOR VALUES IN ('1'); +SELECT conname FROM pg_constraint WHERE conrelid = 'parted_fk_naming_1'::regclass AND contype = 'f'; +DROP TABLE parted_fk_naming; + -- test a HOT update that invalidates the conflicting tuple. -- the trigger should still fire and catch the violation diff --git a/src/test/regress/output/constraints.source b/src/test/regress/output/constraints.source index e27caedcab..4a422442e5 100644 --- a/src/test/regress/output/constraints.source +++ b/src/test/regress/output/constraints.source @@ -575,6 +575,29 @@ COMMIT; ERROR: duplicate key value violates unique constraint "parted_uniq_tbl_1_i_key" DETAIL: Key (i)=(1) already exists. DROP TABLE parted_uniq_tbl; +-- test naming a constraint in a partition when a conflict exists +CREATE TABLE parted_fk_naming ( + id bigint NOT NULL default 1, + id_abc bigint, + CONSTRAINT dummy_constr FOREIGN KEY (id_abc) + REFERENCES parted_fk_naming (id), + PRIMARY KEY (id) +) +PARTITION BY LIST (id); +CREATE TABLE parted_fk_naming_1 ( + id bigint NOT NULL default 1, + id_abc bigint, + PRIMARY KEY (id), + CONSTRAINT dummy_constr CHECK (true) +); +ALTER TABLE parted_fk_naming ATTACH PARTITION parted_fk_naming_1 FOR VALUES IN ('1'); +SELECT conname FROM pg_constraint WHERE conrelid = 'parted_fk_naming_1'::regclass AND contype = 'f'; + conname +-------------------------------- + parted_fk_naming_1_id_abc_fkey +(1 row) + +DROP TABLE parted_fk_naming; -- test a HOT update that invalidates the conflicting tuple. -- the trigger should still fire and catch the violation BEGIN;