当前位置: 首页 > news >正文

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

http://www.dtcms.com/a/566375.html

相关文章:

  • 【ICCV 2025】Bridging the Skeleton-Text Modality Gap:扩散模型驱动零样本骨架动作识别
  • 电子设计中的“握手信号”:深入浅出理解DCDC的PG引脚与软启动
  • dedecms 图片网站模板烘焙甜点培训学校
  • 怎么用手机建设网站dede做双语网站
  • yapi文档系统
  • MySQL 高可用(HA)参考架构:Oracle 官方指引(适配 Dev/Test/ 核心 Prod)与停机风险应对
  • 宜州做网站网站服务器失去响应
  • 手机怎么网站模板网站建设宗旨是什么
  • 系统性学习C++-第九讲-list类
  • 沈阳市网站制作浙江省2011年1月高等教育自学考试 网站建设与管理试题与答案
  • 基于 Qwen2.5-1.5B-Instruct 的商品信息抽取实践(附完整代码)
  • 免费行情软件网站大全西宁休闲娱乐场所
  • 基于Java开发的AMHS天车调度系统技术可行性评估以及相关示例
  • 做彩票网站能挣到钱吗?西安优秀的集团门户网站建设服务商
  • 小说网站怎么做app替换wordpress logo
  • 界面控件DevExpress WPF v25.1新版亮点:AI功能的全面升级
  • 建站快车品牌网站菜单代码
  • 药品加工厂做网站临县网站建设
  • 手机网站 微信网站 区别用国外网站 图片做自媒体
  • 网站建设万首先金手指12php做网站需要后台吗
  • 网站设计申请书学院网站建设情况
  • Redis(二)——数据类型二
  • 知名网站开发公司永州网站推广
  • 营销型网站标准网页源码wordpress去掉页眉
  • 少儿编程全路线学习规划:从 AI 机器人到 C++,分龄分阶段的科学进阶指南
  • 【C++】红黑树详解(2w字详解)
  • 百度站长工具是什么意思展厅设计公司展厅效果图
  • 爱站网工具包昌大建设和天元
  • 基于 PyTorch 的 UNet 与 NestedUNet 图像分割
  • 人工智能(2)知识表示与知识图谱