Skip to content

Commit 18865f4

Browse files
committed
Create FKs properly when attaching table as partition
Commit f56f8f8 added some code in CloneFkReferencing that's way too lax about a Constraint node it manufactures, not initializing enough struct members -- initially_valid in particular was forgotten. This causes some FKs in partitions added by ALTER TABLE ATTACH PARTITION to be marked as not validated. Set initially_valid true, which fixes the bug. While at it, make the struct initialization more complete. Very similar code was added in two other places by the same commit; make them all follow the same pattern for consistency, though no bugs are apparent there. This bug has never been reported: I only happened to notice while working on commit 614a406. The test case that was added there with the improper result is repaired. Backpatch to 12. Discussion: https://postgr.es/m/20221005105523.bhuhkdx4olajboof@alvherre.pgsql
1 parent 2489c38 commit 18865f4

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

src/backend/commands/tablecmds.c

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9858,14 +9858,21 @@ CloneFkReferenced(Relation parentRel, Relation partitionRel)
98589858
mapped_confkey[i] = attmap->attnums[confkey[i] - 1];
98599859

98609860
fkconstraint = makeNode(Constraint);
9861-
/* for now this is all we need */
9861+
fkconstraint->contype = CONSTRAINT_FOREIGN;
98629862
fkconstraint->conname = NameStr(constrForm->conname);
9863-
fkconstraint->fk_upd_action = constrForm->confupdtype;
9864-
fkconstraint->fk_del_action = constrForm->confdeltype;
98659863
fkconstraint->deferrable = constrForm->condeferrable;
98669864
fkconstraint->initdeferred = constrForm->condeferred;
9867-
fkconstraint->initially_valid = true;
9865+
fkconstraint->location = -1;
9866+
fkconstraint->pktable = NULL;
9867+
/* ->fk_attrs determined below */
9868+
fkconstraint->pk_attrs = NIL;
98689869
fkconstraint->fk_matchtype = constrForm->confmatchtype;
9870+
fkconstraint->fk_upd_action = constrForm->confupdtype;
9871+
fkconstraint->fk_del_action = constrForm->confdeltype;
9872+
fkconstraint->old_conpfeqop = NIL;
9873+
fkconstraint->old_pktable_oid = InvalidOid;
9874+
fkconstraint->skip_validation = false;
9875+
fkconstraint->initially_valid = true;
98699876

98709877
/* set up colnames that are used to generate the constraint name */
98719878
for (int i = 0; i < numfks; i++)
@@ -10037,11 +10044,21 @@ CloneFkReferencing(List **wqueue, Relation parentRel, Relation partRel)
1003710044

1003810045
/* No dice. Set up to create our own constraint */
1003910046
fkconstraint = makeNode(Constraint);
10040-
fkconstraint->fk_upd_action = constrForm->confupdtype;
10041-
fkconstraint->fk_del_action = constrForm->confdeltype;
10047+
fkconstraint->contype = CONSTRAINT_FOREIGN;
10048+
/* ->conname determined below */
1004210049
fkconstraint->deferrable = constrForm->condeferrable;
1004310050
fkconstraint->initdeferred = constrForm->condeferred;
10051+
fkconstraint->location = -1;
10052+
fkconstraint->pktable = NULL;
10053+
/* ->fk_attrs determined below */
10054+
fkconstraint->pk_attrs = NIL;
1004410055
fkconstraint->fk_matchtype = constrForm->confmatchtype;
10056+
fkconstraint->fk_upd_action = constrForm->confupdtype;
10057+
fkconstraint->fk_del_action = constrForm->confdeltype;
10058+
fkconstraint->old_conpfeqop = NIL;
10059+
fkconstraint->old_pktable_oid = InvalidOid;
10060+
fkconstraint->skip_validation = false;
10061+
fkconstraint->initially_valid = true;
1004510062
for (int i = 0; i < numfks; i++)
1004610063
{
1004710064
Form_pg_attribute att;
@@ -18019,11 +18036,21 @@ DetachPartitionFinalize(Relation rel, Relation partRel, bool concurrent,
1801918036
* still do), but now we need separate ones of our own.
1802018037
*/
1802118038
fkconstraint = makeNode(Constraint);
18039+
fkconstraint->contype = CONSTRAINT_FOREIGN;
1802218040
fkconstraint->conname = pstrdup(NameStr(conform->conname));
18023-
fkconstraint->fk_upd_action = conform->confupdtype;
18024-
fkconstraint->fk_del_action = conform->confdeltype;
1802518041
fkconstraint->deferrable = conform->condeferrable;
1802618042
fkconstraint->initdeferred = conform->condeferred;
18043+
fkconstraint->location = -1;
18044+
fkconstraint->pktable = NULL;
18045+
fkconstraint->fk_attrs = NIL;
18046+
fkconstraint->pk_attrs = NIL;
18047+
fkconstraint->fk_matchtype = conform->confmatchtype;
18048+
fkconstraint->fk_upd_action = conform->confupdtype;
18049+
fkconstraint->fk_del_action = conform->confdeltype;
18050+
fkconstraint->old_conpfeqop = NIL;
18051+
fkconstraint->old_pktable_oid = InvalidOid;
18052+
fkconstraint->skip_validation = false;
18053+
fkconstraint->initially_valid = true;
1802718054

1802818055
createForeignKeyActionTriggers(partRel, conform->confrelid,
1802918056
fkconstraint, fk->conoid,

src/test/regress/expected/foreign_key.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1960,7 +1960,7 @@ ORDER BY co.contype, cr.relname, co.conname, p.conname;
19601960
----------------+----------------------------+---------+--------------+----------------------------+--------------+----------------
19611961
part1_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
19621962
part2_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
1963-
part32_self_fk | parted_self_fk_id_abc_fkey | f | f | parted_self_fk_id_abc_fkey | t | parted_self_fk
1963+
part32_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
19641964
part33_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
19651965
part3_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
19661966
parted_self_fk | parted_self_fk_id_abc_fkey | f | t | | | parted_self_fk
@@ -1989,7 +1989,7 @@ ORDER BY co.contype, cr.relname, co.conname, p.conname;
19891989
----------------+----------------------------+---------+--------------+----------------------------+--------------+----------------
19901990
part1_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
19911991
part2_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
1992-
part32_self_fk | parted_self_fk_id_abc_fkey | f | f | parted_self_fk_id_abc_fkey | t | parted_self_fk
1992+
part32_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
19931993
part33_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
19941994
part3_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
19951995
parted_self_fk | parted_self_fk_id_abc_fkey | f | t | | | parted_self_fk

0 commit comments

Comments
 (0)