Quick revision
The short answer
A history table keeps the previous versions of rows, usually with the operation, changed_at and changed_by, while the main table holds only the current state. Putting valid_from and valid_to on the main table instead makes every row a version, so current-state queries must filter for the open-ended row, while 'as of' queries become simpler. A history table can be filled by a trigger, by Hibernate Envers, which creates _AUD tables, or by CDC from the write-ahead log; some databases offer SQL:2011 system-versioned tables, while PostgreSQL needs triggers or an extension.
What to remember
In this answer, we compared a separate history table, which keeps current queries simple and records previous versions with who and when, with valid_from and valid_to on the main table, which makes every row a version and simplifies 'as of' queries. We filled a history table with a trigger, with Hibernate Envers and its _AUD tables, and with CDC from the write-ahead log, and saw that SQL:2011 system-versioned tables do it automatically in databases like MariaDB and SQL Server, while PostgreSQL uses triggers or an extension.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A history table keeps the previous versions of rows, usually with the operation, changed_at and changed_by, while the main table holds only the current state. Putting valid_from and valid_to on the main table instead makes every row a version, so current-state queries must filter for the open-ended row, while 'as of' queries become simpler. A history table can be filled by a trigger, by Hibernate Envers, which creates _AUD tables, or by CDC from the write-ahead log; some databases offer SQL:2011 system-versioned tables, while PostgreSQL needs triggers or an extension.
Why this question is asked
Sooner or later someone asks what a record looked like last month: a customer disputes a price, an auditor asks who changed a credit limit, or a bug corrupted data and we need the old values. Interviewers ask this to check that we know the two basic ways to keep history and what each costs. In this answer, we will build both designs side by side, compare the queries they make easy and hard, fill a history table three ways, and see what SQL:2011 system versioning adds.
1A History Table Next to the Main Table
In this design the main table looks exactly as before and always holds the current row. A separate table stores a copy of each previous version, with a few extra columns describing the change.
Notice that current queries do not change at all: SELECT * FROM customers WHERE id = 7 still returns the one current row, with the same indexes and plans as before. The history grows in its own table, so it does not slow down the hot table.
2Versions on the Main Table With valid_from and valid_to
The other design puts the versions in the main table itself. Each row covers a period: valid_from is when that version became true, and valid_to is when it stopped, with null (or 'infinity') for the version that is current.
| Question | History table | valid_from / valid_to on main |
|---|---|---|
| Current state | Plain select, unchanged | Needs valid_to IS NULL filter |
| State as of a date | Union current with history, pick version | One range filter |
| Who changed what | Columns in history rows | Needs extra changed_by column |
| Size of hot table | Stays small | Grows with every change |
| Foreign keys | Point at the main row as usual | Need a stable id table or care |
Every query against current data now needs the valid_to IS NULL filter, and every foreign key that wants 'the customer' has to point at a stable id rather than at one version. An update becomes two writes: close the current row by setting valid_to, and insert the new version. In return, 'as of' questions are a single filter on one table.
3Filling the History Table With a Trigger
A trigger is the simplest way to make sure every change is captured, whichever program makes it. Before each update or delete, it copies the old row into the history table.
Because it runs in the same transaction as the change, the history row is committed or rolled back together with it. The changed_by value comes from a session setting the application sets per transaction, falling back to the database user.
4Hibernate Envers and CDC
In a JPA application, Hibernate Envers does the same job with an annotation. We mark the entity with @Audited, and Envers creates a table such as customers_AUD (the suffix is configurable) holding each version with a revision number and a revision type (add, modify, delete), plus a REVINFO table recording when each revision happened. AuditReader then answers questions like 'the customer at revision 42' or 'all revisions of customer 7'. The limitation is that Envers only sees changes made through Hibernate; SQL scripts and other services are invisible to it.
Change data capture reads the database's write-ahead log, the log PostgreSQL writes before applying any change, and streams every committed change with before and after values, for example through Debezium into Kafka and then into an audit store. It sees every writer, adds no work to the write transaction, and suits companies that want history centrally for many services. The trade-off is more infrastructure, and history arrives a little after the change rather than in the same transaction.
- All writes go through Hibernate?YesEnvers is the least codeNoNeed a database-level method
- History needed in the same transaction?YesTrigger into a history tableNoCDC from the WAL
- Many services need central audit?YesCDC into an audit storeNoKeep it per database
3 steps in order. Play them, or step through them one at a time.
5SQL:2011 System-Versioned Tables
The SQL:2011 standard defines system-versioned tables, where the database itself keeps the history. We declare the table WITH SYSTEM VERSIONING, the database stamps each row with system start and end times and moves old versions into a history area automatically, and queries can say FOR SYSTEM_TIME AS OF '2026-03-01' to see the past. MariaDB, SQL Server, Db2 and others support it.
PostgreSQL does not have built-in system versioning. We use triggers as above, or an extension such as temporal_tables where it is available (check your hosting platform). Recent PostgreSQL versions have added temporal constraints over ranges, which help with application-time designs like valid_from and valid_to, but not automatic history (check your version for the exact features).
6Follow-up questions
Which design would you choose for an audit requirement?
A history table filled by a trigger or CDC, because the main table and its queries stay unchanged, history is captured for every writer, and the extra columns record who made each change. valid_from and valid_to on the main table fit better when the business itself works with versions, such as prices or contracts that have effective dates.
How do you query a customer as of a date with a history table?
Look in the history for the version that was current at that time, and if the date is after the last change, use the main row. With changed_at recording when each old version was replaced, the version valid at a date is the earliest history row changed after that date, or the current row if there is none.
Does a history table need indexes?
Yes, at least on (id, changed_at), because the typical questions are about one entity over time. It usually needs no unique constraints, and because it only grows, it is a good candidate for partitioning by month and for a retention policy.
7Conclusion
In this answer, we compared a separate history table, which keeps current queries simple and records previous versions with who and when, with valid_from and valid_to on the main table, which makes every row a version and simplifies 'as of' queries. We filled a history table with a trigger, with Hibernate Envers and its _AUD tables, and with CDC from the write-ahead log, and saw that SQL:2011 system-versioned tables do it automatically in databases like MariaDB and SQL Server, while PostgreSQL uses triggers or an extension.
Open with
A history table is a second table next to the main one that stores previous versions of rows. The main table keeps only the current state, and each history row has the old values plus columns like the operation, changed_at and changed_by.
Then, if they want more
The alternative is to put valid_from and valid_to on the main table itself, so every row is a version and the current one has an open-ended valid_to. The difference shows up in queries. With a history table, current-state queries are unchanged and the hot table stays small, while as of queries need to combine current and history.
With versions in the main table, as of queries are one range filter, but every current query has to remember valid_to is null, updates become close the old row and insert a new one, and foreign keys need a stable id.
To populate a history table, the simplest way is an after update or delete trigger that inserts the old row in the same transaction, so every writer is captured. In a JPA application, Hibernate Envers does it with @Audited and creates _AUD tables with revision numbers, but only sees Hibernate writes.
For many services, CDC from the write-ahead log with something like Debezium gives central history without touching the write path. Some databases, like MariaDB and SQL Server, have SQL:2011 system-versioned tables that do this automatically; PostgreSQL needs triggers or an extension.
About this answer
- Last updated
- 12 November 2026
- 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.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
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 "What is a history table and how does it differ from storing valid_from and valid_to on...": https://scrivio-interview-library.netlify.app/databases/data-modeling/86f53681ab/