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

Flashback Query

Context

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 the earlier version to obtain the data of the specified earlier version.

Prerequisites

The overall solution consists of three parts: earlier version retention, snapshot maintenance, and earlier version retrieval. Earlier version retention: The undo_retention_time parameter is added to set the retention period of an earlier version. Beyond the retention period, the earlier version will be reclaimed and deleted. To use flashback query, you must set this parameter to a value greater than 0. Contact the administrator to change the value.

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.

Parameters

  • TIMESTAMP
    • Specifies a history 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 barrier of the entire database. To query the data under a CSN means to query the data related to the barrier in the database through SQL statements.

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.

Examples

  • Example (set undo_retention_time to a value greater than 0):
    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);
    CREATE TABLE
    -- Query the CSN.
    gaussdb=# SELECT int8in(xidout(next_csn)) FROM gs_get_next_xid_csn();
      int8in  
    ----------
     79351682
    (1 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