Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit41b6e7c

Browse files
committed
Create FKs properly when attaching table as partition
Commitf56f8f8 added some code in CloneFkReferencing that's way toolax about a Constraint node it manufactures, not initializing enoughstruct members -- initially_valid in particular was forgotten. Thiscauses some FKs in partitions added by ALTER TABLE ATTACH PARTITION tobe marked as not validated. Set initially_valid true, which fixes thebug.While at it, make the struct initialization more complete. Very similarcode was added in two other places by the same commit; make them allfollow the same pattern for consistency, though no bugs are apparentthere.This bug has never been reported: I only happened to notice whileworking on commit614a406. The test case that was added there withthe improper result is repaired.Backpatch to 12.Discussion:https://postgr.es/m/20221005105523.bhuhkdx4olajboof@alvherre.pgsql
1 parentb00f342 commit41b6e7c

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

94679467
fkconstraint = makeNode(Constraint);
9468-
/* for now this is all we need */
9468+
fkconstraint->contype = CONSTRAINT_FOREIGN;
94699469
fkconstraint->conname = NameStr(constrForm->conname);
9470-
fkconstraint->fk_upd_action = constrForm->confupdtype;
9471-
fkconstraint->fk_del_action = constrForm->confdeltype;
94729470
fkconstraint->deferrable = constrForm->condeferrable;
94739471
fkconstraint->initdeferred = constrForm->condeferred;
9474-
fkconstraint->initially_valid = true;
9472+
fkconstraint->location = -1;
9473+
fkconstraint->pktable = NULL;
9474+
/* ->fk_attrs determined below */
9475+
fkconstraint->pk_attrs = NIL;
94759476
fkconstraint->fk_matchtype = constrForm->confmatchtype;
9477+
fkconstraint->fk_upd_action = constrForm->confupdtype;
9478+
fkconstraint->fk_del_action = constrForm->confdeltype;
9479+
fkconstraint->old_conpfeqop = NIL;
9480+
fkconstraint->old_pktable_oid = InvalidOid;
9481+
fkconstraint->skip_validation = false;
9482+
fkconstraint->initially_valid = true;
94769483

94779484
/* set up colnames that are used to generate the constraint name */
94789485
for (int i = 0; i < numfks; i++)
@@ -9644,11 +9651,21 @@ CloneFkReferencing(List **wqueue, Relation parentRel, Relation partRel)
96449651

96459652
/* No dice. Set up to create our own constraint */
96469653
fkconstraint = makeNode(Constraint);
9647-
fkconstraint->fk_upd_action =constrForm->confupdtype;
9648-
fkconstraint->fk_del_action = constrForm->confdeltype;
9654+
fkconstraint->contype =CONSTRAINT_FOREIGN;
9655+
/* ->conname determined below */
96499656
fkconstraint->deferrable = constrForm->condeferrable;
96509657
fkconstraint->initdeferred = constrForm->condeferred;
9658+
fkconstraint->location = -1;
9659+
fkconstraint->pktable = NULL;
9660+
/* ->fk_attrs determined below */
9661+
fkconstraint->pk_attrs = NIL;
96519662
fkconstraint->fk_matchtype = constrForm->confmatchtype;
9663+
fkconstraint->fk_upd_action = constrForm->confupdtype;
9664+
fkconstraint->fk_del_action = constrForm->confdeltype;
9665+
fkconstraint->old_conpfeqop = NIL;
9666+
fkconstraint->old_pktable_oid = InvalidOid;
9667+
fkconstraint->skip_validation = false;
9668+
fkconstraint->initially_valid = true;
96529669
for (int i = 0; i < numfks; i++)
96539670
{
96549671
Form_pg_attribute att;
@@ -17401,11 +17418,21 @@ ATExecDetachPartition(Relation rel, RangeVar *name)
1740117418
* still do), but now we need separate ones of our own.
1740217419
*/
1740317420
fkconstraint = makeNode(Constraint);
17421+
fkconstraint->contype = CONSTRAINT_FOREIGN;
1740417422
fkconstraint->conname = pstrdup(NameStr(conform->conname));
17405-
fkconstraint->fk_upd_action = conform->confupdtype;
17406-
fkconstraint->fk_del_action = conform->confdeltype;
1740717423
fkconstraint->deferrable = conform->condeferrable;
1740817424
fkconstraint->initdeferred = conform->condeferred;
17425+
fkconstraint->location = -1;
17426+
fkconstraint->pktable = NULL;
17427+
fkconstraint->fk_attrs = NIL;
17428+
fkconstraint->pk_attrs = NIL;
17429+
fkconstraint->fk_matchtype = conform->confmatchtype;
17430+
fkconstraint->fk_upd_action = conform->confupdtype;
17431+
fkconstraint->fk_del_action = conform->confdeltype;
17432+
fkconstraint->old_conpfeqop = NIL;
17433+
fkconstraint->old_pktable_oid = InvalidOid;
17434+
fkconstraint->skip_validation = false;
17435+
fkconstraint->initially_valid = true;
1740917436

1741017437
createForeignKeyActionTriggers(partRel, conform->confrelid,
1741117438
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)

[8]ページ先頭

©2009-2025 Movatter.jp