Choose FK name correctly during partition attachment
authorAlvaro Herrera <alvherre@alvh.no-ip.org>
Thu, 8 Sep 2022 11:17:02 +0000 (13:17 +0200)
committerAlvaro Herrera <alvherre@alvh.no-ip.org>
Thu, 8 Sep 2022 11:17:02 +0000 (13:17 +0200)
During ALTER TABLE ATTACH PARTITION, if the name of a parent's foreign
key constraint is already used on the partition, the code tries to
choose another one before the FK attributes list has been populated,
so the resulting constraint name was "<relname>__fkey" instead of
"<relname>_<attrs>_fkey".  Repair, and add a test case.

Backpatch to 12.  In 11, the code to attach a partition was not smart
enough to cope with conflicting constraint names, so the problem doesn't
exist there.

Author: Jehan-Guillaume de Rorthais <jgdr@dalibo.com>
Discussion: https://postgr.es/m/20220901184156.738ebee5@karst

src/backend/commands/tablecmds.c
src/test/regress/input/constraints.source
src/test/regress/output/constraints.source

index 6c5a36400935384b4ce5890d2f2c35facff521e9..3df5c5681f3a5fdd00963dff3d48f517edea5394 100644 (file)
@@ -9631,16 +9631,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;
@@ -9655,6 +9645,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 =
index c325b2753d47a165dcdacfaffdc2bbae65175293..d7996a5d8377dec8992df1cc90d85e3c5b5a7396 100644 (file)
@@ -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
 
index b727c6150ae197f9b4c5963a4777e4897862fa5f..5d60451b9a1ea2113f079eae676fc8f3aa747f84 100644 (file)
@@ -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;