在很多应用系统中,用户需要查询数据库中的某个时间点,或者特定版本的数据来完成一些数据分析或汇总之类的操作。

OceanBase 数据库在 V2.2.7x 版本中提供了 restore point 功能,允许用户在租户上创建 restore point,将历史版本的数据保存下来。restore point 功能类似于租户的快照点,用户可以通过闪回查询的方式来访问特定版本的历史数据。

通过 语句创建租户级的 restore point,示例如下:

创建 restore point 后,可以通过查询 V$RESTORE_POINT视图来查看当前可用的 restore point,示例语句如下:

  1. obclient> SELECT * FROM V$RESTORE_POINT;

如果需要进一步查询数据,可以根据上述语句查询到的版本号(例如:10000),执行以下命令进行数据的查询分析:

  • MySQL 模式

    1. obclient> SELECT * FROM table_name AS OF SNAPSHOT 10000;

保留的 restore point 对应的数据会占用相应的存储资源,在分析业务结束后需要手动执行删除 restore point 的操作。

  1. obclient> DROP RESTORE POINT restore_point;

通过以下示例简单介绍如何在 Oracle 模式下使用 restore point 功能访问特定版本的数据。

  1. 创建一个 测试表 test。

    1. obclient> CREATE TABLE test ( id number primary key, name varchar2(20));
  2. 向表中插入一些数据并提交。

    1. obclient> INSERT INTO test values (1, 'test');
    2. obclient> INSERT INTO test values (2, 'test1');
    3. Query OK, 1 row affected (0.00 sec)
    4. obclient> commit;
    5. Query OK, 0 rows affected (0.00 sec)
  3. 查询表 test 当前版本的数据。

    1. | ID | NAME |
    2. +----+-------+
    3. | 1 | test |
    4. | 2 | test1 |
    5. 2 rows in set (0.00 sec)

当前 restore point 功能的使用限制如下:

  • 不支持物理备份。

  • 不支持主备库。

  • 创建 restore point 后,如果对创建 restore point 前就存在的表执行 DDL 语句,系统会报 的错误。

  • 由于 restore point 功能依赖 GTS 维护全局的一致性快照,故在使用 restore point 时,需要开启 GTS。

    开启 GTS 的 SQL 命令如下。

    1. obclient> set GLOBAL ob_timestamp_service='GTS';