Bitemporal models
Overview
A bitemporal query specifies both a valid-time point and a recorded-time point. This answers two separate questions: what applied on a business day, and what did we believe about that day at a specified recording time? Retroactive corrections preserve old claims rather than silently overwriting them.
First example
SELECT price FROM tm_history WHERE product_id=1 AND valid_start<=3 AND 3<valid_end AND rec_start<=7 AND 7<rec_end;Observed SQLite result (rows in JSON):
[ [ 120 ]]How it works
Use half-open predicates on both axes. In the fixture a BEFORE INSERT/UPDATE guard rejects two claims with the same product whose valid intervals AND recorded intervals both overlap. A correction closes the old recording interval and inserts a replacement atomically. The second example temporarily corrects the price at recorded day 9, reads the new claim, then rolls back. The model does not enforce full coverage or prevent unauthorized historical edits.
Practical example
BEGIN;UPDATE tm_history SET rec_end=9 WHERE id=2;INSERT INTO tm_history VALUES(3,1,1,10,9,100,130);SELECT price FROM tm_history WHERE product_id=1 AND valid_start<=3 AND 3<valid_end AND rec_start<=9 AND 9<rec_end;ROLLBACK;Observed SQLite result (rows in JSON):
[ [ 130 ]]Practice and mistakes
Practice: compare valid day 3 at recorded days 6, 7 and 9 on the original fixture. Hint 1: use two containment predicates. Hint 2: at day 7 the old recording interval ends. Expected prices: 100, 120, 120.
PostgreSQL 17 example — NOT RUN
WITH h(price,valid_span,recorded_span) AS (VALUES (100,int4range(1,10),int4range(1,7)),(120,int4range(1,10),int4range(7,100)))SELECT price FROM h WHERE valid_span @> 3 AND recorded_span @> 7;Expected result, not executed: [[120]]