暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

kingbases闪回使用

原创 jack 2023-07-20
293

1.简介

插件 kdb_flashback 是 KingbaseES 的一个扩展插件。库级别参数。主要功能是提供错误数据的快速恢复能力,目前提供的闪回技术包括

闪回回收站、闪回查询、闪回版本查询、闪回到任意时间点、闪回到任意scn号.整个闪回技术 (闪回查询和闪回表到指定时间点) 可以通过时间戳和 CSN(commit sequence number) 两种方式;


使用闪回前提:

1.在数据进行深度清理后和部分 schema change 后将拒绝闪回查询;

2.闪回查询和闪回表技术依赖于历史的数据,如果历史数据因为 vacuum、truncate、rewrite 等操作被回收掉, 那么会导致无法闪回到这些操作之前的时刻。因此推荐用户在期望使用闪回查询的时候对 vacuum 相关参数做 一定的调整(关闭表级的 autovacuum, 推荐调大 vacuum_defer_cleanup_age 的值以降低历史数据被回收的机 会)。

3.目前闪回查询和闪回表技术在 vacuum、truncate、和部分 ddl 之后将不允许进行闪回到这些操作之前。

4.回查询应用于视图或者物化视图里面应该尽量避免对于常量时间戳和 CSN 的使用,可能会引发 dump 和 restore 的失败。

2.开启闪回

整个实例有效:

1. kingbase.conf 文件加入:

shared_preload_libraries = 'kdb_flashback'

2. 参数

闪回查询的开启参数
kdb_flashback.enable_flashback_query = on 
闪回回收站开启参数
kdb_flashback.db_recyclebin = on 开启提交的时间戳track_commit_timestamp  = on开启外闪回查询kdb_flashback.enable_flashback_query = on

3. 闪回查询

3.1基于时间戳闪回查询

test=# select  * from  test ;
 id | id1 
----+-----
  1 | a
  2 | b
(2 rows)
test=# update   test set  id1 = 'c' where id = 1
test=# select * from test as of timestamp ' 2023-07-20 17:27:33.240756+08'; id | id1 ----+----- 1 | a 2 | b (2 rows)

2.闪回版本查询

:闪回版本查询的伪列


                                                             ^
test=#  select versions_startcsn, versions_endcsn, versions_operation,VERSIONS_STARTTIME,VERSIONS_ENDTIME,VERSIONS_XID,* from test versions between csn minvalue and maxvalue;

 versions_startcsn | versions_endcsn | versions_operation |      VERSIONS_STARTTIME       |       VERSIONS_ENDTIME        | VERSIONS_XID | id | id1 
-------------------+-----------------+--------------------+-------------------------------+-------------------------------+--------------+----+-----
       65536000338 |     65536000340 | D                  | 2023-07-20 17:26:23.306073+08 | 2023-07-20 17:27:34.467034+08 |        13739 |  1 | a
       65536000339 |                 | I                  | 2023-07-20 17:26:26.482848+08 |                               |        13740 |  2 | b
       65536000340 |                 | I                  | 2023-07-20 17:27:34.467034+08 |                               |        13741 |  1 | c
(3 rows)

3.基于CSN闪回查询


test=# select  versions_startscn,versions_endcsn,* from  test versions between csn minvalue and  maxvalue;
 versions_startscn | versions_endcsn | id | id1 
-------------------+-----------------+----+-----
       65536000338 |     65536000340 |  1 | a
       65536000339 |                 |  2 | b
       65536000340 |                 |  1 | c
(3 rows)test=# select    * from  test as of   csn 65536000339 ;                                                   
 id | id1 
----+-----
  1 | a
  2 | b
(2 rows)

4. 闪回表到指定时间点

test=# select    * from  test as of  timestamp ' 2023-07-20 17:27:33.240756+08';                                                    
id | id1 ----+----- 1 | a 2 | b test=# flashback table test to timestamp '2023-07-20 17:27:33.240756+08'; FLASHBACK TABLE test=# select * from test ; id | id1 ----+----- 2 | b 1 | a (2 rows)支持enable/disable trigger 的触发test=# flashback table test to timestamp '2023-07-20 17:27:33.240756+08' enable trigger ;

4. 闪回表到指定csn号


test=# flashback table  test  to csn  65536000339   ;                       
FLASHBACK TABLE
test=# select  * from test ;                        
 id | id1 
----+-----
  2 | b
  1 | a
test=# flashback table  test  to  timestamp '2023-07-20 17:27:33.240756+08' disable trigger ;

5.闪回回收站


test=# drop  table  test ;
DROP TABLE
test=#  flashback table test  to before drop;             
FLASHBACK TABLE
test=# select  * from test ;
 id | id1 
----+-----
  2 | b
  1 | a
(2 rows)  闪回表重命名                                test=#  flashback table test  to before drop  rename to  test1 ; 
FLASHBACK TABLE回收站查询test=#  select * from recyclebin; 
 oid | original_name | droptime | type 
-----+---------------+----------+------
(0 rows)
test=# select   * from  sys_recyclebin ;
 classid | reloid | nspname | object_name | original_name | type | droptime 
---------+--------+---------+-------------+---------------+------+----------
(0 rows)

6.闪回回收站清理

清理单个表=#  select   * from  sys_recyclebin ; 
 classid | reloid | nspname |                object_name                | original_name | type |           droptime            
---------+--------+---------+-------------------------------------------+---------------+------+-------------------------------
    1259 |  48902 | public  | bin$$48902$$2023-07-20 17:46:05.250045+08 | test          | r    | 2023-07-20 17:46:05.250045+08
(1 row)

test=#  purge table  test ;    
PURGE
test=#  select   * from  sys_recyclebin ; 
 classid | reloid | nspname | object_name | original_name | type | droptime 
---------+--------+---------+-------------+---------------+------+----------
(0 rows)清理整个回收站test=#  purge recyclebin;



1.简介

插件 kdb_flashback 是 KingbaseES 的一个扩展插件。库级别参数。主要功能是提供错误数据的快速恢复能力,目前提供的闪回技术包括

闪回回收站、闪回查询、闪回版本查询、闪回到任意时间点、闪回到任意scn号.整个闪回技术 (闪回查询和闪回表到指定时间点) 可以通过时间戳和 CSN(commit sequence number) 两种方式;


使用闪回前提:

1.在数据进行深度清理后和部分 schema change 后将拒绝闪回查询;

2.闪回查询和闪回表技术依赖于历史的数据,如果历史数据因为 vacuum、truncate、rewrite 等操作被回收掉, 那么会导致无法闪回到这些操作之前的时刻。因此推荐用户在期望使用闪回查询的时候对 vacuum 相关参数做 一定的调整(关闭表级的 autovacuum, 推荐调大 vacuum_defer_cleanup_age 的值以降低历史数据被回收的机 会)。

3.目前闪回查询和闪回表技术在 vacuum、truncate、和部分 ddl 之后将不允许进行闪回到这些操作之前。

4.回查询应用于视图或者物化视图里面应该尽量避免对于常量时间戳和 CSN 的使用,可能会引发 dump 和 restore 的失败。

2.开启闪回

整个实例有效:

1. kingbase.conf 文件加入:

shared_preload_libraries = 'kdb_flashback'

2. 参数

闪回查询的开启参数
kdb_flashback.enable_flashback_query = on 
闪回回收站开启参数
kdb_flashback.db_recyclebin = on 开启提交的时间戳track_commit_timestamp  = on开启外闪回查询kdb_flashback.enable_flashback_query = on

3. 闪回查询

3.1基于时间戳闪回查询

test=# select  * from  test ;
 id | id1 
----+-----
  1 | a
  2 | b
(2 rows)
test=# update   test set  id1 = 'c' where id = 1
test=# select * from test as of timestamp ' 2023-07-20 17:27:33.240756+08'; id | id1 ----+----- 1 | a 2 | b (2 rows)

2.闪回版本查询

:闪回版本查询的伪列


                                                             ^
test=#  select versions_startcsn, versions_endcsn, versions_operation,VERSIONS_STARTTIME,VERSIONS_ENDTIME,VERSIONS_XID,* from test versions between csn minvalue and maxvalue;

 versions_startcsn | versions_endcsn | versions_operation |      VERSIONS_STARTTIME       |       VERSIONS_ENDTIME        | VERSIONS_XID | id | id1 
-------------------+-----------------+--------------------+-------------------------------+-------------------------------+--------------+----+-----
       65536000338 |     65536000340 | D                  | 2023-07-20 17:26:23.306073+08 | 2023-07-20 17:27:34.467034+08 |        13739 |  1 | a
       65536000339 |                 | I                  | 2023-07-20 17:26:26.482848+08 |                               |        13740 |  2 | b
       65536000340 |                 | I                  | 2023-07-20 17:27:34.467034+08 |                               |        13741 |  1 | c
(3 rows)

3.基于CSN闪回查询


test=# select  versions_startscn,versions_endcsn,* from  test versions between csn minvalue and  maxvalue;
 versions_startscn | versions_endcsn | id | id1 
-------------------+-----------------+----+-----
       65536000338 |     65536000340 |  1 | a
       65536000339 |                 |  2 | b
       65536000340 |                 |  1 | c
(3 rows)test=# select    * from  test as of   csn 65536000339 ;                                                   
 id | id1 
----+-----
  1 | a
  2 | b
(2 rows)

4. 闪回表到指定时间点

test=# select    * from  test as of  timestamp ' 2023-07-20 17:27:33.240756+08';                                                    
id | id1 ----+----- 1 | a 2 | b test=# flashback table test to timestamp '2023-07-20 17:27:33.240756+08'; FLASHBACK TABLE test=# select * from test ; id | id1 ----+----- 2 | b 1 | a (2 rows)

4. 闪回表到指定csn号


test=# flashback table  test  to csn  65536000339   ;                       
FLASHBACK TABLE
test=# select  * from test ;                        
 id | id1 
----+-----
  2 | b
  1 | a

5.闪回回收站


test=# drop  table  test ;
DROP TABLE
test=#  flashback table test  to before drop;             
FLASHBACK TABLE
test=# select  * from test ;
 id | id1 
----+-----
  2 | b
  1 | a
(2 rows)  闪回表重命名                                test=#  flashback table test  to before drop  rename to  test1 ; 
FLASHBACK TABLE回收站查询test=#  select * from recyclebin; 
 oid | original_name | droptime | type 
-----+---------------+----------+------
(0 rows)
test=# select   * from  sys_recyclebin ;
 classid | reloid | nspname | object_name | original_name | type | droptime 
---------+--------+---------+-------------+---------------+------+----------
(0 rows)

6.闪回回收站清理

清理单个表=#  select   * from  sys_recyclebin ; 
 classid | reloid | nspname |                object_name                | original_name | type |           droptime            
---------+--------+---------+-------------------------------------------+---------------+------+-------------------------------
    1259 |  48902 | public  | bin$$48902$$2023-07-20 17:46:05.250045+08 | test          | r    | 2023-07-20 17:46:05.250045+08
(1 row)

test=#  purge table  test ;    
PURGE
test=#  select   * from  sys_recyclebin ; 
 classid | reloid | nspname | object_name | original_name | type | droptime 
---------+--------+---------+-------------+---------------+------+----------
(0 rows)清理整个回收站test=#  purge recyclebin;
最后修改时间:2023-07-21 09:54:42
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论