Updated on 2024-07-04 GMT+08:00

Flashback Query

Background

Flashback query enables you to query a snapshot of a table at a certain time point in the past. This feature can be used to view and logically rebuild damaged data that is accidentally deleted or modified. The flashback query is based on the MVCC mechanism. You can retrieve and query an earlier version to obtain the data of the specified version.

Prerequisites

The undo_retention_time parameter has been set for specifying the retention period of undo logs.

Syntax

{[ ONLY ] table_name [ * ] [ partition_clause ] [ [ AS ] alias [ ( column_alias [, ...] ) ] ]
[ TABLESAMPLE sampling_method ( argument [, ...] ) [ REPEATABLE ( seed ) ] ]
[TIMECAPSULE { TIMESTAMP | CSN } expression ]
|( select ) [ AS ] alias [ ( column_alias [, ...] ) ]
|with_query_name [ [ AS ] alias [ ( column_alias [, ...] ) ] ]
|function_name ( [ argument [, ...] ] ) [ AS ] alias [ ( column_alias [, ...] | column_definition [, ...] ) ]
|function_name ( [ argument [, ...] ] ) AS ( column_definition [, ...] )
|from_item [ NATURAL ] join_type from_item [ ON join_condition | USING ( join_column [, ...] ) ]}

In the syntax tree, TIMECAPSULE {TIMESTAMP | CSN} expression is a new expression for the flashback function. TIMECAPSULE indicates that the flashback function is used. TIMESTAMP and CSN indicate that the flashback function uses specific time point information or commit sequence number (CSN) information.

Parameter Description

  • TIMESTAMP
    • Specifies a historical time point of the table data to be queried.
  • CSN
    • Specifies a logical commit time point of the data in the entire database to be queried. Each CSN in the database represents a consistency point of the entire database. To query the data under a CSN means to query the data related to the consistency point in the database through SQL statements.

Note: When the time point is used for flashback, there may be a 3s error. To flash back to an operation point exactly, you need to use CSN for flashback. In GTM-free mode, there is no globally unified CSN. Therefore, flashback in CSN mode is not supported.

Examples

  • Example:
    gaussdb=# drop TABLE IF EXISTS "public".flashtest;
    NOTICE:  table "flashtest" does not exist, skipping
    DROP TABLE
    -- Create the flashtest table.
    gaussdb=# CREATE TABLE "public".flashtest (col1 INT,col2 TEXT) with(storage_type=ustore);
    NOTICE:  The 'DISTRIBUTE BY' clause is not specified. Using 'col1' as the distribution column by default.
    HINT:  Please use 'DISTRIBUTE BY' clause to specify suitable data distribution column.
    CREATE TABLE
    -- Query the CSN.
    gaussdb=# select int8in(xidout(next_csn)) from gs_get_next_xid_csn();
      int8in  
    ----------
     79351682
     79351682
     79351682
     79351682
     79351682
     79351682
    (6 rows)
    -- Query the current timestamp.
    gaussdb=# select now();
                  now              
    -------------------------------
     2023-09-13 19:35:26.011986+08
    (1 row)
    -- Insert data.
    gaussdb=# INSERT INTO flashtest VALUES(1,'INSERT1'),(2,'INSERT2'),(3,'INSERT3'),(4,'INSERT4'),(5,'INSERT5'),(6,'INSERT6');
    INSERT 0 6
    gaussdb=# SELECT * FROM flashtest;
     col1 |  col2   
    ------+---------
        3 | INSERT3
        1 | INSERT1
        2 | INSERT2
        4 | INSERT4
        5 | INSERT5
        6 | INSERT6
    (6 rows)
    -- Use flashback query to query the table at a CSN.
    gaussdb=# SELECT * FROM flashtest TIMECAPSULE CSN 79351682;
     col1 | col2 
    ------+------
    (0 rows)
    gaussdb=# SELECT * FROM flashtest;
     col1 |  col2   
    ------+---------
        1 | INSERT1
        2 | INSERT2
        4 | INSERT4
        5 | INSERT5
        3 | INSERT3
        6 | INSERT6
    (6 rows)
    -- Use flashback query to query the table at a timestamp.
    gaussdb=# SELECT * FROM flashtest TIMECAPSULE TIMESTAMP '2023-09-13 19:35:26.011986';
     col1 | col2 
    ------+------
    (0 rows)
    gaussdb=# SELECT * FROM flashtest;
     col1 |  col2   
    ------+---------
        1 | INSERT1
        2 | INSERT2
        4 | INSERT4
        5 | INSERT5
        3 | INSERT3
        6 | INSERT6
    (6 rows)
    -- Use flashback query to query the table at a timestamp.
    gaussdb=# SELECT * FROM flashtest TIMECAPSULE TIMESTAMP to_timestamp ('2023-09-13 19:35:26.011986', 'YYYY-MM-DD HH24:MI:SS.FF');
     col1 | col2 
    ------+------
    (0 rows)
    -- Use flashback query to query the table at a CSN and rename the table.
    gaussdb=# SELECT * FROM flashtest AS ft TIMECAPSULE CSN 79351682;
     col1 | col2 
    ------+------
    (0 rows)
    gaussdb=# drop TABLE IF EXISTS "public".flashtest;
    DROP TABLE