一、实验环境
- Virtualbox:一台虚拟机
- 操作系统:openEuler20.03 LTS
- 数据库版本:5.0.0 企业版
二、相关知识点
关闭模式有两种:
- 快速(fast):快速关闭数据库,断开客户端的连接,让当前未提交事务回滚,然后正常关闭数据库。
- 立即(immediate):立即关闭数据库,立即停止数据库进程,直接退出;
下次启动时会进行数据库恢复
。
三、实验过程
-
启动数据库系统
-
通过gsql登录postgres数据库
-
创建测试表t3
openGauss=# CREATE TABLE t3 (id INT, name VARCHAR(50));
-
开启显示事务并执行insert语句,但未提交事务
openGauss=# begin; BEGIN openGauss=# insert into t3 values(1, 'test'); INSERT 0 1 openGauss=#
-
打开新的SSH会话窗口以fast模式关闭数据库
gs_om -t stop -m fast
-
查看数据库运行日志变化情况
2024-03-25 20:54:14.625 66017163.1 [unknown] 140545200476096 [unknown] 0 dn_6001 00000 0 [BACKEND] LOG: received fast shutdown request 2024-03-25 20:54:14.643 66017164.6124 [unknown] 140541970036480 AutoVacLauncher 0 dn_6001 00000 0 [BACKEND] LOG: autovacuum launcher shutting down 2024-03-25 20:54:14.647 66017164.5080 postgres 140541944805120 JobScheduler 0 dn_6001 00000 0 [BACKEND] LOG: job scheduler is shutting down 2024-03-25 20:54:14.657 66017164.6127 [unknown] 140541538858752 dn_6001 0 dn_6001 00000 0 [BACKEND] LOG: shutting down 2024-03-25 20:54:14.746 66017164.6127 [unknown] 140541538858752 dn_6001 0 dn_6001 00000 0 [BACKEND] LOG: will do full checkpoint, need flush 0 pages. 2024-03-25 20:54:14.746 66017164.6127 [unknown] 140541538858752 dn_6001 0 dn_6001 00000 0 [SLRU] LOG: remove old segments(<0) under pg_csnlog 2024-03-25 20:54:15.017 66017164.6132 [unknown] 140542053943040 dn_6001 0 dn_6001 00000 0 [INCRE_CKPT] LOG: pagewriter thread shut down, id is 2 2024-03-25 20:54:15.020 66017164.6125 [unknown] 140542087505664 dn_6001 0 dn_6001 00000 0 [INCRE_CKPT] LOG: pagewriter thread shut down, id is 0 2024-03-25 20:54:15.023 66017164.6127 [unknown] 140541538858752 dn_6001 0 dn_6001 00000 0 [UNDO] LOG: [CheckPointUndoSystemMeta:355]undo metadata checkPointRedo = 38073248, oldestXmin = 19961, recycleXmin = 19961, globalFrozenXid = 0, globalRecycleXid = 13845. 2024-03-25 20:54:15.026 66017164.6127 [unknown] 140541538858752 dn_6001 0 dn_6001 00000 0 [BACKEND] LOG: keep all the xlog segments, because current segno = 2, less than wal_keep_segments = 16 2024-03-25 20:54:15.026 66017164.6127 [unknown] 140541538858752 dn_6001 0 dn_6001 01000 0 [BACKEND] WARNING: replicationSlotMinLSN is InvalidXLogRecPtr!!! 2024-03-25 20:54:15.026 66017164.6127 [unknown] 140541538858752 dn_6001 0 dn_6001 01000 0 [BACKEND] WARNING: replicationSlotMaxLSN is InvalidXLogRecPtr!!! 2024-03-25 20:54:15.027 66017164.6127 [unknown] 140541538858752 dn_6001 0 dn_6001 00000 0 [BACKEND] LOG: CreateCheckPoint PrintCkpXctlControlFile: [checkPoint] oldCkpLoc:0/244D808, oldRedo:0/244D788, newCkpLoc:0/244F3A0, newRedo:0/244F3A0, preCkpLoc:0/244D6E8 2024-03-25 20:54:15.027 66017164.6127 [unknown] 140541538858752 dn_6001 0 dn_6001 00000 0 [BACKEND] LOG: will update control file (create checkpoint), shutdown:1 2024-03-25 20:54:15.028 66017164.6127 [unknown] 140541538858752 dn_6001 0 dn_6001 00000 0 [BACKEND] LOG: attempting to remove WAL segments older than log file 000000010000000000000000 2024-03-25 20:54:15.029 66017164.6127 [unknown] 140541538858752 dn_6001 0 dn_6001 00000 0 [DBL_WRT] LOG: Double write exit 2024-03-25 20:54:15.029 66017164.6127 [unknown] 140541538858752 dn_6001 0 dn_6001 00000 0 [DBL_WRT] LOG: Double write exit 2024-03-25 20:54:15.029 66017164.6127 [unknown] 140541538858752 dn_6001 0 dn_6001 00000 0 [BACKEND] LOG: database system is shut down 2024-03-25 20:54:15.30 [postmaster][reaper][140545200476096] LOG: checkpoint thread exit and nowait for sharestorage 2024-03-25 20:54:15.118 66017163.1 [unknown] 140545200476096 [unknown] 0 dn_6001 00000 0 [BACKEND] LOG: FiniNuma allocIndex: 0. 2024-03-25 20:54:15.118 66017163.1 [unknown] 140545200476096 [unknown] 0 dn_6001 00000 0 [BACKEND] LOG: Gaussdb exit(0)
-
重启数据库,查看t3表数据及当前xlog插入点及LSN
[omm@standalone ~]$ gs_om -t start [omm@standalone ~]$ gsql -d postgres -p 26000 -r openGauss=# select * from t3; id | name ----+------ (0 rows) openGauss=# select pg_current_xlog_insert_location(); pg_current_xlog_insert_location --------------------------------- 0/244F8C0 (1 row) openGauss=# select pg_xlogfile_name('0/244F8C0'); pg_xlogfile_name -------------------------- 000000010000000000000002 (1 row) openGauss=# select pg_xlogfile_name_offset('0/244F8C0'); pg_xlogfile_name_offset ------------------------------------ (000000010000000000000002,4520128) (1 row)
查看表数据,发现未提交的事务被回滚,insert语句插入的数据没有出现在表中。
-
重复步骤2重新登录数据库,在不开启显示事务下执行insert语句
openGauss=# insert into t3 values(1, 'test'); INSERT 0 1
-
查看当前xlog插入点及LSN所在WAL的位置
openGauss=# select pg_current_xlog_insert_location(); pg_current_xlog_insert_location --------------------------------- 0/244FE20 (1 row) openGauss=# select pg_xlogfile_name('0/244FE20'); pg_xlogfile_name -------------------------- 000000010000000000000002 (1 row) openGauss=# select pg_xlogfile_name_offset('0/244FE20'); pg_xlogfile_name_offset ------------------------------------ (000000010000000000000002,4521504) (1 row)
xlog当前插入点:0/244FE20,当前插入的xlog segment为:000000010000000000000002,LSN为:4521504
-
打开新的SSH会话窗口以immediate模式关闭数据库
gs_om -t stop -m immediate
-
重复步骤6查看新的运行日志变化情况
找到最新的一条日志文件,查看最后11数据,内容如下:2024-03-25 21:06:49.293 660174cd.6125 [unknown] 140493807679232 dn_6001 0 dn_6001 00000 0 [BACKEND] LOG: keep all the xlog segments, because current segno = 2, less than wal_keep_segments = 16 2024-03-25 21:06:49.294 660174cd.6125 [unknown] 140493807679232 dn_6001 0 dn_6001 01000 0 [BACKEND] WARNING: replicationSlotMinLSN is InvalidXLogRecPtr!!! 2024-03-25 21:06:49.294 660174cd.6125 [unknown] 140493807679232 dn_6001 0 dn_6001 01000 0 [BACKEND] WARNING: replicationSlotMaxLSN is InvalidXLogRecPtr!!! 2024-03-25 21:06:49.294 660174cd.6125 [unknown] 140493807679232 dn_6001 0 dn_6001 00000 0 [BACKEND] LOG: CreateCheckPoint PrintCkpXctlControlFile: [checkPoint] oldCkpLoc:0/244FCA0, oldRedo:0/244FC20, newCkpLoc:0/244FEA0, newRedo:0/244FE20, preCkpLoc:0/244FB80 2024-03-25 21:06:49.294 660174cd.6125 [unknown] 140493807679232 dn_6001 0 dn_6001 00000 0 [BACKEND] LOG: will update control file (create checkpoint), shutdown:0 2024-03-25 21:06:49.296 660174cd.6125 [unknown] 140493807679232 dn_6001 0 dn_6001 00000 0 [BACKEND] LOG: attempting to remove WAL segments older than log file 000000010000000000000000 2024-03-25 21:07:07.667 660176fb.5060 postgres 140493504444160 Clean Statement thread 0 dn_6001 00000 0 [BACKEND] LOG: clean statement thread start 2024-03-25 21:07:43.608 660174cd.1 [unknown] 140497468059584 [unknown] 0 dn_6001 00000 0 [BACKEND] LOG: received immediate shutdown request 2024-03-25 21:07:43.608 660174cd.10000 [unknown] 140494557538048 dn_6001 0 dn_6001 00000 0 [BACKEND] LOG: [Alarm Module]alarm checker shutting down... 2024-03-25 21:07:43.612 660174cd.1 [unknown] 140497468059584 [unknown] 0 dn_6001 00000 0 [BACKEND] LOG: FiniNuma allocIndex: 0. 2024-03-25 21:07:43.612 660174cd.1 [unknown] 140497468059584 [unknown] 0 dn_6001 00000 0 [BACKEND] LOG: Gaussdb exit(0)
注意,其中有一行日志为:
[BACKEND] LOG: CreateCheckPoint PrintCkpXctlControlFile: [checkPoint] oldCkpLoc:0/244FCA0, oldRedo:0/244FC20, newCkpLoc:0/244FEA0,
newRedo:0/244FE20
, preCkpLoc:0/244FB80
表明了重做日志点的位置:0/244FE20
,这个值和第9步骤看到的是一样的。 -
再重启数据库,查看t3表数据及运行日志变化情况
openGauss=# select * from t3; id | name ----+------ 1 | test (1 row)
t3表已有数据。再看最新的运行日志:
[BACKEND] LOG: database system was not properly shut down; automatic recovery in progress [BACKEND] LOG: StartupXLOG PrintCkpXctlControlFile: [checkPoint] oldCkpLoc:0/244FEA0, oldRedo:0/244FE20, newCkpLoc:0/244FEA0, newRedo:0/244FE20, preCkpLoc:0/244FCA0 …… [BACKEND] LOG: redo starts at 0/244FE20 [BACKEND] LOG: redo done at 0/244FEA0, end at 0/244FF40 [REDO] LOG: [PR]: Recoverying elapsed: 102217 us, redoTotalBytes:288,EndRecPtr:38076224, redoStartPtr:38075936,speed:0 MB/s, totalTime:102217 [BACKEND] LOG: CreateCheckPoint PrintCkpXctlControlFile: [checkPoint] oldCkpLoc:0/244FEA0, oldRedo:0/244FE20, newCkpLoc:0/244FFC0, newRedo:0/244FF40, preCkpLoc:0/244FEA0
其中
redo starts at 0/244FE20
说明重启后,数据库需要重做日志,将数据进行恢复。