Skip to content

Commit ec497a5

Browse files
Make FKs valid at creation when added as column constraints.
Bug report from Alvaro Herrera
1 parent 5d1d679 commit ec497a5

File tree

7 files changed

+9
-1
lines changed

7 files changed

+9
-1
lines changed

src/backend/commands/tablecmds.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5595,7 +5595,7 @@ ATAddForeignKeyConstraint(AlteredTableInfo *tab, Relation rel,
55955595
CONSTRAINT_FOREIGN,
55965596
fkconstraint->deferrable,
55975597
fkconstraint->initdeferred,
5598-
!fkconstraint->skip_validation,
5598+
fkconstraint->initially_valid,
55995599
RelationGetRelid(rel),
56005600
fkattnum,
56015601
numfks,

src/backend/nodes/copyfuncs.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2341,6 +2341,7 @@ _copyConstraint(Constraint *from)
23412341
COPY_SCALAR_FIELD(fk_upd_action);
23422342
COPY_SCALAR_FIELD(fk_del_action);
23432343
COPY_SCALAR_FIELD(skip_validation);
2344+
COPY_SCALAR_FIELD(initially_valid);
23442345

23452346
return newnode;
23462347
}

src/backend/nodes/equalfuncs.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2270,6 +2270,7 @@ _equalConstraint(Constraint *a, Constraint *b)
22702270
COMPARE_SCALAR_FIELD(fk_upd_action);
22712271
COMPARE_SCALAR_FIELD(fk_del_action);
22722272
COMPARE_SCALAR_FIELD(skip_validation);
2273+
COMPARE_SCALAR_FIELD(initially_valid);
22732274

22742275
return true;
22752276
}

src/backend/nodes/outfuncs.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2625,6 +2625,7 @@ _outConstraint(StringInfo str, Constraint *node)
26252625
WRITE_CHAR_FIELD(fk_upd_action);
26262626
WRITE_CHAR_FIELD(fk_del_action);
26272627
WRITE_BOOL_FIELD(skip_validation);
2628+
WRITE_BOOL_FIELD(initially_valid);
26282629
break;
26292630

26302631
case CONSTR_ATTR_DEFERRABLE:

src/backend/parser/gram.y

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2621,6 +2621,7 @@ ColConstraintElem:
26212621
n->fk_upd_action = (char) ($5 >> 8);
26222622
n->fk_del_action = (char) ($5 & 0xFF);
26232623
n->skip_validation = FALSE;
2624+
n->initially_valid = true;
26242625
$$ = (Node *)n;
26252626
}
26262627
;
@@ -2820,6 +2821,7 @@ ConstraintElem:
28202821
n->deferrable = ($11 & 1) != 0;
28212822
n->initdeferred = ($11 & 2) != 0;
28222823
n->skip_validation = false;
2824+
n->initially_valid = true;
28232825
$$ = (Node *)n;
28242826
}
28252827
| FOREIGN KEY '(' columnList ')' REFERENCES qualified_name
@@ -2836,6 +2838,7 @@ ConstraintElem:
28362838
n->fk_upd_action = (char) ($10 >> 8);
28372839
n->fk_del_action = (char) ($10 & 0xFF);
28382840
n->skip_validation = true;
2841+
n->initially_valid = false;
28392842
$$ = (Node *)n;
28402843
}
28412844
;

src/backend/parser/parse_utilcmd.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,6 +1696,7 @@ transformFKConstraints(CreateStmtContext *cxt,
16961696
Constraint *constraint = (Constraint *) lfirst(fkclist);
16971697

16981698
constraint->skip_validation = true;
1699+
constraint->initially_valid = true;
16991700
}
17001701
}
17011702

src/include/nodes/parsenodes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,6 +1536,7 @@ typedef struct Constraint
15361536
char fk_upd_action; /* ON UPDATE action */
15371537
char fk_del_action; /* ON DELETE action */
15381538
bool skip_validation; /* skip validation of existing rows? */
1539+
bool initially_valid; /* start the new constraint as valid */
15391540
} Constraint;
15401541

15411542
/* ----------------------

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy