AS OF SYSTEM TIME timestamp clause causes statements to execute using the database contents “as of” a specified time in the past.
You can use this clause to read historical data (also known as “time travel queries”) and to improve performance by decreasing transaction conflicts. See .
Historical data is available only within the garbage collection window, which is determined by the
ttlseconds field in the .Synopsis
TheAS OF SYSTEM TIME clause is supported in multiple SQL contexts, including but not limited to:
- In , at the very end of the
FROMsub-clause. The locking clause is not allowed withAS OF SYSTEM TIME. - In , after the parameters of the
TOsub-clause. - In , after the parameters of the
FROMsub-clause. - In , after the
BEGINkeyword. - In , after the
SET TRANSACTIONkeyword.
AS OF SYSTEM TIME cannot be used with:
- (
SELECT... FOR UPDATEandSELECT... FOR SHARE). - (such as or ).
cannot execute {SQL STATEMENT} in a read-only transaction.
Parameters
Thetimestamp argument supports the following formats:
| Format | Notes |
|---|---|
| Nanoseconds since the Unix epoch. | |
| negative | Added to statement\_timestamp(), and thus must be negative. |
| A , of nanoseconds, or negative . | |
follower\_read\_timestamp() | A that returns the statement\_timestamp() - 4.2s. Using this function will set the time as close as possible to the present time while remaining safe for . |
with\_min\_timestamp(TIMESTAMPTZ, [nearest\_only]) | The minimum at which to perform the . The actual timestamp of the read may be equal to or later than the provided timestamp, but cannot be before the provided timestamp. This is useful to request a read from nearby followers, if possible, while enforcing causality between an operation at some point in time and any dependent reads. This function accepts an optional nearest\_only argument that will error if the reads cannot be serviced from a nearby replica. |
with\_max\_staleness(INTERVAL, [nearest\_only]) | The maximum staleness interval with which to perform the . The timestamp of the read can be at most this stale with respect to the current time. This is useful to request a read from nearby followers, if possible, while placing some limit on how stale results can be. Note that with\_max\_staleness(INTERVAL) is equivalent to with\_min\_timestamp(now() - INTERVAL). This function accepts an optional nearest\_only argument that will error if the reads cannot be serviced from a nearby replica. |
Examples
Select historical data (time-travel)
Imagine this example represents the database’s current data:Using different timestamp formats
Assuming the following statements are run at2016-01-01 12:00:00, they would execute as of 2016-01-01 08:00:00:
Selecting from multiple tables
It is not yet possible to select from multiple tables at different timestamps. The entire query runs at the specified time in the past.
FROM clause, the AS OF SYSTEM TIME clause must appear at the very end and applies to the entire SELECT clause.
For example:
Using AS OF SYSTEM TIME in subqueries
To enable time travel, the AS OF SYSTEM TIME clause must appear in at least the top-level statement. It is not valid to use it only in a .
For example, the following is invalid:
AS OF SYSTEM TIME in sub-queries under the following conditions:
- The top level query also specifies
AS OF SYSTEM TIME. - All the
AS OF SYSTEM TIMEclauses specify the same timestamp.
Use AS OF SYSTEM TIME in transactions
You can use the statement to execute the transaction using the database contents “as of” a specified time in the past.
Use AS OF SYSTEM TIME to recover recently lost data
It is possible to recover lost data as a result of an online schema change prior to when begins:
Once garbage collection has occurred,
AS OF SYSTEM TIME will no longer be able to recover lost data. For more long-term recovery solutions, consider taking either a of your cluster.See also
Tech note
Although the following format is supported, it is not intended to be used by most users. HLC timestamps can be specified using a . The integer part is the wall time in nanoseconds. The fractional part is the logical counter, a 10-digit integer. This is the same format as produced by thecluster_logical_timestamp() function.
