Patch
Fix temporal foreign key validation outside ADD CONSTRAINT
- Branch
- master
- Risk
- 15
- Base commit
- dc168a83874fbf5cee4afb76a623e1349a539724
To apply it to a checkout of master at the base commit:
curl -s https://ai-annex.j-davis.com/api/v1/patches/01a23440-4d7b-7912-ac43-c1f3dd2d3822 | jq -r .body | git am
From 2fdc75999681c182d192b3cb518d1e5bb188da2b Mon Sep 17 00:00:00 2001
From: Claude <noreply@anthropic.com>
Date: Tue, 1 Dec 2026 16:54:37 -0800
Subject: [PATCH] Fix temporal foreign key validation outside ADD CONSTRAINT
When ALTER TABLE checks existing rows against a foreign key in phase 3,
validateForeignKeyConstraint() normally uses RI_Initial_Check(), which
looks for violations with a single LEFT JOIN query. That query compares
the PERIOD column with the overlaps operator, so for a temporal foreign
key it only verifies that each referencing range overlaps some
referenced row, not that the referenced rows together cover it. For
that reason temporal foreign keys are supposed to skip it and fire the
RI check trigger for each row instead, as directed by
NewConstraint.conwithperiod.
Only ALTER TABLE ... ADD FOREIGN KEY set conwithperiod, though. ALTER
TABLE ... VALIDATE CONSTRAINT, ALTER CONSTRAINT ... ENFORCED, and ATTACH
PARTITION (both when the partition's own NOT VALID constraint is
adopted and when a new one is cloned from the parent) left it false.
Those commands therefore accepted rows whose ranges were only partly
covered by the referenced table and marked the constraint as valid,
even though adding the same constraint directly rejects them.
Fix by setting conwithperiod from pg_constraint.conperiod in
QueueFKConstraintValidation() and ATExecAlterFKConstrEnforceability(),
and from the with_period argument in addFkRecurseReferencing(); the
Constraint node that CloneFkReferencing() builds does not have
fk_with_period set.
This does not recheck constraints that were already marked valid this
way.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---
src/backend/commands/tablecmds.c | 4 +-
.../regress/expected/without_overlaps.out | 45 +++++++++++++++++++
src/test/regress/sql/without_overlaps.sql | 39 ++++++++++++++++
3 files changed, 87 insertions(+), 1 deletion(-)
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 9755943f110..a52dc7c396d 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -11766,7 +11766,7 @@ addFkRecurseReferencing(List **wqueue, Constraint *fkconstraint, Relation rel,
newcon->refrelid = RelationGetRelid(pkrel);
newcon->refindid = indexOid;
newcon->conid = parentConstr;
- newcon->conwithperiod = fkconstraint->fk_with_period;
+ newcon->conwithperiod = with_period;
newcon->qual = (Node *) fkconstraint;
tab->constraints = lappend(tab->constraints, newcon);
@@ -13209,6 +13209,7 @@ ATExecAlterFKConstrEnforceability(List **wqueue, ATAlterConstraint *cmdcon,
newcon->refrelid = currcon->confrelid;
newcon->refindid = currcon->conindid;
newcon->conid = currcon->oid;
+ newcon->conwithperiod = currcon->conperiod;
newcon->qual = (Node *) fkconstraint;
/* Find or create work queue entry for this table */
@@ -14112,6 +14113,7 @@ QueueFKConstraintValidation(List **wqueue, Relation conrel, Relation fkrel,
newcon->refrelid = con->confrelid;
newcon->refindid = con->conindid;
newcon->conid = con->oid;
+ newcon->conwithperiod = con->conperiod;
newcon->qual = (Node *) fkconstraint;
/* Find or create work queue entry for this table */
diff --git a/src/test/regress/expected/without_overlaps.out b/src/test/regress/expected/without_overlaps.out
index 8e3f6eee0b0..9b8ecfad04f 100644
--- a/src/test/regress/expected/without_overlaps.out
+++ b/src/test/regress/expected/without_overlaps.out
@@ -1661,6 +1661,29 @@ ALTER TABLE temporal_fk_rng2rng
REFERENCES temporal_rng;
ERROR: insert or update on table "temporal_fk_rng2rng" violates foreign key constraint "temporal_fk_rng2rng_fk"
DETAIL: Key (parent_id, valid_at)=([1,2), [2018-01-02,2018-04-01)) is not present in table "temporal_rng".
+-- validating later must check the same thing:
+ALTER TABLE temporal_fk_rng2rng
+ ADD CONSTRAINT temporal_fk_rng2rng_fk
+ FOREIGN KEY (parent_id, PERIOD valid_at)
+ REFERENCES temporal_rng
+ NOT VALID;
+-- should fail:
+ALTER TABLE temporal_fk_rng2rng VALIDATE CONSTRAINT temporal_fk_rng2rng_fk;
+ERROR: insert or update on table "temporal_fk_rng2rng" violates foreign key constraint "temporal_fk_rng2rng_fk"
+DETAIL: Key (parent_id, valid_at)=([1,2), [2018-01-02,2018-04-01)) is not present in table "temporal_rng".
+ALTER TABLE temporal_fk_rng2rng
+ DROP CONSTRAINT temporal_fk_rng2rng_fk,
+ ADD CONSTRAINT temporal_fk_rng2rng_fk
+ FOREIGN KEY (parent_id, PERIOD valid_at)
+ REFERENCES temporal_rng
+ NOT ENFORCED;
+-- should fail:
+ALTER TABLE temporal_fk_rng2rng
+ ALTER CONSTRAINT temporal_fk_rng2rng_fk ENFORCED;
+ERROR: insert or update on table "temporal_fk_rng2rng" violates foreign key constraint "temporal_fk_rng2rng_fk"
+DETAIL: Key (parent_id, valid_at)=([1,2), [2018-01-02,2018-04-01)) is not present in table "temporal_rng".
+ALTER TABLE temporal_fk_rng2rng
+ DROP CONSTRAINT temporal_fk_rng2rng_fk;
-- okay again:
DELETE FROM temporal_fk_rng2rng;
ALTER TABLE temporal_fk_rng2rng
@@ -2349,6 +2372,28 @@ UPDATE temporal_partitioned_fk_rng2rng SET valid_at = daterange('2000-01-01', '2
ERROR: conflicting key value violates exclusion constraint "tfkp1_pkey"
DETAIL: Key (id, valid_at)=([1,2), [2000-01-01,2000-04-01)) conflicts with existing key (id, valid_at)=([1,2), [2000-01-01,2000-04-01)).
--
+-- partitioned FK: attaching a partition checks its existing rows
+--
+CREATE TABLE tfkp3 (LIKE temporal_partitioned_fk_rng2rng);
+INSERT INTO tfkp3 (id, valid_at, parent_id) VALUES
+ ('[13,14)', daterange('2000-01-01', '2000-04-01'), '[1,2)');
+-- should fail:
+ALTER TABLE temporal_partitioned_fk_rng2rng
+ ATTACH PARTITION tfkp3 FOR VALUES IN ('[13,14)');
+ERROR: insert or update on table "tfkp3" violates foreign key constraint "temporal_partitioned_fk_rng2rng_fk"
+DETAIL: Key (parent_id, valid_at)=([1,2), [2000-01-01,2000-04-01)) is not present in table "temporal_partitioned_rng".
+ALTER TABLE tfkp3
+ ADD CONSTRAINT tfkp3_fk
+ FOREIGN KEY (parent_id, PERIOD valid_at)
+ REFERENCES temporal_partitioned_rng (id, PERIOD valid_at)
+ NOT VALID;
+-- should fail:
+ALTER TABLE temporal_partitioned_fk_rng2rng
+ ATTACH PARTITION tfkp3 FOR VALUES IN ('[13,14)');
+ERROR: insert or update on table "tfkp3" violates foreign key constraint "tfkp3_fk"
+DETAIL: Key (parent_id, valid_at)=([1,2), [2000-01-01,2000-04-01)) is not present in table "temporal_partitioned_rng".
+DROP TABLE tfkp3;
+--
-- partitioned FK referenced updates NO ACTION
--
TRUNCATE temporal_partitioned_rng, temporal_partitioned_fk_rng2rng;
diff --git a/src/test/regress/sql/without_overlaps.sql b/src/test/regress/sql/without_overlaps.sql
index 46bdd7856a4..4efbea87be3 100644
--- a/src/test/regress/sql/without_overlaps.sql
+++ b/src/test/regress/sql/without_overlaps.sql
@@ -1197,6 +1197,25 @@ ALTER TABLE temporal_fk_rng2rng
ADD CONSTRAINT temporal_fk_rng2rng_fk
FOREIGN KEY (parent_id, PERIOD valid_at)
REFERENCES temporal_rng;
+-- validating later must check the same thing:
+ALTER TABLE temporal_fk_rng2rng
+ ADD CONSTRAINT temporal_fk_rng2rng_fk
+ FOREIGN KEY (parent_id, PERIOD valid_at)
+ REFERENCES temporal_rng
+ NOT VALID;
+-- should fail:
+ALTER TABLE temporal_fk_rng2rng VALIDATE CONSTRAINT temporal_fk_rng2rng_fk;
+ALTER TABLE temporal_fk_rng2rng
+ DROP CONSTRAINT temporal_fk_rng2rng_fk,
+ ADD CONSTRAINT temporal_fk_rng2rng_fk
+ FOREIGN KEY (parent_id, PERIOD valid_at)
+ REFERENCES temporal_rng
+ NOT ENFORCED;
+-- should fail:
+ALTER TABLE temporal_fk_rng2rng
+ ALTER CONSTRAINT temporal_fk_rng2rng_fk ENFORCED;
+ALTER TABLE temporal_fk_rng2rng
+ DROP CONSTRAINT temporal_fk_rng2rng_fk;
-- okay again:
DELETE FROM temporal_fk_rng2rng;
ALTER TABLE temporal_fk_rng2rng
@@ -1846,6 +1865,26 @@ UPDATE temporal_partitioned_fk_rng2rng SET id = '[1,2)' WHERE id = '[4,5)';
-- should fail:
UPDATE temporal_partitioned_fk_rng2rng SET valid_at = daterange('2000-01-01', '2000-04-01') WHERE id = '[1,2)';
+--
+-- partitioned FK: attaching a partition checks its existing rows
+--
+
+CREATE TABLE tfkp3 (LIKE temporal_partitioned_fk_rng2rng);
+INSERT INTO tfkp3 (id, valid_at, parent_id) VALUES
+ ('[13,14)', daterange('2000-01-01', '2000-04-01'), '[1,2)');
+-- should fail:
+ALTER TABLE temporal_partitioned_fk_rng2rng
+ ATTACH PARTITION tfkp3 FOR VALUES IN ('[13,14)');
+ALTER TABLE tfkp3
+ ADD CONSTRAINT tfkp3_fk
+ FOREIGN KEY (parent_id, PERIOD valid_at)
+ REFERENCES temporal_partitioned_rng (id, PERIOD valid_at)
+ NOT VALID;
+-- should fail:
+ALTER TABLE temporal_partitioned_fk_rng2rng
+ ATTACH PARTITION tfkp3 FOR VALUES IN ('[13,14)');
+DROP TABLE tfkp3;
+
--
-- partitioned FK referenced updates NO ACTION
--
base-commit: dc168a83874fbf5cee4afb76a623e1349a539724
--
2.53.0