闪回查询(Flashback Query)是 OceanBase 数据库提供的一种可以查看记录某个历史版本数据的能力。默认的查询 SQL 返回的是当前事务隔离级别下允许查询的已提交的版本数据,如果要查询历史上提交的版本,可以在表后指定历史时间信息。
示例:闪回查询历史记录
obclient> create table t1 (id number not null primary key, name varchar2(50) not null, gmt_create timestamp default CURRENT_TIMESTAMP );
Query OK, 0 rows affected (0.06 sec)
obclient> create sequence seq_t1 start with 1;
Query OK, 0 rows affected (0.02 sec)
obclient> insert into t1 (id, name) values(seq_t1.nextval, 'A');commit;
Query OK, 1 row affected (0.01 sec)
Query OK, 0 rows affected (0.00 sec)
obclient> insert into t1 (id, name) values(seq_t1.nextval, 'B');commit;
Query OK, 1 row affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
obclient> insert into t1 (id, name) values(seq_t1.nextval, 'C');commit;
Query OK, 1 row affected (0.00 sec)
Query OK, 0 rows affected (0.01 sec)
obclient> insert into t1 (id, name) values(seq_t1.nextval, 'D');commit;
Query OK, 1 row affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
obclient> select * from t1;
+----+------+---------------------+
| ID | NAME | GMT_CREATE |
+----+------+---------------------+
| 1 | A | 2020-04-02 17:41:51 |
| 2 | B | 2020-04-02 17:41:57 |
| 3 | C | 2020-04-02 17:42:04 |
| 4 | D | 2020-04-02 17:42:10 |
+----+------+---------------------+
4 rows in set (0.00 sec)
obclient> select * from t1 as of timestamp to_timestamp('2020-04-02 17:41:57','YYYY-MM-DD HH24:MI:SS');
+----+------+---------------------+
| ID | NAME | GMT_CREATE |
+----+------+---------------------+
| 1 | A | 2020-04-02 17:41:51 |
| 2 | B | 2020-04-02 17:41:57 |
+----+------+---------------------+
2 rows in set (0.00 sec)
闪回查询对能查询的最早时间有个限制,受变量 undo_retention 控制。如果 undo_retention 未设置或设置为 0,则可以查询集群上次 major freeze 之后最后一次 minor freeze 之后的历史数据。变量 undo_retention 的修改需要有租户的管理员权限,通过 set global 命令修改,并且只对修改之后的会话产生的数据生效。
obclient> show variables like 'undo_retention';
+----------------+-------+
| VARIABLE_NAME | VALUE |
+----------------+-------+
| undo_retention | 0 |
+----------------+-------+
1 row in set (0.00 sec)
当集群发生 minor freeze 事件后,再查询这个历史时间点数据,会报错。
obclient> select * from t1 as of timestamp to_timestamp('2020-04-02 17:41:57','YYYY-MM-DD HH24:MI:SS');
ORA-08186: invalid timestamp