MariaDB system-versioned tables for audit history: design and limits
Use MariaDB system-versioned tables for point-in-time audit reads, but plan for manual history retention and privilege boundaries.
18 Aug 2026, 00:23 UTC

The problem and the takeaway
You need to answer what did this row look like last Tuesday without maintaining application change logs. Triggers and shadow tables can do this, but they couple audit logic to every write path and are easy to forget or bypass.
MariaDB system-versioned tables, available since MariaDB 10.3, keep a linked history table automatically and let you read past row states with FOR SYSTEM_TIME. The useful takeaway: use them for low-to-moderate write audit trails where point-in-time reads matter more than event volume. Do not use them as a security boundary, an event log, or an append-only compliance record, and plan to purge history yourself because MariaDB does not auto-expire it.
Requirements: when this design fits
- You need point-in-time reads of row values, not a full log of who changed what and why.
- Write volume is low to moderate. Constant micro-batches and bulk reloads are poor fits.
- You can schedule retention purging; the database will not remove old history for you.
- Schema changes are infrequent enough to plan around table rebuilds.
Smallest suitable design
Create the base table with system versioning. MariaDB creates a linked history table for prior row versions.
CREATE TABLE invoices (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
customer_id BIGINT UNSIGNED NOT NULL,
amount DECIMAL(12,2) NOT NULL,
status VARCHAR(20) NOT NULL,
PRIMARY KEY (id)
) WITH SYSTEM VERSIONING;
Run this in the mysql or mariadb client as a user with CREATE and ALTER privileges on the target schema. Query a past state like this:
SELECT id, amount, status
FROM invoices
FOR SYSTEM_TIME AS OF '2026-11-01 12:00:00'
WHERE id = 12345;
Or a range:
SELECT id, amount, status
FROM invoices
FOR SYSTEM_TIME BETWEEN '2026-11-01' AND '2026-11-02';
System-versioned tables add hidden time-boundary columns to track when each version was current. Confirm the actual history table name and column names with SHOW CREATE TABLE invoices; before writing any purge or diagnostic query against the history table.
Trust and data boundaries
System-versioning is not access control. A user who can read the base table may also read history unless you explicitly restrict privileges on the history table. It does not capture the application user, session, or business reason for a change; it stores row values and server time only. Treat history-table data as readable by anyone with table privileges, and grant or revoke on the history table separately if needed.
| Factor | System-versioned table | Application audit table |
|---|---|---|
| Point-in-time row reads | Built in | Custom queries |
| Change context (user, reason) | Not stored | Stored by design |
| History retention | Manual purge required | Manual purge required |
| Write overhead | Base plus history on every UPDATE and DELETE | One insert per recorded event |
Operational checks
- Run
SHOW CREATE TABLE invoices;as a user withSELECTprivileges and confirm the output containsWITH SYSTEM VERSIONINGand lists a history table name. - Run a representative batch of updates, then run
SELECT COUNT(*) FROM <history_table>;to confirm history rows are being written. - Run
EXPLAIN SELECT ... FOR SYSTEM_TIME ...for your temporal queries and confirm the optimizer reads the history table when needed. - Monitor
data_lengthandindex_lengthininformation_schema.tablesfor the history table. Graph it over one to two weeks to detect unbounded growth.
Retention is manual. Identify the history table and its time-boundary columns, then delete old versions in small batches before disk pressure becomes critical. Test any purge statement on a replica first.
Failure modes
- Unbounded disk growth. Every UPDATE and DELETE writes a prior row version to the history table. Without purging, the history table grows forever.
- Write amplification. Each write touches the base table, the history table, and indexes on both. Benchmark under production write load before adopting.
- ALTER TABLE cost. Adding or changing a column can rebuild both the base and history tables and hold metadata locks. Schedule schema changes in a maintenance window.
- Clock dependence.
FOR SYSTEM_TIMEuses server system time. Clock changes or time zone confusion can make point-in-time results harder to interpret. - Not tamper-proof. A user with DELETE on the history table can remove rows. It is not an immutable compliance log.
Conditions that would change the design
- High-throughput event capture: use an application event table or change data capture instead.
- Need to store who changed a row and why: add an application-level audit table; system-versioning only stores row values and system time.
- Frequent schema evolution: versioning adds DDL complexity; consider a separate audit schema.
- Regulatory requirement for an append-only, verifiable log: system-versioned tables are not designed for that purpose.
Practical verification
SHOW CREATE TABLE invoices;confirmsWITH SYSTEM VERSIONINGand the history table name.SELECT ... FOR SYSTEM_TIME AS OF 'timestamp'returns rows as they existed at that time.- After a batch of updates, the history table row count increases with write volume.
- History table disk usage trends upward predictably; if it accelerates, implement or tune the retention purge.
Disabling system versioning is a state-changing operation. Test ALTER TABLE ... WITHOUT SYSTEM VERSIONING in staging before running it on production, and confirm whether your MariaDB version retains or drops the history table.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.