闪回查询(Flashback Query)是 OceanBase 提供的一种可以查看记录某个历史版本数据的能力。默认的查询SQL 返回的是当前事务隔离级别下允许查询的已提交的版本数据,如果要查询历史上提交的版本,可以在表后指定历史时间信息。

    示例:闪回查询历史记录

    1. obclient> create table t1 (id number not null primary key, name varchar2(50) not null, gmt_create date default sysdate );
    2. Query OK, 0 rows affected (0.06 sec)
    3. obclient> create sequence seq_t1 start with 1;
    4. Query OK, 0 rows affected (0.02 sec)
    5. obclient> insert into t1 (id, name) values(seq_t1.nextval, 'A');commit;
    6. Query OK, 1 row affected (0.01 sec)
    7. Query OK, 0 rows affected (0.00 sec)
    8. obclient> insert into t1 (id, name) values(seq_t1.nextval, 'B');commit;
    9. Query OK, 1 row affected (0.00 sec)
    10. Query OK, 0 rows affected (0.00 sec)
    11. obclient> insert into t1 (id, name) values(seq_t1.nextval, 'C');commit;
    12. Query OK, 1 row affected (0.00 sec)
    13. Query OK, 0 rows affected (0.01 sec)
    14. obclient> insert into t1 (id, name) values(seq_t1.nextval, 'D');commit;
    15. Query OK, 1 row affected (0.00 sec)
    16. Query OK, 0 rows affected (0.00 sec)
    17. obclient> select * from t1;
    18. +----+------+---------------------+
    19. | ID | NAME | GMT_CREATE |
    20. +----+------+---------------------+
    21. | 1 | A | 2020-04-02 17:41:51 |
    22. | 2 | B | 2020-04-02 17:41:57 |
    23. | 3 | C | 2020-04-02 17:42:04 |
    24. | 4 | D | 2020-04-02 17:42:10 |
    25. +----+------+---------------------+
    26. 4 rows in set (0.00 sec)
    27. obclient> select * from t1 as of timestamp to_timestamp('2020-03-30 15:08:25','YYYY-MM-DD HH24:MI:SS');
    28. +----+------+---------------------+
    29. | ID | NAME | GMT_CREATE |
    30. +----+------+---------------------+
    31. | 1 | A | 2020-04-02 17:41:51 |
    32. | 2 | B | 2020-04-02 17:41:57 |
    33. +----+------+---------------------+
    34. 2 rows in set (0.00 sec)

    闪回查询对能查询的最早时间有个限制,受变量 undo_retention 控制。如果 undo_retention 未设置,则可以查询集群上次 major freeze 之后最后一次 minor freeze 之后的历史数据。变量 undo_retention 的修改需要有租户的管理员权限,通过 set global 命令修改,并且只对修改之后的会话产生的数据生效。

    1. obclient> show variables like 'undo_retention';
    2. +----------------+-------+
    3. | VARIABLE_NAME | VALUE |
    4. +----------------+-------+
    5. | undo_retention | 0 |
    6. +----------------+-------+
    7. 1 row in set (0.00 sec)

    当集群发生 minor freeze 事件后,再查询这个历史时间点数据,会报错。

    1. obclient> select * from t1 as of timestamp to_timestamp('2020-03-30 15:08:25','YYYY-MM-DD HH24:MI:SS');
    2. ERROR-08186: invalid timestamp