From ec497a5ad6a0849efa3fb2fd05988bd8e0c0865d Mon Sep 17 00:00:00 2001 From: Simon Riggs Date: Tue, 22 Mar 2011 23:10:35 +0000 Subject: [PATCH] Make FKs valid at creation when added as column constraints. Bug report from Alvaro Herrera --- src/backend/commands/tablecmds.c | 2 +- src/backend/nodes/copyfuncs.c | 1 + src/backend/nodes/equalfuncs.c | 1 + src/backend/nodes/outfuncs.c | 1 + src/backend/parser/gram.y | 3 +++ src/backend/parser/parse_utilcmd.c | 1 + src/include/nodes/parsenodes.h | 1 + 7 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index f677a8e8bd..596cd86401 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -5595,7 +5595,7 @@ ATAddForeignKeyConstraint(AlteredTableInfo *tab, Relation rel, CONSTRAINT_FOREIGN, fkconstraint->deferrable, fkconstraint->initdeferred, - !fkconstraint->skip_validation, + fkconstraint->initially_valid, RelationGetRelid(rel), fkattnum, numfks, diff --git a/src/backend/nodes/copyfuncs.c b/src/backend/nodes/copyfuncs.c index b903e457a0..0eac9826a4 100644 --- a/src/backend/nodes/copyfuncs.c +++ b/src/backend/nodes/copyfuncs.c @@ -2341,6 +2341,7 @@ _copyConstraint(Constraint *from) COPY_SCALAR_FIELD(fk_upd_action); COPY_SCALAR_FIELD(fk_del_action); COPY_SCALAR_FIELD(skip_validation); + COPY_SCALAR_FIELD(initially_valid); return newnode; } diff --git a/src/backend/nodes/equalfuncs.c b/src/backend/nodes/equalfuncs.c index 10b160f537..c811077563 100644 --- a/src/backend/nodes/equalfuncs.c +++ b/src/backend/nodes/equalfuncs.c @@ -2270,6 +2270,7 @@ _equalConstraint(Constraint *a, Constraint *b) COMPARE_SCALAR_FIELD(fk_upd_action); COMPARE_SCALAR_FIELD(fk_del_action); COMPARE_SCALAR_FIELD(skip_validation); + COMPARE_SCALAR_FIELD(initially_valid); return true; } diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c index 46435ab21c..47f3523366 100644 --- a/src/backend/nodes/outfuncs.c +++ b/src/backend/nodes/outfuncs.c @@ -2625,6 +2625,7 @@ _outConstraint(StringInfo str, Constraint *node) WRITE_CHAR_FIELD(fk_upd_action); WRITE_CHAR_FIELD(fk_del_action); WRITE_BOOL_FIELD(skip_validation); + WRITE_BOOL_FIELD(initially_valid); break; case CONSTR_ATTR_DEFERRABLE: diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index 7c94aadf3b..27fdccae5b 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -2621,6 +2621,7 @@ ColConstraintElem: n->fk_upd_action = (char) ($5 >> 8); n->fk_del_action = (char) ($5 & 0xFF); n->skip_validation = FALSE; + n->initially_valid = true; $$ = (Node *)n; } ; @@ -2820,6 +2821,7 @@ ConstraintElem: n->deferrable = ($11 & 1) != 0; n->initdeferred = ($11 & 2) != 0; n->skip_validation = false; + n->initially_valid = true; $$ = (Node *)n; } | FOREIGN KEY '(' columnList ')' REFERENCES qualified_name @@ -2836,6 +2838,7 @@ ConstraintElem: n->fk_upd_action = (char) ($10 >> 8); n->fk_del_action = (char) ($10 & 0xFF); n->skip_validation = true; + n->initially_valid = false; $$ = (Node *)n; } ; diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index 3dffcded4a..eafc3b3053 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -1696,6 +1696,7 @@ transformFKConstraints(CreateStmtContext *cxt, Constraint *constraint = (Constraint *) lfirst(fkclist); constraint->skip_validation = true; + constraint->initially_valid = true; } } diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h index b7e7104f44..41fdb3e6ba 100644 --- a/src/include/nodes/parsenodes.h +++ b/src/include/nodes/parsenodes.h @@ -1536,6 +1536,7 @@ typedef struct Constraint char fk_upd_action; /* ON UPDATE action */ char fk_del_action; /* ON DELETE action */ bool skip_validation; /* skip validation of existing rows? */ + bool initially_valid; /* start the new constraint as valid */ } Constraint; /* ---------------------- -- 2.40.0