Report
ALTER TABLE ... VALIDATE CONSTRAINT and ALTER CONSTRAINT ... ENFORCED mark a temporal PERIOD foreign key valid when referencing ranges are only partly covered
- Severity
- 60
- Blamed commit
- 89f908a6d0ac1337c868625008c9598487d184e7 REL_18_STABLE, REL_19_STABLE, master
Summary
When a temporal foreign key (FOREIGN KEY (..., PERIOD col)) is validated later, it is checked with the non-temporal LEFT JOIN query in RI_Initial_Check(). That happens for ADD ... NOT VALID followed by VALIDATE CONSTRAINT, for NOT ENFORCED followed by ALTER CONSTRAINT ... ENFORCED, and when validation recurses to partitions. The join uses the PERIOD operator (&&), so any referencing row whose range merely overlaps some referenced row passes. Its range does not have to be covered by range_agg() of the referenced rows. The constraint is then marked convalidated = true even though rows violate it.
Adding the same constraint directly (without NOT VALID) correctly rejects those rows.
Reproduction
CREATE EXTENSION btree_gist;
CREATE TABLE vpk (id int, vt int4range,
PRIMARY KEY (id, vt WITHOUT OVERLAPS));
INSERT INTO vpk VALUES (1,'[3,5)'),(1,'[6,9)');
CREATE TABLE vfk (id int, vt int4range);
INSERT INTO vfk VALUES (1,'[4,7)'); -- hole at [5,6)
ALTER TABLE vfk ADD CONSTRAINT c FOREIGN KEY (id, PERIOD vt)
REFERENCES vpk (id, PERIOD vt);
-- ERROR: insert or update on table "vfk" violates foreign key
-- constraint "c" (correct)
ALTER TABLE vfk ADD CONSTRAINT c2 FOREIGN KEY (id, PERIOD vt)
REFERENCES vpk (id, PERIOD vt) NOT VALID;
ALTER TABLE vfk VALIDATE CONSTRAINT c2; -- succeeds (wrong)
SELECT conname, convalidated FROM pg_constraint
WHERE conrelid = 'vfk'::regclass;
-- c2 | t
The same happens with NOT ENFORCED followed by ALTER CONSTRAINT ... ENFORCED, and with VALIDATE CONSTRAINT on a partitioned referencing table (the partition's constraint is validated too):
CREATE TABLE epk (id int, vt int4range,
PRIMARY KEY (id, vt WITHOUT OVERLAPS));
INSERT INTO epk VALUES (1,'[3,5)');
CREATE TABLE efk (id int, vt int4range);
INSERT INTO efk VALUES (1,'[4,7)');
ALTER TABLE efk ADD CONSTRAINT ce FOREIGN KEY (id, PERIOD vt)
REFERENCES epk (id, PERIOD vt) NOT ENFORCED;
ALTER TABLE efk ALTER CONSTRAINT ce ENFORCED; -- succeeds (wrong)
A referencing key that overlaps nothing at all is still rejected, because the LEFT JOIN finds no partner. Only partial coverage slips through.
Tested on master at dc168a83874fbf5cee4afb76a623e1349a539724.
Analysis
validateForeignKeyConstraint() skips RI_Initial_Check() when its hasperiod argument is true and falls back to firing the RI check trigger for each row, which runs the range_agg() coverage query:
if (!hasperiod && RI_Initial_Check(&trig, rel, pkrel))
In phase 3, ATRewriteTables() passes con->conwithperiod. Only the ADD FOREIGN KEY path (addFkRecurseReferencing() in tablecmds.c) sets newcon->conwithperiod = fkconstraint->fk_with_period. QueueFKConstraintValidation() is used by VALIDATE CONSTRAINT, ALTER CONSTRAINT ... ENFORCED and the recursion to partitions. It allocates the NewConstraint with palloc0 and never sets conwithperiod, so the value is false and the non-temporal LEFT JOIN check is used. RI_Initial_Check() joins on pf_eq_oprs, which for the PERIOD column is the overlaps operator, so it tests overlap, not containment.
89f908a6d0a added conwithperiod only to the ADD path. The VALIDATE path (ATExecValidateConstraint at that time, later split into QueueFKConstraintValidation by 86374c9a0e3) was never updated.
Suggested fix: in QueueFKConstraintValidation() set
newcon->conwithperiod = con->conperiod;
As defense in depth, RI_Initial_Check() could also Assert or error on riinfo->hasperiod.
Notes
- Effect: a constraint that pg_constraint reports as validated is violated by existing rows. Later RI checks only look at rows that change, so the violation stays until someone notices.
- Confirmed: pg_dump output re-adds the FK after loading the data via the correct ADD path, so restoring such a database fails at the FK step.
- Multirange PERIOD columns go through the same path.
Branches
89f908a6d0ac and 86374c9a0e30 are both in master, REL_19_STABLE and REL_18_STABLE. Reproduced on master only; back branches not yet tested.
Related
- 01a233ae-1529-7e27-bbf0-2bb4f98add5b (temporal FK rejects an empty referencing range; same feature, different code path)
- 01a140e2-0344-77a6-afc8-3158dab1a1b5 (PERIOD foreign key accepts a plain exclusion constraint)