闪回DROP/TRUNCATE
-
闪回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)
- 修改参数并生效
MogDB=# alter system set enable_recyclebin=on;
ALTER SYSTEM SET
MogDB=# SELECT pg_reload_conf();
pg_reload_conf
----------------
t
(1 row)
MogDB=# select name,setting,context from pg_settings where name in ('enable_recyclebin','recyclebin_retention_time');
name | setting | context
---------------------------+---------+---------
enable_recyclebin | on | sighup
recyclebin_retention_time | 900 | sighup
(2 rows)
创建测试数据
- 创建表并插入数据
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)
闪回truncate
- truncate表
MogDB=# truncate table t2;
TRUNCATE TABLE
MogDB=# select * from t2;
id
----
(0 rows)
- 闪回truncate操作
MogDB=# timecapsule table t2 to before truncate;
TimeCapsule Table
MogDB=# select * from t2;
id
----
1
2
3
4
5
(5 rows)
闪回drop
- drop表
MogDB=# drop table t2;
DROP TABLE
- 查看回收站信息
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
- 闪回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)
- 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)
最后修改时间:2023-08-15 18:31:08
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




