Bitemporal Modeling in TypeDB
Learn how to implement bitemporal data modeling in TypeDB using valid time and system time to track historical facts, manage corrections, and maintain an auditable data history.

Simple temporal modeling can tell you what is true now. A bitemporal model also tells you what you used to think was true, and when that changed. This post explains the idea, then builds a working model in TypeDB 3 with example schema and queries.
Two kinds of time
When recording facts, there are two timelines:
Valid time is when a fact was true in the world. An employee’s salary became 211/day on 15 February, regardless of when anyone wrote it down. Also called business, application or effective time.
System time is when the database believed the fact. We recorded the new salary on 15 March; before that instant the system didn’t know. This is also called transaction or record time.
A bitemporal store tracks both axes. Valid time is the history of the world; we rewrite it when we learn we were wrong. System time is the history of our knowledge; it is append-only. An append-only record of belief over a correctable record of fact lets you fix the past without destroying the evidence that you got it wrong, and keeps an auditable trail in storage.
Why we keep two histories
Let’s take a payroll scenario:
- On 1 January, an employee is hired at 100/day. We record it the same day.
- On 25 February, payroll runs and pays them 100/day.
- On 15 March, HR tells us their rate changed to 211/day, effective 15 February.
With both timelines, four distinct questions about the same date get four distinct answers:
| Question | Valid time | System time | Answer |
|---|---|---|---|
| What do we believe the employee earned on 25 Feb? | 25 Feb | now | 211 |
| What did we believe on 25 Feb, when payroll ran? | 25 Feb | 25 Feb | 100 |
| What did we believe on 20 Mar about 25 Feb? | 25 Feb | 20 Mar | 211 |
| Was anyone paid at a rate we have since corrected? | 25 Feb | 25 Feb vs now | 100 ≠ 211 |
While a simple temporal model could answer the first question, the subsequent questions all require something more – and this is where bitemporal modeling comes into play.
These questions are often particularly important in regulated industries. For example, financial industries might need to reconstruct a trading book as it was viewed at market close, or insurance could settle a claim against the policy terms in force at the time of a loss rather than following a later amendment. In healthcare it can separate a diagnosis from when that diagnosis was charted.
Having to maintain two separate timelines for every object can be complex. To maintain a full history, nothing can be overwritten, so every write must respect both axes and ensure that they don’t produce overlapping versions from any point of view. Reads need to ensure they pick the correct time across both axes and find the correct version in force from that perspective.
Modeling it in TypeDB
TypeDB’s composable relationships and connectivity constraints allow us to build a high-fidelity temporal storage system. To get there, we’ll start by building the schema up from a simple baseline schema with no notion of time, then add in the temporal requirements as we go.
Step 0: salary as attributes
A salary is two attributes on the employee:
define
attribute employee-id, value string;
attribute full-name, value string;
attribute salary, value double;
entity employee,
owns employee-id @key,
owns full-name @card(1..1),
owns salary @card(1..1);
An employee has one salary. An update overwrites it and the old value is gone. The database model can answer one question: what is the salary now?
Step 1: salary as a temporal fact
To keep history along the valid-time axis, a salary must stop being a property of the employee and become a statement about the employee with its own lifetime. There can be many, each true for a period. In TypeDB we can model this as a relation. Every temporal fact needs the same two timestamps, so they go on an abstract relation that concrete facts subtype:
define
attribute valid-from, value datetime-tz;
attribute valid-to, value datetime-tz;
relation temporal-fact @abstract,
relates subject,
owns valid-from @card(1..1),
owns valid-to @card(0..1);
attribute employee-id, value string;
attribute full-name, value string;
attribute amount, value double;
entity employee,
owns employee-id @key,
owns full-name @card(1..1),
plays temporal-fact:subject;
relation salary sub temporal-fact,
owns amount @card(1..1);
Our model now has a very different structure. employee plays a role instead of owning a value, and salary becomes a relation owning an amount. Writes change too: instead of overwriting an attribute, you close the current fact’s valid-to and insert a new one.
This model can now answer “what was the employee’s salary on 25 February?”, but it can’t answer what we thought it was, because a correction edits valid-time history in place. When HR calls on 15 March, we truncate the old fact and insert the new one. The fact that payroll ran against 100 on 25 February remains untracked.
Step 2: salary as a bitemporal fact
Now let’s add the second axis. The whole schema again:
define
attribute valid-from, value datetime-tz;
attribute valid-to, value datetime-tz;
attribute system-from, value datetime-tz;
attribute system-to, value datetime-tz;
relation bitemporal-fact @abstract,
relates subject,
owns valid-from @card(1..1),
owns valid-to @card(0..1),
owns system-from @card(1..1),
owns system-to @card(0..1);
attribute employee-id, value string;
attribute full-name, value string;
attribute amount, value double;
entity employee,
owns employee-id @key,
owns full-name @card(1..1),
plays bitemporal-fact:subject;
relation salary sub bitemporal-fact,
owns amount @card(1..1);
Compare with the previous schema: the structure of the model is untouched, all we’ve done is a rename to bitemporal-fact, and two new attributes that it owns. employee and salary are unchanged.
What does change is behaviour: corrections close facts in system time instead of editing valid time, and queries pick a point on each axis.
Open intervals
Note the cardinalities: the -from attributes are mandatory, the -to attributes are @card(0..1). No valid-to means true until further notice; no system-to means currently believed.
The other option would be a sentinel such as 9999-12-31. It keeps range checks to plain comparisons, but puts a fake date in your data that every reader must know about. In TypeQL we can use a negation to easily query around the optional attribute:
$s has valid-from $vf;
$vf <= $t;
not { $s has valid-to $vto; $vto <= $t; };
This gives us the two important constraints of “started by t”, and “not ended at or before t”. A fact with no end passes the not trivially. System time works the same way, making “currently believed” a simple one-liner:
not { $s has system-to $_; };
It’s worth noting here that while a bare $vto > $t looks equivalent it drops every open-ended fact, because an absent attribute never binds $vto. That type of easy mistake gives a good reason to write the range logic once in the schema.
Reading: as-of functions in the schema
TypeDB 3 functions are read pipelines defined in the schema and callable from any query. We can use these to codify on a schema level the way you query bitemporal facts.
The general query takes a point on each axis: a valid time (“when was this true?”) and a system time (“when did we believe it?”):
define
fun salary-as-of($e: employee, $vt: datetime-tz, $tt: datetime-tz) -> salary:
match
$s isa salary, links (subject: $e),
has valid-from $vf, has system-from $sf;
$vf <= $vt; not { $s has valid-to $vto; $vto <= $vt; };
$sf <= $tt; not { $s has system-to $sto; $sto <= $tt; };
return first $s;
We can make a couple of other functions for other common queries, namely querying for current salary and salary history:
fun salary-at($e: employee, $vt: datetime-tz) -> salary:
match
$s isa salary, links (subject: $e), has valid-from $vf;
not { $s has system-to $_; };
$vf <= $vt; not { $s has valid-to $vto; $vto <= $vt; };
return first $s;
fun salary-history($e: employee) -> { salary }:
match
$s isa salary, links (subject: $e);
not { $s has system-to $_; };
return { $s };
At any point on both axes there is at most one salary, so the as-of functions return a single salary with return first, and a caller assigns it with let ... =. salary-history returns a stream, { salary }, which a caller unwinds with let ... in:
match
$e isa employee, has employee-id "E-1001";
let $s = salary-at($e, 2026-02-25T00:00:00Z);
fetch {
"amount": $s.amount,
"valid-from": $s.valid-from,
"valid-to": $s.valid-to
};
One more function is worth having. We can’t (yet) express “no two current versions overlap in valid time” as a constraint, so we instead define a function that we can call to check for their existence, and only commit a write if there are none.
fun overlapping-current-salaries() -> { salary, salary }:
match
$a isa salary, links (subject: $e), has valid-from $a-vf;
$b isa salary, links (subject: $e), has valid-from $b-vf;
not { $a is $b; };
not { $a has system-to $_; };
not { $b has system-to $_; };
not { $b has valid-to $b-vto; $b-vto <= $a-vf; };
not { $a has valid-to $a-vto; $a-vto <= $b-vf; };
return { $a, $b };
Writing: corrections as a pipeline
The first insert is simple. Its open ends are simply absent:
insert
$e isa employee,
has employee-id "E-1001",
has full-name "Alex Smith";
$v1 isa salary, links (subject: $e),
has amount 100.0,
has valid-from 2026-01-01T00:00:00Z, # no valid-to: until further notice
has system-from 2026-01-01T09:00:00Z; # no system-to: currently believed
Corrections are where things get more interesting. On 15 March we learn the correct salary was 211 from 15 February. Three things need to happen as one event:
- Close the current version covering 15 Feb, in system time only.
- Re-assert the old value, ending 15 Feb.
- Assert the new value from 15 Feb, inheriting the old end date if there was one.
TypeQL pipelines make this one query: match calls salary-at to find the version to supersede, update closes it, insert writes the replacements:
match
$e isa employee, has employee-id "E-1001";
let $old = salary-at($e, 2026-02-15T00:00:00Z); # the current version covering the effective date
$old has amount $amt, has valid-from $vf;
try { $old has valid-to $vto; }; # carry the old end forward, if any
update
$old has system-to 2026-03-15T10:00:00Z; # (1) superseded as of now
insert
$before isa salary, links (subject: $e), # (2) old value, now known to end 15 Feb
has $amt,
has $vf, has valid-to 2026-02-15T00:00:00Z,
has system-from 2026-03-15T10:00:00Z;
$after isa salary, links (subject: $e), # (3) the correction
has amount 211.0,
has valid-from 2026-02-15T00:00:00Z,
has system-from 2026-03-15T10:00:00Z;
try { $after has $vto; }; # only if the old version had an end
Note the use of try to optionally bind $vto in the match, and again to optionally insert it later.
We also don’t delete anything: the superseded fact keeps its original valid time, which is what makes “what did payroll see?” answerable.
A planned change uses the same query structure. A raise to 250 effective 1 July, recorded on 1 June, is this pipeline with different literals.
Putting it together
After the hire, the correction and the raise, the employee has six salary facts. Three are superseded; none are deleted:
| amount | valid-from | valid-to | system-from | system-to | Current? |
|---|---|---|---|---|---|
| 100 | 2026-01-01 | — | 2026-01-01 | 2026-03-15 | superseded |
| 100 | 2026-01-01 | 2026-02-15 | 2026-03-15 | 2026-06-01 | superseded |
| 211 | 2026-02-15 | — | 2026-03-15 | 2026-06-01 | superseded |
| 100 | 2026-01-01 | 2026-02-15 | 2026-06-01 | — | current belief |
| 211 | 2026-02-15 | 2026-07-01 | 2026-06-01 | — | current belief |
| 250 | 2026-07-01 | — | 2026-06-01 | — | current belief |
The four questions from earlier are now simple queries using our earlier functions:
What do we now believe the employee earned on 25 Feb? Pin valid time; leave system time open. Returns 211.
match
$e isa employee, has employee-id "E-1001";
let $s = salary-at($e, 2026-02-25T00:00:00Z);
fetch { "amount": $s.amount };
What did we believe on 25 Feb, when payroll ran? Pin both axes to payroll day. Returns 100, believed from 1 Jan to 15 Mar.
match
$e isa employee, has employee-id "E-1001";
let $s = salary-as-of($e, 2026-02-25T00:00:00Z, 2026-02-25T12:00:00Z);
fetch {
"amount": $s.amount,
"believed-from": $s.system-from,
"believed-until": $s.system-to
};
Was anyone paid on a since-corrected rate? Call both functions for the same valid date and compare. The reconciliation query falls out of the model with no extra machinery:
match
$e isa employee, has employee-id $id;
let $paid = salary-as-of($e, 2026-02-25T00:00:00Z, 2026-02-25T12:00:00Z);
let $true = salary-at($e, 2026-02-25T00:00:00Z);
not { $paid is $true; };
$paid has amount $paid-amt;
$true has amount $true-amt;
let $delta = $true-amt - $paid-amt;
fetch {
"employee": $id,
"paid-at": $paid-amt,
"should-be": $true-amt,
"adjustment": $delta
};
What changed in the 15 March correction? Audit queries read the system-time attributes directly: rows closed at that instant were superseded, rows opened then replaced them.
match
$e isa employee, has employee-id "E-1001";
$closed isa salary, links (subject: $e), has system-to 2026-03-15T10:00:00Z;
$opened isa salary, links (subject: $e), has system-from 2026-03-15T10:00:00Z;
fetch {
"superseded": { "amount": $closed.amount, "valid-from": $closed.valid-from, "valid-to": $closed.valid-to },
"replacement": { "amount": $opened.amount, "valid-from": $opened.valid-from, "valid-to": $opened.valid-to }
};
And the current timeline, sorted:
match
$e isa employee, has employee-id "E-1001";
let $s in salary-history($e);
$s has valid-from $vf;
sort $vf asc;
fetch { "from": $s.valid-from, "to": $s.valid-to, "amount": $s.amount };
Conclusion
Bitemporal modeling allows for a whole new set of questions to be asked relative to just storing simple start and end timestamps for data.
TypeDB allows the bitemporal modeling to be kept in a simple abstract type where it can be separated from the domain model and re-applied in multiple places across the schema.
Using functions, we can ensure that the more complex querying mechanics required by bitemporal modeling are easily reusable, both when querying data directly and as part of an insert pipeline for adding corrected data.
The result is a model where bitemporality is enforced in the schema, rather than being something a developer has to consistently remember to do.
