1.使用场景
南大通用GBase 8c支持闪回功能,支持表级和库级。库级闪回将数据库中的表恢复至特定时间点,当逻辑损坏仅限于一组表,可以快速恢复表的数据。数据库级的闪回基于表的闪回,基于MVCC多版本机制,通过删除指定时间点和该时间点之后的增量数据,并找回指定时间点和当前时间点删除的数据,实现表级数据还原。
本文对数据库级闪回进行说明,并以GBase 8c V5 5.0.0版本举例说明。
2.数据库闪回语法
- 数据库闪回到指定时间点
timecapsule database to timestamp expression;
- 数据库闪回到指定CSN
timecapsule database to csn expression;
3.库级闪回开关参数
库级闪回由enable_timecapsule_database参数控制,控制是否开启数据库闪回功能。
修改方法:可以在postgresql.conf配置文件中修改,或者通过gs_guc reload -c “enable_timecapsule_database=参数值”命令修改。
取值范围:
- ON / TRUE:开启库级闪回功能
- OFF / FALSE(默认值):关闭库级闪回功能
4.大致原理
数据库级闪回基于表的闪回实现,将数据库级的闪回操作转化为表的闪回操作。
在库级闪回功能使能的情况下,执行过dml操作的数据表,被记录在回收站中,操作类型为'i'(insert),'u'(update),'D' (delete),时间戳为进行dml操作时的时间。对于同一张表,每次执行dml操作时,更新操作类型和时间戳。
执行数据库闪回时,遍历回收站中所有dml操作类型的数据,逐个执行表闪回,同时刷新回收站中对应元组的时间戳。
回收站中dml操作的表的存活时间为undo_retention_time(元组旧版本的保留时间)+15min(快照保留时间),超时后,无法找到对应时间的快照,无法闪回,回收站中对应数据被删除。
5.使用限制
- undo_retention_time用于设置undo旧版本的保留时间,只能闪回undo_retention_time+快照保留时间之内的数据;
- 闪回的旧版本数据不能被vacuumn掉;
- 仅支持数据库内普通表的dml操作的闪回;
- 仅支持ustore表;
- 在闪回时间点后,执行了drop或truncate操作,不能被闪回;
- 不支持删库闪回:
- 不支持表结构修改后的闪回
6.使用示例
【前置条件】
(1)开启库级闪回功能,设置enable_timecapsule_database为on
gs_guc set -N all -I all -c "enable_timecapsule_database=on"
(2)开启回收站
gs_guc set -N all -I all -c "enable_recyclebin=on"
(3)设置undo旧版本的保留时间>0
gs_guc set -N all -I all -c "undo_retention_time=600s"
(4)重启数据库,使配置生效
gs_om -t restart
【验证步骤】
--步骤1:创建测试数据
create table recover_ustore_table01(id int,name char(120),age int,name2 text) with (storage_type=ustore);
insert into recover_ustore_table01 select generate_series(1,5),'gbase 8c',99,'recover_ustore_table01';
create table recover_ustore_table02(id int,name char(120),age int,name2 text) with (storage_type=ustore);
insert into recover_ustore_table02 select generate_series(1,5),'gbase 8c',99,'recover_ustore_table01';
--步骤2:查询并记录测试表数据,等待10s后,记录闪回点 point1
select * from recover_ustore_table01 order by 1;
select * from recover_ustore_table02 order by 1;
select now();
执行记录及返回信息如下:
--步骤3:执行DML操作
update recover_ustore_table01 set age =6;
update recover_ustore_table02 set age =6;
insert into recover_ustore_table01 select generate_series(88,88),'gbase 8c',88,'88';
insert into recover_ustore_table02 select generate_series(88,88),'gbase 8c',88,'88';
delete from recover_ustore_table01 where id <5;
delete from recover_ustore_table02 where id <5;
--步骤4:查询并记录测试表数据
select * from recover_ustore_table01 order by 1;
select * from recover_ustore_table02 order by 1;
执行记录及返回信息如下:
--步骤5:执行数据库闪回到指定时间点,闪回到point1 --'2024-08-08 10:17:06.25'为步骤2查询出的时间
Timecapsule database to timestamp to_timestamp('2024-08-08 10:17:06.25', 'YYYY-MM-DD HH24:MI:SS:FF');
--步骤6:查询并记录测试表数据,等待10s后,记录闪回点 point2 --可以查询出两张表的数据被闪回成功
select * from recover_ustore_table01 order by 1;
select * from recover_ustore_table02 order by 1;
select int8in(xidout(next_csn)) from gs_get_next_xid_csn() order by 1 limit 1;
执行记录及返回信息如下:
--步骤7:执行DML操作
update recover_ustore_table01 set age =6;
update recover_ustore_table02 set age =6;
insert into recover_ustore_table01 select generate_series(88,88),'gbase 8c',88,'88';
insert into recover_ustore_table02 select generate_series(88,88),'gbase 8c',88,'88';
delete from recover_ustore_table01 where id <5;
delete from recover_ustore_table02 where id <5;
--步骤8:查询并记录测试表数据
select * from recover_ustore_table01 order by 1;
select * from recover_ustore_table02 order by 1;
执行记录及返回信息如下:
--步骤9:执行数据库闪回到指定CSN,闪回到point2 -- 22580' 为步骤6查询出来的csn值
timecapsule database to csn '22580';
--步骤10:验证闪回结果 --可以查询出两张表的数据被闪回成功
select * from recover_ustore_table01 order by 1;
select * from recover_ustore_table02 order by 1;
执行记录及返回信息如下:
欢迎大家共同探索,下次尝试表级闪回的使用。