Skip to content

Commit ab70b3a

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 d9ffccf commit ab70b3a

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
@@ -9010,14 +9010,21 @@ CloneFkReferenced(Relation parentRel, Relation partitionRel)
90109010
mapped_confkey[i] = attmap[confkey[i] - 1];
90119011

90129012
fkconstraint = makeNode(Constraint);
9013-
/* for now this is all we need */
9013+
fkconstraint->contype = CONSTRAINT_FOREIGN;
90149014
fkconstraint->conname = NameStr(constrForm->conname);
9015-
fkconstraint->fk_upd_action = constrForm->confupdtype;
9016-
fkconstraint->fk_del_action = constrForm->confdeltype;
90179015
fkconstraint->deferrable = constrForm->condeferrable;
90189016
fkconstraint->initdeferred = constrForm->condeferred;
9019-
fkconstraint->initially_valid = true;
9017+
fkconstraint->location = -1;
9018+
fkconstraint->pktable = NULL;
9019+
/* ->fk_attrs determined below */
9020+
fkconstraint->pk_attrs = NIL;
90209021
fkconstraint->fk_matchtype = constrForm->confmatchtype;
9022+
fkconstraint->fk_upd_action = constrForm->confupdtype;
9023+
fkconstraint->fk_del_action = constrForm->confdeltype;
9024+
fkconstraint->old_conpfeqop = NIL;
9025+
fkconstraint->old_pktable_oid = InvalidOid;
9026+
fkconstraint->skip_validation = false;
9027+
fkconstraint->initially_valid = true;
90219028

90229029
/* set up colnames that are used to generate the constraint name */
90239030
for (int i = 0; i < numfks; i++)
@@ -9190,11 +9197,21 @@ CloneFkReferencing(List **wqueue, Relation parentRel, Relation partRel)
91909197

91919198
/* No dice. Set up to create our own constraint */
91929199
fkconstraint = makeNode(Constraint);
9193-
fkconstraint->fk_upd_action = constrForm->confupdtype;
9194-
fkconstraint->fk_del_action = constrForm->confdeltype;
9200+
fkconstraint->contype = CONSTRAINT_FOREIGN;
9201+
/* ->conname determined below */
91959202
fkconstraint->deferrable = constrForm->condeferrable;
91969203
fkconstraint->initdeferred = constrForm->condeferred;
9204+
fkconstraint->location = -1;
9205+
fkconstraint->pktable = NULL;
9206+
/* ->fk_attrs determined below */
9207+
fkconstraint->pk_attrs = NIL;
91979208
fkconstraint->fk_matchtype = constrForm->confmatchtype;
9209+
fkconstraint->fk_upd_action = constrForm->confupdtype;
9210+
fkconstraint->fk_del_action = constrForm->confdeltype;
9211+
fkconstraint->old_conpfeqop = NIL;
9212+
fkconstraint->old_pktable_oid = InvalidOid;
9213+
fkconstraint->skip_validation = false;
9214+
fkconstraint->initially_valid = true;
91989215
for (int i = 0; i < numfks; i++)
91999216
{
92009217
Form_pg_attribute att;
@@ -17005,11 +17022,21 @@ ATExecDetachPartition(Relation rel, RangeVar *name)
1700517022
* still do), but now we need separate ones of our own.
1700617023
*/
1700717024
fkconstraint = makeNode(Constraint);
17025+
fkconstraint->contype = CONSTRAINT_FOREIGN;
1700817026
fkconstraint->conname = pstrdup(NameStr(conform->conname));
17009-
fkconstraint->fk_upd_action = conform->confupdtype;
17010-
fkconstraint->fk_del_action = conform->confdeltype;
1701117027
fkconstraint->deferrable = conform->condeferrable;
1701217028
fkconstraint->initdeferred = conform->condeferred;
17029+
fkconstraint->location = -1;
17030+
fkconstraint->pktable = NULL;
17031+
fkconstraint->fk_attrs = NIL;
17032+
fkconstraint->pk_attrs = NIL;
17033+
fkconstraint->fk_matchtype = conform->confmatchtype;
17034+
fkconstraint->fk_upd_action = conform->confupdtype;
17035+
fkconstraint->fk_del_action = conform->confdeltype;
17036+
fkconstraint->old_conpfeqop = NIL;
17037+
fkconstraint->old_pktable_oid = InvalidOid;
17038+
fkconstraint->skip_validation = false;
17039+
fkconstraint->initially_valid = true;
1701317040

1701417041
createForeignKeyActionTriggers(partRel, conform->confrelid,
1701517042
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)