OceanBase Database supports record-specific flashback queries, which allow 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. For more information about the undo_retention parameter, see undo_retention.
Considerations
When you set the undo_retention parameter to perform a flashback query, note the following:
If you set the
undo_retentionparameter to T0, 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.
Assuming that the current time is
T, after you set theundo_retentionparameter, if a DDL operation is performed in the time range fromT - undo_retentionto the current time, the system uses the latest data dictionary to explain the data in the table. For data generated before the DDL operation is performed, the deleted column data is changed to the deleted state, and the added column data is set to the default value.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
Before you set the undo_retention parameter to perform a flashback query, check whether multi-version minor compaction is enabled, namely whether the value of the undo_retention parameter is not 0. The default value of the undo_retention parameter is 1800, in seconds.
- Log on to the database as a tenant administrator.
Note
The administrator user of a MySQL user tenant is root and that of an Oracle user tenant is SYS.
Example (the command can vary in practice):
obclient -h10.xx.xx.xx -P2883 -uroot@mysql -p***** -A
For more information, see Database connection overview.
Execute the following statement to modify the value of the
undo_retentionparameter.Example:
obclient [(none)]> ALTER SYSTEM SET undo_retention=900;For more information about the
undo_retentionparameter, see undo_retention.
Run a flashback query in MySQL mode
Log on to the database as an administrator of a MySQL user tenant.
Example (the command can vary in practice):
obclient -h10.xx.xx.xx -P2883 -uinfo@mysql -p***** -AFor more information, see Database connection 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;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
Run a flashback query in Oracle mode
Log on to the database as an administrator of an Oracle user tenant.
Example (the command can vary in practice):
obclient -h10.xx.xx.xx -P2883 -uinfo@mysql -p***** -AFor more information, see Database connection 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 AS OF TIMESTAMP expr1,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;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