Scrivio Interview library
ScenarioAdvanced8 min read · updated 12 November 2026

Regulators require you to show what your system believed a customer's risk rating was on any past date, including later corrections to earlier data. Which temporal model do you need and how would you implement it?

We need a bitemporal model. Every row records two time ranges: valid time, when the risk rating was true for the customer in the real world, and transaction time, when our system believed it. Corrections never overwrite a row; they close the old row's recorded range and insert new rows, so we can answer 'what did we believe on date X about date Y' with one query that names both dates.

Why this question is asked

Most systems keep only the current value, and a few keep a history of changes. A regulator asking what we believed on a past date, including mistakes we later fixed, needs more than either. If we simply update the rating when an error is found, the old belief is gone, and we cannot explain why a loan was approved in March under a rating that we now say was wrong. Interviewers ask this to see whether we can tell the two kinds of time apart and turn that idea into tables, constraints and queries. In this answer, we will recap the two time dimensions, design the table, walk through a correction step by step, write the as-of query, add constraints that keep the data clean, and talk about where this complexity belongs.

1A Quick Recap of the Two Kinds of Time

Valid time is the time in the world. If an analyst decides that customer 42 was high risk from 1 March to 1 June, that range is valid time. It answers the question 'when was this true?'.

Transaction time, which we will call recorded time, is the time in our database. It starts when we write a row and ends when we stop believing that row. It answers the question 'when did we know this?'. The system sets it from its own clock, and nobody is allowed to edit it later.

A table that tracks only valid time can tell us that the rating was high in April, but not that on 10 April we still thought it was low. A table that tracks only recorded time can tell us what the row looked like on 10 April, but it mixes up late corrections with real changes in the customer. Keeping both is called bitemporal data, and it is exactly what the regulator is asking for.

2Designing the Table

Each row says: from valid_from to valid_to the rating was R, and we believed this from recorded_from until recorded_to. In PostgreSQL we can store each pair as a range type, a single column holding a start and an end, which makes overlap checks and containment tests easy. A range written as [a, b) includes a and excludes b, so adjacent periods touch without overlapping.

SQL
CREATE EXTENSION IF NOT EXISTS btree_gist;
CREATE TABLE customer_risk (
customer_id bigint NOT NULL,
rating text NOT NULL,
valid_period daterange NOT NULL, -- when it was true for the customer
recorded tstzrange NOT NULL DEFAULT tstzrange(now(), NULL), -- when we believed it
recorded_by text NOT NULL,
reason text,
EXCLUDE USING gist (customer_id WITH =, valid_period WITH &&, recorded WITH &&)
);

The exclusion constraint is the heart of the design. It says two rows for the same customer may not overlap in both valid time and recorded time. In plain words, at any moment of our history we held exactly one belief about any given day. Rows may overlap in valid time (an old belief and its correction cover the same days) as long as their recorded ranges do not overlap, and that is exactly the shape a correction produces. The btree_gist extension lets a GiST index combine the plain equality on customer_id with the range overlaps.

PostgreSQL 18 also adds temporal keys written as PRIMARY KEY (customer_id, valid_period WITHOUT OVERLAPS), which covers the one-dimension case; check your version, and keep the exclusion constraint for the two-dimension rule.

3Walking Through a Correction

Suppose that on 1 January we recorded customer 42 as LOW from 1 January onward. On 1 March the customer really changed, so on 1 March we recorded HIGH from 1 March onward. Then on 15 April an analyst finds that the change actually happened on 1 February. We must not edit the old rows. Instead we close the recorded range of every row the correction affects and insert the corrected picture.

SQL
BEGIN;
-- 1. stop believing the rows that cover the corrected days
UPDATE customer_risk
SET recorded = tstzrange(lower(recorded), now())
WHERE customer_id = 42
AND upper_inf(recorded)
AND valid_period && daterange('2026-02-01', NULL); -- 2 rows closed
-- 2. insert the corrected belief, including the untouched part of the old row
INSERT INTO customer_risk (customer_id, rating, valid_period, recorded_by, reason) VALUES
(42, 'LOW', daterange('2026-01-01', '2026-02-01'), 'analyst7', 'split by correction'),
(42, 'HIGH', daterange('2026-02-01', NULL), 'analyst7', 'change dated 2026-02-01'); -- 2 rows
COMMIT;
Rows for customer 42 after the correction
ratingvalid_periodrecordedstate
LOW[2026-01-01, inf)[01 Jan, 01 Mar)replaced when HIGH arrived
LOW[2026-01-01, 2026-03-01)[01 Mar, 15 Apr)closed by the correction
HIGH[2026-03-01, inf)[01 Mar, 15 Apr)closed by the correction
LOW[2026-01-01, 2026-02-01)[15 Apr, inf)current
HIGH[2026-02-01, inf)[15 Apr, inf)current
On 1 March the open LOW row was itself closed and split. Nothing is deleted, so every past belief can be rebuilt.

Notice that the only UPDATE touches recorded, and only to set the end of a range that was open. No rating, no valid period and no start of a recorded range is ever changed. We call this append-only in spirit, and we can enforce it with a trigger or by granting the application only INSERT plus an UPDATE on that column. Notice also that we re-insert LOW for January: the old row covered January too, and once we close it we must restate what we still believe about January, or January would have no current belief at all.

4Asking the Two-Date Question

Every query on a bitemporal table names two dates: the day in the customer's life we care about, and the day of our knowledge we want to see. The containment operator @> asks whether a range contains a value.

SQL
-- What did we believe on 10 April about the customer's rating on 15 February?
SELECT rating
FROM customer_risk
WHERE customer_id = 42
AND valid_period @> date '2026-02-15'
AND recorded @> timestamptz '2026-04-10 12:00Z'; -- LOW
-- Same day in the customer's life, but as we know it today
SELECT rating FROM customer_risk
WHERE customer_id = 42
AND valid_period @> date '2026-02-15'
AND upper_inf(recorded); -- HIGH

The first answer is LOW, because on 10 April the correction had not been made yet. The second is HIGH, because today we know the change happened on 1 February. That pair of answers is exactly what the regulator wants to see, and it explains a decision made on 10 April without hiding the later fix.

The same exclusion constraint guarantees that each query returns at most one row, so a report can never show two ratings for the same day and the same moment of knowledge.

5Where Event Sourcing Fits

Event sourcing stores every change as an immutable event, such as RiskRatingAssessed or RiskRatingCorrected, and derives the current state from the log. The log is a natural record of transaction time, because each event is stamped when it was accepted and never changed. If the risk domain already uses events, we can keep the event log as the source of truth and build the bitemporal table as a projection, rebuilt from the log whenever we need it.

6Keeping the Complexity Where It Belongs

Bitemporal tables are harder for everyone. Every join needs two extra conditions, every developer must remember to close ranges instead of updating rows, and a careless report that forgets the recorded filter will happily return old and new beliefs together. So we should not make the whole schema bitemporal. We put it in the domains where the regulator needs it, such as risk ratings, limits and prices, and leave ordinary tables alone.

Then we hide the complexity behind views. A view called customer_risk_current returns the latest belief for each day, and a function customer_risk_as_of(valid date, known timestamptz) answers the two-date question. Most code and reports use these, and only the write path deals with the raw table. We also test the write path with cases that cover splits, corrections of corrections and a correction that starts before the first row.

7Follow-up questions

Why not simply keep an audit table of old row versions?

An audit table records transaction time only. It can show that the row changed on 15 April, but it cannot easily answer what we believed on 10 April about February, because the old rows mixed the real change in March with a mistake about February. With a valid period on every row and a recorded range on every version, the question becomes a simple filter.

How does SQL:2011 relate to this?

SQL:2011 defines application-time periods, which is valid time, and system-versioned tables, which is transaction time maintained by the database itself. Some databases implement both, such as SQL Server and MariaDB with system versioning. PostgreSQL does not have system versioning built in, so we maintain the recorded range ourselves or with a trigger, and use range types and exclusion constraints for the rules.

8Conclusion

In this answer, we saw that showing past beliefs including later corrections needs bitemporal data: a valid period for when the rating was true and a recorded range for when we believed it. Corrections close recorded ranges and insert new rows instead of updating in place, queries name both an as-of valid date and an as-of recorded date, an exclusion constraint on the ranges keeps one belief per day per moment, and event sourcing can supply the recorded history. Because the model is demanding, we keep it to the domains that need it and give everyone else views.

What to say in the interviewAbout 80 seconds aloud

Open with

This is a bitemporal problem. There are two kinds of time: valid time, when a rating was true for the customer, and transaction time, when our system recorded and believed it.

Then, if they want more

The regulator wants both, so every row carries a valid period and a recorded period. In PostgreSQL I would use a daterange and a tstzrange and add an exclusion constraint with btree_gist so that for one customer no two rows overlap in both ranges, which means one belief per day at any moment. Writes are append-only.

When a correction arrives I close the recorded range of the affected rows with now() and insert new rows for the corrected picture, including the untouched part of the old period, all in one transaction. A query then names two dates, for example what we believed on 10 April about 15 February, using range containment on both columns.

If the domain already uses event sourcing, the event log is the transaction-time record and the table is a projection. Because this model is hard to query correctly, I would limit it to regulated domains like risk ratings and expose views such as current rating and an as-of function so most developers and reports never touch the raw ranges.

About this answer

Last updated
12 November 2026
Versions it names
PostgreSQL 18
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Regulators require you to show what your system believed a customer's risk rating was on...": https://scrivio-interview-library.netlify.app/databases/data-modeling/31a8725ea3/