Temporal tables (MariaDB)
System-versioned tables (WITH SYSTEM VERSIONING, FOR SYSTEM_TIME, DELETE HISTORY) and application-time periods (PERIOD FOR, FOR PORTION OF, WITHOUT OVERLAPS) of the MariaDB dialect.
MariaDB keeps the past of a table in two ways, and the MariaDB dialect of Raft DB has both (D100). System versioning keeps every version of every row with the time it was current. Application-time periods let a table hold rows that are valid for a period of the application's own time. MySQL has neither: in the MySQL dialect every statement on this page is a syntax error, as it is in MySQL.
System-versioned tables#
CREATE TABLE t (id INT PRIMARY KEY, x INT) WITH SYSTEM VERSIONING;
CREATE TABLE t (
id INT, x INT,
start_ts TIMESTAMP(6) GENERATED ALWAYS AS ROW START [INVISIBLE],
end_ts TIMESTAMP(6) GENERATED ALWAYS AS ROW END [INVISIBLE],
PERIOD FOR SYSTEM_TIME (start_ts, end_ts)
) WITH SYSTEM VERSIONING;
col_name data_type [WITH | WITHOUT] SYSTEM VERSIONING
ALTER TABLE t ADD SYSTEM VERSIONING;
ALTER TABLE t DROP SYSTEM VERSIONING;
SELECT ... FROM t FOR SYSTEM_TIME
{ ALL | AS OF [TIMESTAMP] time | FROM time TO time | BETWEEN time AND time } [[AS] alias]
DELETE HISTORY FROM t [BEFORE SYSTEM_TIME time]A versioned table has two more columns, row_start and row_end (invisible: SELECT * leaves them out, SELECT row_start, row_end reads them), of type TIMESTAMP(6). A row that is current has row_end = '2038-01-19 03:14:07.999999'. You can also name the two columns yourself, with GENERATED ALWAYS AS ROW START and ROW END and the PERIOD FOR SYSTEM_TIME clause; they are then ordinary visible columns that an INSERT has to give as DEFAULT (a value written for them is error 1906). The columns can only be made TIMESTAMP(6); the transaction-id form (BIGINT UNSIGNED columns) is refused with error 1235.
What is kept. UPDATE, DELETE, REPLACE and INSERT ... ON DUPLICATE KEY UPDATE copy the row as it was into the history, with row_end set to the time of the statement, and a changed row gets that time as its row_start. The time is the statement's start with microseconds, one value for all its rows. Two statements of a transaction get two times. SET timestamp = x fixes it (and NOW()) for the session, which is how the examples on this page and the tests get the same times every run. A change in the very moment the row was made leaves no version; a deletion always leaves the last one. A change that touches only columns declared WITHOUT SYSTEM VERSIONING makes no history. A rolled back transaction, a failed statement and ROLLBACK TO take the history back with the change.
What is read. A plain query sees only the current rows and costs what it cost before the table was versioned: the history is in a hidden table of the database, __h_<table> (never listed), read only by FOR SYSTEM_TIME. A row is there AS OF a time from its row_start (included) to its row_end (not included); FROM a TO b takes the rows that were there at some time of [a, b), BETWEEN a AND b those of [a, b]; an empty or backward FROM ... TO (and a backward BETWEEN) has none. A bound that is NULL or is not a time gives no rows. The clause can follow any table of a FROM (also in a subquery, a derived table, a WITH or a join), before its alias. On a view it goes to every versioned table in the view that has no clause of its own; a view can also hold a clause. FOR SYSTEM_TIME on a table that is not versioned is error 4124.
What is refused, with MariaDB's errors: TRUNCATE TABLE (4137), CREATE TEMPORARY TABLE ... WITH SYSTEM VERSIONING (4137), the transaction-precise AS OF TRANSACTION and friends (4111), changing a column of a versioned table and adding or dropping a unique key (4119, MariaDB's system_versioning_alter_history = ERROR; adding a plain index, dropping one, renaming a column or the table, and ADD/DROP SYSTEM VERSIONING are done), writing row_start or row_end (an unknown column) and a table with no versioned column (4123).
ALTER TABLE ... ADD SYSTEM VERSIONING gives the rows that are there row_start = the time of the statement. Dropping the versioning of a table whose ROW START / ROW END columns you named is not possible without dropping those columns (error 4125), and a column of a versioned table cannot be dropped (4119), so such a table stays versioned, as in MariaDB.
Differences from MariaDB. The history lives in a table of its own, so SHOW TABLE STATUS and the like do not count it; PARTITION BY SYSTEM_TIME is accepted and not kept (as all partitioning is, D94). GRANT DELETE HISTORY is a syntax error: DELETE HISTORY needs the privilege DELETE. A foreign-key ON UPDATE action on a versioned table writes no history (a cascaded delete keeps it). A duplicate-key message on a table with columns of your own shows row_end in UTC where MariaDB shows it in the session's time zone. UPDATE t FOR SYSTEM_TIME ... is refused with 1099 (as MariaDB does), DELETE FROM t FOR SYSTEM_TIME is a syntax error.
Application-time periods#
CREATE TABLE r (room INT, ci DATE, co DATE, PERIOD FOR p (ci, co),
UNIQUE (room, p WITHOUT OVERLAPS));
ALTER TABLE r ADD PERIOD [IF NOT EXISTS] FOR p (ci, co);
ALTER TABLE r DROP PERIOD [IF EXISTS] FOR p;
UPDATE r FOR PORTION OF p FROM x TO y SET ... [WHERE ...] [ORDER BY ...] [LIMIT n];
DELETE FROM r FOR PORTION OF p FROM x TO y [WHERE ...] [ORDER BY ...] [LIMIT n];A period names two DATE, DATETIME or TIMESTAMP columns of one type; they become NOT NULL, and the period is half open, [start, end). A table has at most one (error 4154), a period can be called application_time, and start < end is checked on every write as a hidden CHECK (error 4025, CONSTRAINT pfailed fordb.t``). The columns of a period cannot be dropped, renamed or given another type. information_schema.PERIODS lists them.
FOR PORTION OF changes only the part of a row that lies inside [x, y). UPDATE changes the inside (the row's period becomes the inside, the SET is applied) and leaves the parts before x and after y as new rows with the old values; DELETE removes the inside the same way. The count a client gets is the rows that matched, not the rows made. The bounds are expressions converted to the column's type; an empty or backward portion, or a NULL one, changes nothing. The columns of the period cannot be set (4157); an unknown period is 4156. The triggers fire as for the statements the portion is made of: update (delete) for the row, insert for the rows left behind. The whole statement is undone if one of the new rows is refused (a plain primary key would refuse them: it repeats).
WITHOUT OVERLAPS keys (UNIQUE or PRIMARY KEY, the period last) refuse a row that has the same key columns as another row while the periods overlap; the message is The value 'room-end-start' is already present for key 'name'. A key with a NULL in it never conflicts. REPLACE and ON DUPLICATE KEY UPDATE on such a table are refused (1235, as MariaDB does), and INSERT IGNORE leaves the row out without a warning. The check reads the rows with the same key and compares the periods; unlike an ordinary unique key it keeps no claim, so two sessions that write overlapping rows at the same moment can both succeed.
Application-time periods and system versioning can be on one table (PERIOD FOR application_time (a, b) and PERIOD FOR SYSTEM_TIME (...)): FOR PORTION OF then keeps history like any other UPDATE/DELETE.