OpenGauss数据库闪回恢复基本功能
Opengauss数据库的闪回恢复功能,包括闪回查询、闪回表操作(闪回truncate和truncate)
闪回回复
闪回恢复功能是数据库恢复技术的一环,可以有选择性的撤销一个已提交事务的影响,将数据从人为不正确的操作中进行恢复。在采用闪回技术之前,只能通过备份恢复、PITR等手段找回已提交的数据库修改,恢复时长需要数分钟甚至数小时。采用闪回技术后,恢复已提交的数据库修改前的数据,只需要秒级,而且恢复时间和数据库大小无关。
其实各家数据库闪回功能大同小异,差别不太大。
说明:
默认的ASTORE引擎暂不支持闪回功能。创建表时需要指定USTORE引擎。
备机不支持闪回操作
前提条件
- undo_retention_time参数用于设置undo旧版本的保留时间。
- enable_recyclebin参数用于设置回收站的开启和关闭。
alter system set undo_retention_time=3600; 默认单位是秒,即1h。
alter system set enable_recyclebin=on; 开启回收站功能,默认是关闭的。
以上参数是即时生效,无需重启实例或集群。
一、闪回查询
闪回查询可以查询过去某个时间点表的某个snapshot数据,这一特性可用于查看和逻辑重建意外删除或更改的受损数据。闪回查询基于MVCC多版本机制,通过检索查询旧版本,获取指定老版本数据。
示例:
--创建表t5
testdb=> create table t5 (id int,info text) with (storage_type=ustore);
CREATE TABLE
testdb=> insert into t5 select generate_series(1,50),md5(random()::text);
INSERT 0 50
testdb=> select current_timestamp;
pg_systimestamp
-------------------------------
2025-11-03 14:13:38.277513+08
(1 row)
testdb=> select * from t5 timecapsule timestamp to_timestamp('2025-11-03 14:13:33','yyyy-mm-dd hh24:mi:ss');
id | info
----+----------------------------------
1 | 6d97b385ffe370c3e2deb22f71b06c47
2 | 109902c1d0e34cd74a3c21c7ede38507
3 | de1b5a98feec63dc37aaa2fc6a3ea109
4 | 185a1130c4d4d8e85ca42678b56abc67
5 | 06acca84a8fac56f161f1233828db566
…………………………………………………
50 | f30603bbe231a82acc9558278ddad76b
(50 rows)
--delete后闪回查询
testdb=> delete from t5 where id >40;
DELETE 10
testdb=> select * from t5 timecapsule timestamp to_timestamp('2025-11-03 14:14:00','yyyy-mm-dd hh24:mi:ss');
id | info
----+----------------------------------
1 | 6d97b385ffe370c3e2deb22f71b06c47
2 | 109902c1d0e34cd74a3c21c7ede38507
3 | de1b5a98feec63dc37aaa2fc6a3ea109
4 | 185a1130c4d4d8e85ca42678b56abc67
5 | 06acca84a8fac56f161f1233828db566
--update后闪回查询
testdb=> update t5 set info=md5(random()*5::text);
UPDATE 40
testdb=> select * from t5 timecapsule timestamp to_timestamp('2025-11-03 14:19:30','yyyy-mm-dd hh24:mi:ss');
id | info
----+----------------------------------
1 | 6d97b385ffe370c3e2deb22f71b06c47
2 | 109902c1d0e34cd74a3c21c7ede38507
3 | de1b5a98feec63dc37aaa2fc6a3ea109
4 | 185a1130c4d4d8e85ca42678b56abc67
5 | 06acca84a8fac56f161f1233828db566
语法树中“TIMECAPSULE {TIMESTAMP | CSN} expression”为闪回功能新增表达方式,其中TIMECAPSULE表示使用闪回功能,TIMESTAMP以及CSN表示闪回功能使用具体时间点信息或使用CSN(commit sequence number)信息。就是gaussdb这个TIMECAPSULE乍看下不习惯,一般数据库都用flashback。
二、闪回恢复
闪回DROP/TRUNCATE
- 闪回DROP:可以恢复意外删除的表,从回收站(recycle bin)中恢复被删除的表及其附属结构如索引、表约束等。闪回drop是基于回收站机制,通过还原回收站中记录的表的物理文件,实现已drop表的恢复。
- 闪回TRUNCATE:可以恢复误操作或意外被进行truncate的表,从回收站中恢复被truncate的表及索引的物理数据。闪回truncate基于回收站机制,通过还原回收站中记录的表的物理文件,实现已truncate表的恢复。
--闪回被删除的表
testdb=> drop table t5;
DROP TABLE
testdb=> timecapsule table t5 to before drop;
TimeCapsule Table
testdb=> \d
List of relations
Schema | Name | Type | Owner | Storage
--------+------+-------+-------+------------------------------------------------------
public | t3 | table | | {orientation=row,compression=no}
test | t1 | table | test | {orientation=row,compression=no}
test | t11 | table | test | {orientation=row,compression=no}
test | t2 | table | test | {orientation=row,compression=no}
test | t4 | table | test | {orientation=row,compression=no}
test | t5 | table | test | {orientation=row,storage_type=ustore,compression=no}
(6 rows)
--闪回被截断的表
testdb=> truncate table t5;
TRUNCATE TABLE
testdb=> select * from t5;
id | info
----+------
(0 rows)
testdb=> timecapsule table t5 to before truncate;
TimeCapsule Table
testdb=> select * from t5;
id | info
----+----------------------------------
1 | c14f96bda4e2bd1e2dadedaff98ad469
2 | 8b7cd502c4f23b12c04dcec4fb8bc06d
3 | cd1d5bc3cc0f682fd2c4c363442392e0
4 | 67d3c27b21ac22727065968a7cef562f
5 | 4b45db9cd0d483ab0d9ccdcb585cbcf2
……………………………………………………..
39 | ee0907221ca2a3871c50d16d81f7fe28
40 | ca101a65d1511dc47443c11b5dce7575
(40 rows)
三、查看和清理回收站数据
testdb=> select count(*) from t5;
count
-------
50
(1 row)
testdb=> truncate table t5;
TRUNCATE TABLE
testdb=> select * from gs_recyclebin;

testdb=> purge table "BIN$40044EB7264$9E4CBA10==$0";
PURGE TABLE
testdb=> select * from t5;
id | info
----+------
(0 rows)
或者
testdb=> purge table t5;
PURGE TABLE
