Valid and transaction time
Overview
Valid time describes when a fact applies in the business world. Transaction time describes when the database recorded that version. A price effective on day 3 may only be recorded on day 7. The fixture uses a manually supplied logical recording day, not an engine-maintained transaction timestamp.
First example
SELECT price,rec_start,rec_end FROM tm_history WHERE product_id=1 AND valid_start<=3 AND 3<valid_end ORDER BY rec_start;Observed SQLite result (rows in JSON):
[ [ 100, 1, 7 ], [ 120, 7, 100 ]]How it works
In the history table the original claim for valid days [1,10) was recorded on day 1 and superseded on day 7. The replacement claim has price 120 and recorded interval [7,100). The finite endpoint 100 is a teaching horizon, not infinity. SQL Server system-versioned tables maintain a system-time history, which does not itself express business effective dates. PostgreSQL 17 uses explicit modeled columns for this exercise.
Practical example
SELECT price FROM tm_history WHERE product_id=1 AND valid_start<=3 AND 3<valid_end AND rec_start<=6 AND 6<rec_end;Observed SQLite result (rows in JSON):
[ [ 100 ]]Practice and mistakes
Practice: retrieve all claims about valid day 3 and identify the superseded claim. Hint 1: filter by the valid interval first. Hint 2: order by recorded start. Do not interpret the recorded day as the effective date.
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 ORDER BY lower(recorded_span);Expected result, not executed: [[100], [120]]