📢📢📢📣📣📣
哈喽!大家好,我是【IT邦德】,江湖人称jeames007,10余年DBA及大数据工作经验
一位上进心十足的【大数据领域博主】!😜😜😜
中国DBA联盟(ACDU)成员,目前服务于工业互联网
擅长主流Oracle、MySQL、PG、高斯及GP 运维开发,备份恢复,安装迁移,性能优化、故障应急处理等。
✨ 如果有对【数据库】感兴趣的【小可爱】,欢迎关注【IT邦德】💞💞💞
❤️❤️❤️感谢各位大可爱小可爱!❤️❤️❤️
文章目录
- 前言
- 📣 1.闪回概述
- 📣 2.参数设置
- 📣 3.回收站的恢复
- ✨ 3.1 TRUNCATE 恢复
- ✨ 3.2 TRUNCATE 恢复
- 📣4.MVCC数据恢复
- 📣 5.技能总结
前言
本篇结合企业实战介绍了openGauss闪回技术。📣 1.闪回概述
openGauss闪回功能能够有选择性的高效撤销一个已提交事务的影响,从人为错误中恢复。
在采用闪回技术之前,只能通过备份恢复、PITR等手段找回已提交的数据库修改,恢复时长需要数分钟甚至数小时。
采用闪回技术后,恢复已提交的数据库修改前的数据,只需要秒级,而且恢复时间和数据库大小无关。
1)误删除表的场景;
2)需要将表中的数据恢复到指定时间点或者CSN
闪回支持两种恢复模式:
1.基于MVCC多版本的数据恢复:适用于误删除、误更新、误插入数据的查询和恢复,用户通过配置旧版本保留时间,
并执行相应的查询或恢复命令,查询或恢复到指定的时间点或CSN点。
2.基于类似windows系统回收站的恢复:适用于误DROP、误TRUNCATE的表的恢复。用户通过配置回收站开关,并执行相应的恢复命令,
可以将误DROP、误TRUNCATE的表找回。
📣 2.参数设置
## 参数说明:
1.启用回收站参数enable_recyclebin
2.设置回收站对象保留时间recyclebin_retention_time,超过该时间的回收站对象将被自动清理。单位为s,最小值为0,最大值为2147483647。
3.设置内存中可分配的undo zone数量undo_zone_count,0代表禁用undo和Ustore表,建议取值为max_connections*4
openGauss=# select name,setting,min_val,max_val from pg_settings where name like 'enable_recyclebin';
name | setting | min_val | max_val
-------------------+---------+---------+---------
enable_recyclebin | off | |
openGauss=# select name,setting,min_val,max_val from pg_settings where name like 'recyclebin_retention_time';
name | setting | min_val | max_val
---------------------------+---------+---------+------------
recyclebin_retention_time | 900 | 1 | 2147483647
openGauss=# select name,setting,min_val,max_val from pg_settings where name like 'undo_zone_count';
name | setting | min_val | max_val
-----------------+---------+---------+---------
undo_zone_count | 0 | 0 | 1048576
select name,setting,min_val,max_val from pg_settings where name like 'max_connections';
name | setting | min_val | max_val
-----------------+---------+---------+---------
max_connections | 5000 | 10 | 262143
## 何设置参数
gs_guc set -N all -I all -c "undo_zone_count=20000"
gs_guc set -N all -I all -c "enable_recyclebin=on"
gs_guc set -N all -I all -c "recyclebin_retention_time=1440min"
gs_om -t restart
📣 3.回收站的恢复
回收站的方式有两种:
1.闪回TRUNCATE:可以恢复误操作或意外被进行truncate的表,从回收站中恢复被truncate的表及索引的物理数据。
闪回truncate基于回收站机制,通过还原回收站中记录的表的物理文件,实现已truncate表的恢复。
2.闪回DROP:可以恢复意外删除的表,从回收站(recyclebin)中恢复被删除的表及其附属结构如索引、表约束等。
闪回drop是基于回收站机制,通过还原回收站中记录的表的物理文件,实现已drop表的恢复。
✨ 3.1 TRUNCATE 恢复
openGauss=# \c
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "postgres" as user "omm".
openGauss=# CREATE DATABASE mydb WITH ENCODING 'GBK' template = template0;
openGauss=# CREATE USER mesdb PASSWORD 'Gauss@123';
openGauss=# GRANT ALL PRIVILEGES TO mesdb;
openGauss=# \du
openGauss=# \c mydb mesdb
Password for user mesdb:
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "mydb" as user "mesdb".
密码:Gauss@123
1.建表、插入测试数据
create SCHEMA tpcds;
DROP TABLE IF EXISTS tpcds.reason;
CREATE TABLE tpcds.reason
(
r_reason_sk integer,
r_reason_id character(16),
r_reason_desc character(100)
) with(STORAGE_TYPE=USTORE);
INSERT INTO tpcds.reason VALUES (1, 'AA', 'reason1'),(2, 'AB', 'reason2'),(3, 'AC', 'reason3');
select * from tpcds.reason;
说明:
Ustore存储引擎将最新版本的“有效数据”和历史版本的“垃圾数据”分离存储。将最新版本的“有效数据”存储在数据页面上,并单独开辟一段UNDO空间,用于统一管理历史版本的“垃圾数据”,因此数据空间不会由于频繁更新而膨胀,“垃圾数据”集中回收效率更高。
Ustore存储引擎结合UNDO空间,可以实现更高效、更全面的闪回查询和回收站机制,能快速回退人为“误操作”,为openGauss提供了更丰富的企业级功能。
数据库启动之前,在postgresql.conf中设置“enable_default_ustore_table=on”,默认指定用户创建表时使用USTORE存储引擎。
select name,setting,min_val,max_val from pg_settings where name like 'enable_default_ustore_table';
如果有以下报错,由于openGauss建表默认为astore模式,是不支持闪回的。所以,现在需要修改建表脚本为ustore模式才可以闪回。
mydb=> TIMECAPSULE TABLE tpcds.reason1 to BEFORE TRUNCATE;
ERROR: timecapsule does not support astore yet
2.误操作TRUNCATE
mydb=> TRUNCATE TABLE tpcds.reason;
mydb=> select * from tpcds.reason;
r_reason_sk | r_reason_id | r_reason_desc
-------------+-------------+---------------
(0 rows)
3.闪回数据
mydb=> TIMECAPSULE TABLE tpcds.reason to BEFORE TRUNCATE;
TimeCapsule Table
mydb=> select * from tpcds.reason;
r_reason_sk | r_reason_id | r_reason_desc
-------------+------------------+--------------
1 | AA | reason1
2 | AB | reason2
3 | AC | reason3
说明:以上数据已经被闪回了
✨ 3.2 TRUNCATE 恢复
1.误操作DROP
mydb=> DROP TABLE tpcds.reason;
mydb=> select * from tpcds.reason;
ERROR: relation "tpcds.reason" does not exist on dn_6001_6002
2.闪回
mydb=> TIMECAPSULE TABLE tpcds.reason to BEFORE DROP;
TimeCapsule Table
mydb=> select * from tpcds.reason;
r_reason_sk | r_reason_id | r_reason_desc
-------------+------------------+--------------
1 | AA | reason1
2 | AB | reason2
3 | AC | reason3
说明:以上数据已经被闪回了
📣4.MVCC数据恢复
1.建表
drop table if EXISTS tpcds.time_table;
create table tpcds.time_table(idx integer, snaptime timestamp, snapcsn bigint, timeDesc character(100)) with(STORAGE_TYPE=USTORE);
INSERT INTO tpcds.time_table select 1, now(),int8in(xidout(next_csn)), 'time1' from gs_get_next_xid_csn();
INSERT INTO tpcds.time_table select 2, now(),int8in(xidout(next_csn)), 'time2' from gs_get_next_xid_csn();
--出去上个厕所再回来
INSERT INTO tpcds.time_table select 3, now(),int8in(xidout(next_csn)), 'time3' from gs_get_next_xid_csn();
--去买了包烟
INSERT INTO tpcds.time_table select 4, now(),int8in(xidout(next_csn)), 'time4' from gs_get_next_xid_csn();
--刷了会视频
INSERT INTO tpcds.time_table select 5, now(),int8in(xidout(next_csn)), 'time5' from gs_get_next_xid_csn();
mydb=> select * from tpcds.time_table;
idx | snaptime | snapcsn | timedesc
-----+----------------------------+---------+----------------------------------------
1 | 2023-04-09 09:37:10.415875 | 2124 | time1
2 | 2023-04-09 09:37:15.374307 | 2127 | time2
3 | 2023-04-09 09:41:49.92012 | 2311 | time3
4 | 2023-04-09 09:41:55.351731 | 2316 | time4
5 | 2023-04-09 09:41:59.390929 | 2319 | time5
2.设置参数
mydb=> show undo_retention_time;
undo_retention_time
---------------------
0
undo_retention_time --参数用于设置undo旧版本的保留时间。
undo_zone_count=16384 --代表的时候undo log的一种资源个数
enable_default_ustore_table=on --默认指定用户创建表时使用USTORE存储引擎
gs_guc set -N all -I all -c "undo_retention_time=2000s"
gs_guc set -N all -I all -c "undo_retention_time=1440min"
gs_guc set -N all -I all -c "undo_zone_count=16384"
gs_guc set -N all -I all -c "enable_default_ustore_table=on"
gs_guc set -N all -I all -c "undo_retention_time=1440min"
gs_ctl reload -D /opt/openGauss/data/dn --加载参数
3.误删除数据
mydb=> delete tpcds.time_table;
4.根据时间点闪回
SELECT * FROM tpcds.time_table TIMECAPSULE TIMESTAMP to_timestamp('2023-04-09 09:41:49.92012','YYYY-MM-DD HH24:MI:SS.FF');
idx | snaptime | snapcsn | timedesc
-----+----------------------------+---------+---------
1 | 2023-04-09 09:37:10.415875 | 2124 | time1
2 | 2023-04-09 09:37:15.374307 | 2127 | time2
5.基于SCN闪回
SELECT * FROM tpcds.time_table TIMECAPSULE CSN 2316;
idx | snaptime | snapcsn | timedesc
-----+----------------------------+---------+---------
1 | 2023-04-09 09:37:10.415875 | 2124 | time1
2 | 2023-04-09 09:37:15.374307 | 2127 | time2
3 | 2023-04-09 09:41:49.92012 | 2311 | time3
📣 5.技能总结
##清空回收站
mydb=> purge recyclebin;
## ustore VS astore
现在反思下我遇到的两个问题,都是因为表的存储方式为astore,而闪回特性不支持该种类型导致的,建表时指定为ustore就可以解决。so,有没有办法默认建表时就是ustore存储呢?继续翻看产品文档,是有参数可以设置的。
gs_guc set -N all -I all -c "enable_default_ustore_table=on" --设置参数
gs_ctl reload -D /opt/openGauss/data/dn --加载参数
astore:
openGauss内核当前使用的行引擎采用的是Append Update(追加更新)模式,该模式在INSERT、DELETE、HOT UPDATE(页面内更新)的场景下有较好的表现。主要面向通用的在线交易处理类业务应用场景,适合高并发、小数据量的单点或小范围数据读、写操作。astore为行存储格式,向上提供元组形式的读、写;向下以页面为单位通过可扩展的介质管理器对存储介质进行读、写操作;并通过页面粒度的共享缓冲区来优化读、写操作的效率。
因此,astore存储格式比较适合频繁插入、少量更新的业务场景.
ustore:
ustore属于In-place Update更新模式,中文意思为:原地更新,是openGauss内核新增的一种存储模式。astore对于非HOT UPDATE场景,垃圾回收不够高效。ustore存储模式提供“原地更新”能力,主要思路是将最新版本的“有效数据”和历史版本的“垃圾数据”分离存储。将最新版本的“有效数据”存储在数据页面上,而单独开辟一段undo(回滚)空间,用于统一管理历史版本的“垃圾数据”,因此数据空间不会由于频繁更新而膨胀,垃圾回收效率更高。通过NUMA-aware的undo子系统设计,使得undo子系统在多核平台上高效扩展。同时通过对元组和数据页面结构的重新设计,减少存储空间的占用。采用多版本索引技术,解决索引膨胀问题,彻底去除autovacuum(垃圾清理线程)机制,提升存储空间的回收复用效率。当前USTORE存储引擎不支持极致RTO回放模式。对于主机,在recovery_parse_workers参数设置大于1的情况下,创建USTORE存储引擎的表将返回报错;对于备机,如果数据库中已经包含USTORE表,那么后续如果再打开极致RTO功能,可能会导致回放失败和报错,严重情况下甚至可能导致备机数据损坏(这种情况下需要执行备机重建进行修复)。
## 其他设置
mydb=> show server_version;
server_version
----------------
9.2.4
mydb=> \copyright
GaussDB Kernel Database Management System
Copyright (c) Huawei Technologies Co., Ltd. 2018. All rights reserved.
mydb=> \conninfo
You are connected to database "mydb" as user "mesdb" via socket in "/opt/openGauss/tmp" at port "15400".
mydb=> show AUTOCOMMIT;
autocommit
------------
on