• 闪回DROP:可以恢复意外删除的表,从回收站(recycle bin)中恢复被删除的表及其附属结构如索引、表约束等。闪回drop是基于回收站机制,通过还原回收站中记录的表的物理文件,实现已drop表的恢复。

  • 闪回TRUNCATE:可以恢复误操作或意外被进行truncate的表,从回收站中恢复被truncate的表及索引的物理数据。闪回truncate基于回收站机制,通过还原回收站中记录的表的物理文件,实现已truncate表的恢复。

前提条件

开启enable_recyclebin参数,启用回收站。

recyclebin_retention_time参数用于设置回收站对象保留时间,超过该时间的回收站对象将被自动清理,默认900s(15分钟)

模拟测试

测试环境

环境信息如下

操作系统:CentOS7.6 x86_64

数据库版本:5.0

数据库环境:单机

参数配置

查看参数信息

MogDB=# select name,setting,context from pg_settings where name in ('enable_recyclebin','recyclebin_retention_time');
           name            | setting | context 
---------------------------+---------+---------
 enable_recyclebin         | off     | sighup
 recyclebin_retention_time | 900     | sighup
(2 rows)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

修改参数并生效

MogDB=# select name,setting,context from pg_settings where name in ('enable_recyclebin','recyclebin_retention_time');
           name            | setting | context 
---------------------------+---------+---------
 enable_recyclebin         | off     | sighup
 recyclebin_retention_time | 900     | sighup
(2 rows)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

创建测试数据

创建表并插入数据

MogDB=# create table t2 (id int);
CREATE TABLE
MogDB=# insert into t2 select generate_series(1,5);
INSERT 0 5
MogDB=# select * from t2;
 id 
----
  1
  2
  3
  4
  5
(5 rows)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

闪回TRUNCATE

TRUNCATE表

MogDB=# truncate table t2;
TRUNCATE TABLE
MogDB=# select * from t2;
 id 
----
(0 rows)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

闪回TRUNCATE操作

MogDB=# timecapsule table t2 to before truncate;
TimeCapsule Table
MogDB=# select * from t2;
 id 
----
  1
  2
  3
  4
  5
(5 rows)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

闪回DROP

DROP表

MogDB=# drop table t2;
DROP TABLE

  • 1.
  • 2.
  • 3.

查看回收站信息

MogDB=# select * from gs_recyclebin where rcyoperation='d' and rcyoriginname='t2';
-[ RECORD 1 ]--+------------------------------
rcybaseid      | 18008
rcydbid        | 16133
rcyrelid       | 18003
rcyname        | BIN$3F054EB4653$39849300==$0
rcyoriginname  | t2
rcyoperation   | d
rcytype        | 0
rcyrecyclecsn  | 11817
rcyrecycletime | 2023-08-15 18:19:18.240296+08
rcycreatecsn   | 11739
rcychangecsn   | 11739
rcynamespace   | 2200
rcyowner       | 10
rcytablespace  | 0
rcyrelfilenode | 18003
rcycanrestore  | t
rcycanpurge    | t
rcyfrozenxid   | 32536
rcyfrozenxid64 | 32536
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

闪回DROP表并且rename

MogDB=# timecapsule table t2 to before drop rename to new_t2;
TimeCapsule Table
MogDB=# select * from new_t2;
 id 
----
  1
  2
  3
  4
  5
(5 rows)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

DROP表时不放入回收站

MogDB=# drop table new_t2 purge;
DROP TABLE
MogDB=# select rcyname,rcyoriginname,rcytablespace from gs_recyclebin where rcyoperation='d';
 rcyname | rcyoriginname | rcytablespace 
---------+---------------+---------------
(0 rows)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

关于作者

张晓娟,云和恩墨技术顾问,拥有OCM、PGCE等数据库认证,具备运营商、金融等行业的工作经验。