OceanBase Database supports record-specific flashback queries, which allows you to obtain data of a specific historical version. You can use the AS OF SCN and AS OF TIMESTAMP clauses to perform flashback queries in Oracle mode and the AS OF SNAPSHOT clause to perform flashback queries in MySQL mode.
OceanBase Database allows you to set the undo_retention parameter for a tenant to perform a flashback query. After you set the undo_retention parameter, you can use the flashback query feature to obtain data of multiple versions in the time range from T - undo_retention to T. The default value of the undo_retention parameter is 1800, in seconds.
Considerations
When you set the undo_retention parameter to perform a flashback query, note that:
If you set the
undo_retentionparameter toT0, this feature takes effect only on data generated afterT - T0.If the queried table is moved to the recycle bin, you must restore it from the recycle bin before you can query data in the table.
For more information, see Restore objects from the recycle bin.
Assume that the current time is
T. If a DDL operation is performed within the time range fromT - undo_retentiontoT, the following rules apply:If the DDL operation is to add columns, and you initiate a flashback query to query data generated before the operation, the system returns default values for all new columns.
If the DDL operation is to create a table, drop a table, or drop a column, and you initiate a flashback query to query data generated before the operation, the system reports an error.
Flashback queries require additional storage space to retain historical data. If you increase the value of the
undo_retentionparameter, the storage space required is increased. We recommend that you observe the change of the storage space after you increase the value of theundo_retentionparameter.
Prerequisites
You have modified the value of the undo_retention parameter. The default value of the undo_retention parameter is 1800, in seconds. To modify the value, perform the following operations:
Log in to the database as a tenant administrator.
Note
The administrator user is the
rootuser in a MySQL tenant and theSYSuser in an Oracle tenant.Note that you must specify the corresponding parameters in the following sample code based on your actual database configurations.
obclient -h10.xx.xx.xx -P2883 -uroot@mysql#demo -p***** -AFor more information about how to connect to a database, see Overview (MySQL mode) or Overview (Oracle mode).
Modify the value of the
undo_retentionparameter.Here is an example:
obclient [(none)]> ALTER SYSTEM SET undo_retention=900;
Perform a flashback query in MySQL mode
Log in to the database as the administrator of a MySQL user tenant.
Note that you must specify the corresponding parameters in the following sample code based on your actual database configurations.
obclient -h10.xx.xx.xx -P2883 -uinfo@mysql#demo -p***** -AFor more information, see Overview.
Perform a flashback query on a table.
Assume that the value of the
undo_retentionparameter is900and you have a table namedtable1. Specify a historical point in time by usingAS OF SNAPSHOTand query the status and data of the table at this point in time.obclient [(none)]> SELECT * FROM table1 AS OF SNAPSHOT 1597306800000000000;Here,
1597306800000000000is a timestamp, in nanoseconds. The timestamp is the time since 08:00:00 (UTC+8) on January 1, 1970. You can convert the point in time to be queried into a timestamp based on your business needs.For example, if you want to perform a flashback query on table data at 16:20:00 on August 13, 2020, you can convert the time as follows:
obclient [(none)]> SELECT time_to_usec('2020-08-13 16:20:00') * 1000; +--------------------------------------------+ | time_to_usec('2022-01-01 00:00:00') * 1000 | +--------------------------------------------+ | 1597306800000000000 | +--------------------------------------------+ 1 row in set
Perform a flashback query in Oracle mode
Log in to the database as the administrator of an Oracle user tenant.
Note that you must specify the corresponding parameters in the following sample code based on your actual database configurations.
obclient -h10.xx.xx.xx -P2883 -uinfo@oracle#demo -p***** -AFor more information, see Overview.
Perform a flashback query on a table.
Assume that the value of the
undo_retentionparameter is900and you have three tables:table1,table2, andexpr1. The following examples show the sample code for flashback queries in Oracle mode.Specify a historical point in time by using
TIMESTAMPand query the status and data of a single table at this point in time:obclient [SYS]> SELECT * FROM table1 AS OF TIMESTAMP TO_TIMESTAMP('2020-08-13 16:20:00','yyyy-mm-dd hh24:mi:ss');Specify a historical point in time by using
TIMESTAMPand query the status and data of the multiple tables at this point in time:obclient [SYS]> SELECT * FROM table1,table2 AS OF TIMESTAMP TO_TIMESTAMP('2020-08-13 16:20:00','yyyy-mm-dd hh24:mi:ss');Specify a historical point in time by using
SCNand query the status and data of a single table at this point in time:obclient [SYS]> SELECT * FROM table1 AS OF SCN 1597306800000000000;Here,
1597306800000000000is a timestamp, in nanoseconds. The timestamp is the time since 08:00:00 (UTC+8) on January 1, 1970. You can convert the point in time to be queried into a timestamp based on your business needs.For example, if you want to perform a flashback query on table data at 16:20:00 on August 13, 2020, you can convert the time as follows:
obclient [(none)]> SELECT (to_date('2020-08-13 16:20:00','yyyy-mm-dd hh24:mi:ss') - to_date('1970-01-01 08:00:00', 'yyyy-mm-dd hh24:mi:ss')) * 86400 * 1000 * 1000 * 1000 AS unix_nsec_timestamp FROM DUAL; +---------------------+ | UNIX_NSEC_TIMESTAMP | +---------------------+ | 1597306800000000000 | +---------------------+ 1 row in set