闪回概念是 Oracle 最先提出来的,其本质是为了回退错误操作产生的,避免人为的“灾难”,并且要能够快速回退。
闪回恢复功能是数据库恢复技术的一环,可以有选择性的撤销一个已提交事务的影响,将数据从人为不正确的操作中进行恢复。在采用闪回技术之前,只能通过备份恢复、PITR 等手段找回已提交的数据库修改,恢复时长需要数分钟甚至数小时。采用闪回技术后,恢复已提交的数据库修改前的数据,只需要秒级,而且恢复时间和数据库大小无关。
openGauss 的闪回分为一下两类:
- 闪回查询
- 闪回表
一、闪回查询
基于 MVCC 多版本的数据恢复:适用于误删除、误更新、误插入数据的查询和恢复,用户通过配置旧版本保留时间,并执行相应的查询或恢复命令,查询或恢复到指定的时间点或 CSN 点。
前提条件(下面三个缺一不可)
undo_retention_time:参数用于设置 undo 旧版本的保留时间。
undo_zone_count=16384 ---代表的时候 undo log 的一种资源个数
enable_default_ustore_table=on --默认指定用户创建表时使用 USTORE 存储引擎。
存储引擎:Ustore Ustore 存储引擎将最新版本的“有效数据”和历史版本的“垃圾数据”分离存储。将最新版本的“有效数据”存储在数据页面上,并单独开辟一段 UNDO 空间,用于统一管理历史版本的“垃圾数据”,因此数据空间不会由于频繁更新而膨胀,“垃圾数据”集中回收效率更高。
设置参数命令如下:
gs_guc set -N all -I all -c "undo_retention_time=2000s"
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_om -t restart
重启后验证参数:
openGauss=# show undo_retention_time;
undo_retention_time
---
2000s
(1 row)
openGauss=# show undo_zone_count;
undo_zone_count
---
16384
(1 row)
openGauss=# show enable_default_ustore_table;
enable_default_ustore_table
---
on
(1 row)
openGauss=#
下面开始演示:
创建表:
openGauss=# CREATE TABLE flashback_tab(
id int not null,
name text not null);
openGauss-#
CREATE TABLE
查看表存储引擎:
openGauss=# \d+ flashback_tab;
Table "public.flashback_tab"
Column | Type | Modifiers | Storage | Stats target | Description
--------+---------+-----------+----------+--------------+-------------
id | integer | not null | plain | |
name | text | not null | extended | |
Has OIDs: no
Options: orientation=row, compression=no, storage_type=USTORE, toast.storage_type=USTORE
插入数据:
insert into flashback_tab values (1,'ybj');
查询当前日期;
openGauss=# select current_timestamp;
pg_systimestamp
---
2022-09-29 01:13:32.691158+08
(1 row)
查询数据:
openGauss=# select \* from flashback_tab;
id | name
----+------
1 | ybj
(1 row)
插入数据:
openGauss=# insert into flashback_tab values (2,'yangkai');
INSERT 0 1
查询结果:
openGauss=# select \* from flashback_tab;
id | name
----+---------
1 | ybj
2 | yangkai
(2 rows)
---基于 timestamp 的闪回查询:
openGauss=# SELECT \* FROM flashback_tab TIMECAPSULE TIMESTAMP to_timestamp ('2022-09-29 01:13:32.691158', 'YYYY-MM-DD HH24:MI:SS.FF');
id | name
----+------
1 | ybj
(1 row)
---查询 timestamp 对应的 CSN
openGauss=# select snptime,snpcsn from gs_txn_snapshot where snptime between '2022-09-29 01:13:32.691158' and ' 2022-09-29 01:15:24.921426'
openGauss-# ;
snptime | snpcsn
-------------------------------+--------
2022-09-29 01:13:32.841985+08 | 2112
2022-09-29 01:13:35.87922+08 | 2114
2022-09-29 01:13:38.924031+08 | 2116
2022-09-29 01:13:41.966247+08 | 2118
2022-09-29 01:13:45.013022+08 | 2120
2022-09-29 01:13:48.04741+08 | 2122
2022-09-29 01:13:51.078498+08 | 2124
2022-09-29 01:13:54.101686+08 | 2126
2022-09-29 01:13:57.123891+08 | 2128
2022-09-29 01:14:00.147156+08 | 2130
2022-09-29 01:14:03.169433+08 | 2132
2022-09-29 01:14:06.192879+08 | 2134
2022-09-29 01:14:09.216963+08 | 2136
2022-09-29 01:14:12.240249+08 | 2138
2022-09-29 01:14:15.26606+08 | 2140
2022-09-29 01:14:18.288409+08 | 2142
2022-09-29 01:14:21.309986+08 | 2144
2022-09-29 01:14:24.332801+08 | 2146
2022-09-29 01:14:27.378095+08 | 2148
2022-09-29 01:14:30.416234+08 | 2151
2022-09-29 01:14:33.460251+08 | 2153
2022-09-29 01:14:36.508431+08 | 2155
---基于 CSN 的闪回查询
openGauss=# SELECT \* FROM flashback_tab TIMECAPSULE CSN 2116;
id | name
----+------
1 | ybj
(1 row)
二、闪回表
基于类似 windows 系统回收站的恢复:适用于误 DROP、误 TRUNCATE 的表的恢复。用户通过配置回收站开关,并执行相应的恢复命令,可以将误 DROP、误 TRUNCATE 的表找回。
前置条件:开启回收站、设置回收站对象保留时间
enable_recyclebin=on 启用回收站。
recyclebin_retention_time=30min 参数用于设置回收站对象保留时间,超过该时间的回收站对象将被自动清理。
命令如下:
gs_guc set -N all -I all -c "enable_recyclebin=on"
gs_guc set -N all -I all -c "recyclebin_retention_time=30min"
设置完重启数据库:
gs_om -t restart
[omm@huaweidb ~]$ gsql -d postgres -p 15400
gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:34 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
openGauss=# show recyclebin_retention_time;
recyclebin_retention_time
---
30min
(1 row)
openGauss=# show enable_recyclebin;
enable_recyclebin
---
on
(1 row)
openGauss=#
下面开始操作演示:
[omm@huaweidb ~]$ gsql -d postgres -p 15400
gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:34 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
--查询当前表
openGauss=# select _ from flashback_tab ;
id | name
----+---------
1 | ybj
2 | yangkai
(2 rows)
--truncate 表
openGauss=# truncate flashback_tab;
TRUNCATE TABLE
openGauss=# select _ from flashback_tab ;
id | name
----+------
(0 rows)
---闪回表
openGauss=# timecapsule table flashback_tab to before truncate;
TimeCapsule Table
--查询结果
openGauss=# select _ from flashback_tab ;
id | name
----+---------
1 | ybj
2 | yangkai
(2 rows)
--drop 表
openGauss=# drop table flashback_tab;
DROP TABLE
openGauss=# select _ from flashback_tab;
ERROR: relation "flashback_tab" does not exist on dn_6001
LINE 1: select \* from flashback_tab;
^
--查看回收站:
openGauss=# sELECT rcyname,rcyoriginname,rcytablespace FROM GS_RECYCLEBIN;
rcyname | rcyoriginname | rcytablespace
------------------------------+----------------------+---------------
BIN$3C7C4EB8014$30BE34C8==$0 | pg_toast_32783_index | 0
BIN$3C7C4EB8012$30BE3AA0==$0 | pg_toast_32783 | 0
BIN$3C7C4EB800F$30BE40B0==$0 | flashback_tab | 0
BIN$3C7C4EB800F$30BEA658==$0 | flashback_tab | 0
BIN$3C7C4EB8014$30BEAF78==$0 | pg_toast_32783_index | 0
BIN$3C7C4EB8012$30BEB560==$0 | pg_toast_32783 | 0
(6 rows)
--通过回收站闪回表并命名 flashback_yangkai;
openGauss=# timecapsule table flashback_tab to before drop rename to flashback_yangkai;
TimeCapsule Table
openGauss=# select \* from flashback_yangkai;
id | name
----+---------
1 | ybj
2 | yangkai
(2 rows)
三、总结
openGauss 闪回非常强大,可以秒杀国产大部分数据库,基本可以满足日常运维需求,希望后期可以推出类似 oracle 数据库级别闪回、snapshot standby 就完美了。
操作之前也应该优先使用 ls 命令进行检查
测试卸载
之前已安装过 MogDB 集群,先测试卸载
注意:卸载之前先需要关闭集群服务
# /opt/ptk/ptk uninstall -n mymogdb
=============================
global:
cluster_name: mymogdb
user: omm
group: dbgrp
app_dir: /opt/mogdb301/app
data_dir: /opt/mogdb301/data
log_dir: /opt/mogdb301/log
tool_dir: /opt/mogdb301/tool
tmp_dir: /opt/mogdb301/tmp
cm_server_port: 15300
db_servers:
- host: ***.***.***.*** db_port: 3000
role: primary
az_name: AZ1
az_priority: 1
- host: ***.***.***.*** db_port: 3000
role: standby
az_name: AZ1
az_priority: 1
=============================
Do you really want to uninstall this cluster? Please confirm carefully[Y|Yes](default=N)
根据操作交互提示进行操作,即可顺利完成卸载。
测试安装
安装一主一备
checkos
[root@mogdb1 ~]# /opt/ptk/ptk checkos
...
# Check Results
Item | Level
------------------------------------+-----------
A1.Check_OS_Version | OK
A2.Check_Kernel_Version | OK
A3.Check_Unicode | OK
A4.Check_TimeZone | OK
A5.Check_Swap_Memory_Configure | Warning
A6.Check_SysCtl_Parameter | OK
A7.Check_FileSystem_Configure | OK
A8.Check_Disk_Configure | OK
A9.Check_BlockDev_Configure | OK
A9.Check_Logical_Block | OK
A10.Check_IO_Request | Warning
A10.Check_Asynchronous_IO_Request | OK
A10.Check_IO_Configure | OK
A11.Check_Network_Configure | OK
A12.Check_Time_Consistency | OK
A13.Check_Firewall_Service | OK
A14.Check_THP_Service | OK
A15.Check_Dependent_Package | OK
A16.Check_CPU_Instruction_Set | Abnormal
A17.Check_Port | OK
Total count 20, abnormal count 1, warning count 2
Failed to check os, can’t perform installation unless fix all the abnormal items
You can use 'ptk checkos -i ITEM --detail' to see detail message
Please check root_fix_os.[TIMESTAMP].sh for commands to resolve.
生产环境需要根据 fix 脚本把 Warning 和 Abnormal 项修复成功之后,再进行安装。
本文虚拟机暂时不执行操作系统参数 fix 的脚本,重点关注 A16 项:
# /opt/ptk/ptk checkos -i A16 --detail
...
...
# Check Results
Item | Level | Message
--------------------------------+----------+-----------------------------------------------------------------------
A16.Check_CPU_Instruction_Set | Abnormal | [***.***.***.***] [PTK-508001] not found cpu instruction set: [bmi2]
Total count 1, abnormal count 1, warning count 0
Failed to check os, can’t perform installation unless fix all the abnormal items
You can use 'ptk checkos -i ITEM --detail' to see detail message
Please check root_fix_os.[TIMESTAMP].sh for commands to resolve.
提示是缺 bmi2 指令集,bmi2 是做位操作的 CPU 指令集,需要安装。
生产环境对 checkos 检查发现的问题应该逐个修复再进行安装,本文主要演示 PTK 功能,下面安装时通过参数临时忽略。
生成模板配置文件
如果已有模板配置文件,可以直接使用
/opt/ptk/ptk template \
--base-dir=/opt/mogdb301 \
--cluster-name=mymogdb \
--user=omm \
--group=dbgrp \
--port=3000 \
−−cluster > config.yaml
对 config.yaml 可以进行敏感内容加密,比如数据库初始用户密码、SSH 密码等。
# /opt/ptk/ptk encrypt admin
admin: pTk6Y2Q4MzNmYmQ8RD1FPTxAPTgzSDBzTGdrY1VuVmVhd0s0LUhrTzNyNW5qNFhKaTFRNDI4RnN5VW52YWM=
编辑好的配置文件 config.yaml 如下:
global:
cluster_name: "mymogdb"
user: "omm"
group: "dbgrp"
user_password: "pTk6NDM4Yjk3NjA8RDxCPUQ8RDVYd2VfVDFfNzU3WEtWUkV2YU5YRHFSVlVCZFBwLV8ybkZabFY3VjJUTjA="
db_password: "pTk6MDE1ZmQ3ZTg8RDxCPUM/RVNKUzQtNE10S2h0NGZ3eXRpMXlTWURDWGdVSUtNeDZvRzNwRHk0M09lUEk="
db_port: 3000
cm_server_port: 15300
base_dir: "/opt/mogdb301"
app_dir: "/opt/mogdb301/app"
log_dir: "/opt/mogdb301/log"
data_dir: "/opt/mogdb301/data"
tool_dir: "/opt/mogdb301/tool"
cm_dir: "/opt/mogdb301/cm"
# tmp_dir: "/tmp"
# core_file_dir: ""
ssh_option:
port: 22
user: root
password: "pTk6ZDA2NmFmOTQ8RDxCPUNBP19NSnVKVFV1eFJ6SG5wOElmVC1uS3pqbWNDSGh1bFJzNEZqSHlGQTRuRWs="
conn_timeout: "5s"
exec_timeout: "1m"
db_servers:
- host: "***.***.***.***"
db_port: 3000
ha_port: 3001
role: "primary"
replication_type: 1
gs_initdb_opts:
- "--encoding=UTF-8"
- "--dbcompatibility=PG"
- host: "***.***.***.***"
db_port: 3000
ha_port: 3001
role: "standby"
replication_type: 1
安装
/opt/ptk/ptk install --assumeyes \
--pkg /opt/software/MogDB-3.0.1-CentOS-x86_64.tar.gz \
--skip-check-distro --skip-check-os --skip-create-user \
--file=config.yaml
本文使用–skip-check-distro --skip-check-os 参数做了临时跳过,生产环境应该逐个处理修复。 –skip-create-user 跳过创建用户,因为做了手工创建。
安装过程简略如下:
...
INFO[2022-08-19T10:55:02.420] Time elapsed: 1m45s
cluste_name | host | user | port | stage | status | message
--------------+-----------------+------+------+--------+---------------+----------
mymogdb | ***.***.***.*** | omm | 3000 | launch | start_success | success
| ***.***.***.*** | omm | 3000 | launch | start_success | success
查看状态
安装完查看状态正常
# /opt/ptk/ptk cluster status -n mymogdb
[ Cluster State ]
database_version : MogDB-MogDB
cluster_name : mymogdb
cluster_state : Normal
current_az : AZ_ALL
[ Datanode State ]
id | ip | port | user | instance | db_role | state
-------+-----------------+------+------+----------+---------+---------
6001 | ***.***.***.*** | 3000 | omm | dn_6001 | primary | Normal
6002 | ***.***.***.*** | 3000 | omm | dn_6002 | standby | Normal
四、PTK 扩缩容
扩缩容使用 scale-in、scale-out 命令进行操作,操作前先使用 cluster status 查看确认集群,参考上一步。
测试缩容
对刚才安装的一主一备进行节点删除,删除备节点缩容后将变成单机。
使用 scale-in 指定要删除备机的 IP 进行缩容
# /opt/ptk/ptk cluster -n mymogdb scale-in -H ***.***.***.***...
Would you want delete directory(AppDir,DataDir,ToolDir,LogDir)?[Y|Yes](default=N) y
Would you want clear the env?[Y|Yes](default=N) y
Would you want delete the user?[Y|Yes](default=N) n
...
Scale success.
缩容完查看集群状态已经变为单机
# /opt/ptk/ptk cluster status -n mymogdb
[ Cluster State ]
database_version : MogDB-MogDB
cluster_name : mymogdb
cluster_state : Normal
current_az : AZ_ALL
[ Datanode State ]
id | ip | port | user | instance | db_role | state
-------+-----------------+------+------+----------+---------+---------
6001 | ***.***.***.*** | 3000 | omm | dn_6001 | Normal | Normal
测试扩容
使用 scale-out 接着对单机扩容,增加一个备节点,扩容成一主一备。
scale-out 需要接收一个新增节点配置的文件,使用–gen-template 生产,然后修改为具体的配置
/opt/ptk/ptk cluster -n mymogdb scale-out --gen-template > add.yaml
修改完的 add.yaml 文件内容如下:
- host: ***.***.***.*** db_port: 3000
role: standby
ssh_option:
host: ***.***.***.*** port: 22
user: root
password: "pTk6ZDA2NmFmOTQ8RDxCPUNBP19NSnVKVFV1eFJ6SG5wOElmVC1uS3pqbWNDSGh1bFJzNEZqSHlGQTRuRWs="
使用 scale-out 命令进行缩容
# /opt/ptk/ptk cluster -n mymogdb --skip-check-os --skip-create-user scale-out -c add.yaml
scale [stage=preCheck]
...
Scale success.
注意这里进行扩容时会联网去下载 MogDB 安装包,本文是在离线环境进行,可以在.ptk 目录的 cache 子目录把安装包提前拷贝进去。
# ll /root/.ptk/cache/
total 136004
-rw-r--r-- 1 omm dbgrp 139264406 Aug 6 11:30 MogDB-3.0.1-CentOS-x86_64.tar.gz
拷贝时需要覆盖,可能会存在一个零字节的文件,因为联网下载没有成功。
缩容完检查状态
# /opt/ptk/ptk cluster status -n mymogdb
[ Cluster State ]
database_version : MogDB-MogDB
cluster_name : mymogdb
cluster_state : Normal
current_az : AZ_ALL
[ Datanode State ]
id | ip | port | user | instance | db_role | state
-------+-----------------+------+------+----------+---------+---------
6001 | ***.***.***.*** | 3000 | omm | dn_6001 | primary | Normal
6002 | ***.***.***.*** | 3000 | omm | dn_6002 | standby | Normal
总结
PTK 进行安装、卸载、扩缩容非常便捷,期待下一版本支持数据库升级的功能。




