KingbaseES 的闪回查询

2022-11-12,,

KingbaseES V008R006C006B0013版本新增支持闪回查询,闪回版本查询、闪回表到指定时间点。旧版本已支持闪回回收站技术。

闪回技术(闪回查询和闪回表到指定时间点)可以通过时间戳和CSN(commit sequence number)两种方式进行。
两种方式目前都需要依赖提交的时间戳,因此需要开启track_commit_timestamp这个参数。另外闪回查询还需要开启kdb_flashback.enable_flashback_query(新版本已默认开启)的参数。
这里解释下时间戳和 CSN 两个快照指定的方式,时间戳即 timestamp 用户可以在闪回查询的快照时间表达式里指定任意一个有效的时间戳。如果允许闪回查询,那么将返回该时刻能够可见的历史快照的数据。

CSN 是提交顺序号,第一个有效的 CSN 为 65536000000,CSN 随着提交线性增加。因此如果用户指定一个有效的 CSN,比如 65536000160,那么闪回查询将基于这个 CSN 提交号构建历史快照。
闪回查询技术有一定的限制,在数据进行深度清理后和部分 schema change 后将拒绝闪回查询。

闪回查询

create table t1(id int, name varchar(100));
insert into t1 values(1, 'AA');
insert into t1 values(2, 'BB');
insert into t1 values(3, 'CC'); test=# select * from t1;
id | name
----+------
1 | AA
2 | BB
3 | CC
(3 行记录) 在更新前,记录下时间
test=# select now();
now
-------------------------------
2022-08-05 14:22:30.617930+08
(1 行记录) 假设误操作更新了全表数据
test=# update t1 set name = null ;
UPDATE 3
test=# select * from t1;
id | name
----+------
1 |
2 |
3 |
(3 行记录) 使用闪回查询查历史快照
test=# select * from t1 as of timestamp to_timestamp('2022-08-05 14:22:30', 'yyyy-mm-dd hh24:mi:ss');
id | name
----+------
1 | AA
2 | BB
3 | CC
(3 行记录) 使用csn进行闪回查询示例
test=# select versions_startscn, versions_endcsn, * from t1 versions between csn minvalue and maxvalue;
versions_startscn | versions_endcsn | id | name
-------------------+-----------------+----+------
65536000009 | 65536000012 | 1 | AA
65536000010 | 65536000012 | 2 | BB
65536000011 | 65536000012 | 3 | CC
65536000012 | | 1 |
65536000012 | | 2 |
65536000012 | | 3 |
(6 行记录) test=# select * from t1 as of csn 65536000011;
id | name
----+------
1 | AA
2 | BB
3 | CC
(3 行记录) 使用闪回表的功能对表进行恢复 test=# select * from t1 ;
id | name
----+------
1 |
2 |
3 |
(3 行记录)
test=# flashback table t1 to csn 65536000011;
FLASHBACK TABLE
test=#
test=#
test=# select * from t1;
id | name
----+------
1 | AA
2 | BB
3 | CC
(3 行记录)

闪回查询的一些限制

闪回查询和闪回表技术依赖于历史的数据,如果历史数据因为 vacuum、truncate、rewrite 等操作被回收掉,那么会导致无法闪回到这些操作之前的时刻。

因此推荐用户在期望使用闪回查询的时候对 vacuum相关参数做一定的调整(关闭表级的 autovacuum, 推荐调大 vacuum_defer_cleanup_age 的值以降低历史数据被回收的机会)。
目前闪回查询和闪回表技术在 vacuum、truncate、和部分 ddl 之后将不允许进行闪回到这些操作之前。
闪回查询应用于视图或者物化视图里面应该尽量避免对于常量时间戳和CSN的使用,可能会引发dump和restore的失败

KingbaseES 的闪回查询的相关教程结束。

《KingbaseES 的闪回查询.doc》

下载本文的Word格式文档,以方便收藏与打印。