KingbaseES恢复被删除数据

生产环境操作请先备份整个data目录或cp 当前数据目录/home/kingbase/pg_data到新的data目录,然后在备份的data目录进行恢复被删除数据操作。

通过新备份的路径恢复数据之后,确认数据完整性(确认业务数据完整性)。再把数据从备份环境导出重新导入到生产环境。

使用到的系统工具:
sys_resetwal
sys_waldump

1.准备测试环境:

使用initdb初始化数据库
[kingbase@postgres ~]$ initdb -EUTF8 -Usystem -mpg -D /home/kingbase/pg_data
The files belonging to this database system will be owned by user "kingbase".
This user must also own the server process. The database cluster will be initialized with locale "C".
The default text search configuration will be set to "english". The comparision of strings is case-sensitive.
Data page checksums are disabled. creating directory /home/kingbase/pg_data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Shanghai
creating configuration files ... ok
Begin setup encrypt device
initializing the encrypt device ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
create security database ... ok
load security database ... ok
syncing data to disk ... ok initdb: warning: enabling "trust" authentication for local connections
You can change this by editing sys_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb. Success. You can now start the database server using: sys_ctl -D /home/kingbase/pg_data -l logfile start --启动刚initdb初始化的数据库
[kingbase@postgres ~]$ sys_ctl -D /home/kingbase/pg_data/ start
waiting for server to start....2022-12-16 10:26:49.044 CST [24811] LOG: sepapower extension initialized
2022-12-16 10:26:49.049 CST [24811] LOG: starting KingbaseES V008R006C006B0021 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-46), 64-bit
2022-12-16 10:26:49.049 CST [24811] LOG: listening on IPv4 address "0.0.0.0", port 54321
2022-12-16 10:26:49.049 CST [24811] LOG: listening on IPv6 address "::", port 54321
2022-12-16 10:26:49.053 CST [24811] LOG: listening on Unix socket "/tmp/.s.KINGBASE.54321"
2022-12-16 10:26:49.095 CST [24811] LOG: redirecting log output to logging collector process
2022-12-16 10:26:49.095 CST [24811] HINT: Future log output will appear in directory "sys_log".
done
server started [kingbase@postgres ~]$ ksql -Usystem -dtest
ksql (V8.0)
Type "help" for help. test=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+--------+----------+---------+-------+-------------------
security | system | UTF8 | C | C |
template0 | system | UTF8 | C | C | =c/system +
| | | | | system=CTc/system
template1 | system | UTF8 | C | C | =c/system +
| | | | | system=CTc/system
test | system | UTF8 | C | C |
(4 rows)
  1. 创建测试表并插入测试数据
--创建表
CREATE TABLE TB (
"MENU_ID" numeric(8,0) NOT NULL,
"SYSTEM_CODE" character varying(15) NULL,
"ORDER_NO" character varying(5) NULL,
"TITLE" character varying(50) NULL,
"OBJECT_CODE" character varying(12) NULL,
"ICO" character varying(36) NULL,
"ICO_LAYOUT" character varying(10) NULL,
"MENU_ID_PARENT" numeric(8,0) NOT NULL DEFAULT 0
);
--插入数据
INSERT INTO TB ("MENU_ID","SYSTEM_CODE","ORDER_NO","TITLE","OBJECT_CODE","ICO","ICO_LAYOUT","MENU_ID_PARENT") VALUES
(1320,'SYS','4D1','药品黑白名单管理','PASS-W01',NULL,NULL,1319),
(1321,'SYS','4D2','药品限制使用设置','PASS-W03',NULL,NULL,1319),
(293,'SYS','3843','医嘱排斥项设置','ORDER-W05',NULL,NULL,260),
(294,'SYS','385','化验医嘱',NULL,NULL,NULL,515),
(295,'SYS','3851','化验容器设置','ASSAY-W01',NULL,NULL,294),
(296,'SYS','3852','化验标本类型设置','ASSAY-W02',NULL,NULL,294),
(297,'SYS','3853','化验报告取单时间设置','ASSAY-W03',NULL,NULL,294),
(300,'SYS','10','功能相关',NULL,NULL,NULL,299),
(301,'SYS','101','系统对象设置','SYS-W01',NULL,NULL,300),
(302,'SYS','102','系统菜单设置','SYS-W02',NULL,NULL,300);
INSERT INTO TB ("MENU_ID","SYSTEM_CODE","ORDER_NO","TITLE","OBJECT_CODE","ICO","ICO_LAYOUT","MENU_ID_PARENT") VALUES
(303,'SYS','103','系统平台功能设置','SYS-W03',NULL,NULL,300),
(1172,'SYS','150','系统报表设置','RPT-W11',NULL,NULL,1171),
(1173,'SYS','151','病人出院列表','RPT-W01',NULL,NULL,1171),
(1174,'SYS','152','费用统计','RPT-W01',NULL,NULL,1171),
(1175,'SYS','153','医院业务统计报表','RPT-W01',NULL,NULL,1171),
(663,'SYS','3B6','体温单格式配置','ENR-W14',NULL,NULL,373),
(1176,'SYS','154','医院业务统计报表(病区)','RPT-W01',NULL,NULL,1171),
(665,'SYS','316','病案诊断类型设置','DIAG-W04',NULL,NULL,613),
(666,'SYS','132','报表格式打印机设置','SYS1-W04',NULL,NULL,405),
(669,'KTCIS_IDW','57','住院病历质量检查评分','EMR-W21',NULL,NULL,239);
INSERT INTO TB ("MENU_ID","SYSTEM_CODE","ORDER_NO","TITLE","OBJECT_CODE","ICO","ICO_LAYOUT","MENU_ID_PARENT") VALUES
(673,'SYS','4S','包药机相关设置',NULL,NULL,NULL,516),
(674,'SYS','4S1','包药机设置','MED-W05',NULL,NULL,673),
(373,'SYS','3B','电子护理记录基础设置',NULL,NULL,NULL,517),
(675,'SYS','4M','摆药包药相关设置',NULL,NULL,NULL,516),
(676,'SYS','4M0','药品包药字典设置','PACK_MED-W01',NULL,NULL,675),
(304,'SYS','104','系统参数设置','SYS-W04',NULL,NULL,300),
(305,'SYS','105','个人参数设置','SYS-W05',NULL,NULL,300),
(306,'SYS','106','机器参数设置','SYS-W06',NULL,NULL,300),
(307,'SYS','107','系统机器设置','SYS-W07',NULL,NULL,300),
(308,'SYS','108','通知发送','SYS-W08',NULL,NULL,300);
INSERT INTO TB ("MENU_ID","SYSTEM_CODE","ORDER_NO","TITLE","OBJECT_CODE","ICO","ICO_LAYOUT","MENU_ID_PARENT") VALUES
(309,'SYS','109','系统文件发布','SYS-W09',NULL,NULL,300),
(310,'SYS','10A','系统文件查询','SYS-W10',NULL,NULL,300),
(311,'SYS','10B','带功能的窗体测试','SYS-W99',NULL,NULL,300),
(312,'SYS','11','行政组织',NULL,NULL,NULL,299),
(325,'SYS','12','医院基础设置',NULL,NULL,NULL,299),
(326,'SYS','311','数据字典设置','PUB2-W01',NULL,NULL,613),
(327,'SYS','121','行政区域设置','PUB2-W02',NULL,NULL,325),
(329,'SYS','1251','自定义字段设置','PUB2-W05',NULL,NULL,614),
(330,'SYS','124','医疗工作点设置','PUB2-W06',NULL,NULL,325),
(331,'SYS','1252','系统条件参数设置','PUB2-W07',NULL,NULL,614);
  1. 删除数据后,未执行checkpoint,vacuum操作,进行数据恢复

    3.1 确认当前事务日志及事务(LSN)号

select pg_current_wal_lsn(),pg_walfile_name(pg_current_wal_lsn()),pg_walfile_name_offset(pg_current_wal_lsn());
--查询insert后,delete前的当前事务日志及事务(LSN)号
test=# select pg_current_wal_lsn(),pg_walfile_name(pg_current_wal_lsn()),pg_walfile_name_offset(pg_current_wal_lsn());
pg_current_wal_lsn | pg_walfile_name | pg_walfile_name_offset
--------------------+--------------------------+------------------------------------
0/17AF9D0 | 000000010000000000000001 | (000000010000000000000001,8059344)
(1 row)

​ 3.2 进行删除数据操作,不手动执行chekpoint命令

test=# select count(*) from tb;
count
-------
40
(1 row) test=# delete from tb where "MENU_ID"=300;
DELETE 1
test=#
test=# select count(*) from tb;
count
-------
39
(1 row) --查询进行delete删除操作后的当前事务日志及事务(LSN)号 test=# select pg_current_wal_lsn(),pg_walfile_name(pg_current_wal_lsn()),pg_walfile_name_offset(pg_current_wal_lsn());
pg_current_wal_lsn | pg_walfile_name | pg_walfile_name_offset
--------------------+--------------------------+------------------------------------
0/17B07B8 | 000000010000000000000001 | (000000010000000000000001,8062904)
(1 row)

​ 3.3 根据查询到的事务号以及日志文件,查找WAL日志确定恢复数据的范围

lsn数据类型可以被用来存储 LSN(日志序列号)数据,LSN 是一个指向WAL中的位置的指针。
这个类型是XLogRecPtr的一种表达并且是数据库内部系统类型。 在数据库内部,一个 LSN 是一个 64 位整数,表示在预写式日志流中的一个字节位置。
它由两个长度为8位的十六进制数组成,中间用斜线分隔,如6/7311F80。
lsn类型支持标准的比较操作符 = 和 >
两个 LSN 可以做相减操作, 结果将是分隔两个预写式日志位置的字节数 --通过wal日志事物号找到delete的大概位置,这里位置是 552 [kingbase@postgres sys_wal]$ pwd
/home/kingbase/pg_data/sys_wal
[kingbase@postgres sys_wal]$ ls -l
total 16384
-rw------- 1 kingbase kingbase 16777216 Dec 16 11:47 000000010000000000000001
drwx------ 2 kingbase kingbase 6 Dec 16 10:26 archive_status [kingbase@postgres sys_wal]$ sys_waldump 000000010000000000000001 -s 0/17AF9D0
rmgr: Heap len (rec/tot): 59/ 3227, tx: 552, lsn: 0/017AF9D0, prev 0/017AF9A0, desc: DELETE off 8 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16384 blk 0 FPW
rmgr: Transaction len (rec/tot): 34/ 34, tx: 552, lsn: 0/017B0688, prev 0/017AF9D0, desc: COMMIT 2022-12-16 11:46:15.040166 CST
rmgr: Standby len (rec/tot): 42/ 42, tx: 0, lsn: 0/017B06B0, prev 0/017B0688, desc: RUNNING_XACTS nextXid 553 latestCompletedXid 24839816 oldestRunningXid 553
rmgr: Standby len (rec/tot): 42/ 42, tx: 0, lsn: 0/017B06E0, prev 0/017B06B0, desc: RUNNING_XACTS nextXid 553 latestCompletedXid 24839904 oldestRunningXid 553
rmgr: XLOG len (rec/tot): 114/ 114, tx: 0, lsn: 0/017B0710, prev 0/017B06E0, desc: CHECKPOINT_ONLINE redo 0/17B06E0; tli 1; prev tli 1; fpw true; xid 0:553; oid 24576; multi 1; offset 0; oldest xid 519 in DB 1; oldest multi 1 in DB 1; oldest/newest commit timestamp xid: 0/0; oldest running xid 553; online
rmgr: Standby len (rec/tot): 42/ 42, tx: 0, lsn: 0/017B0788, prev 0/017B0710, desc: RUNNING_XACTS nextXid 553 latestCompletedXid 24839952 oldestRunningXid 553
rmgr: XLOG len (rec/tot): 114/ 114, tx: 0, lsn: 0/017B07B8, prev 0/017B0788, desc: CHECKPOINT_SHUTDOWN redo 0/17B07B8; tli 1; prev tli 1; fpw true; xid 0:553; oid 16388; multi 1; offset 0; oldest xid 519 in DB 1; oldest multi 1 in DB 1; oldest/newest commit timestamp xid: 0/0; oldest running xid 0; shutdown
sys_waldump: fatal: error in WAL record at 0/17B07B8: invalid record length at 0/17B0830: wanted 24, got 0 --如果删除数据量大,涉及多个wal日志,也可以指定事务日志起始wal文件名,结束wal日志文件名称
--示例 sys_waldump 000000010000000000000001 000000010000000000000009 -s 0/17AF9D0 --通过以下命令进行在wal日志精确定位事务号 [kingbase@postgres sys_wal]$ sys_waldump 000000010000000000000001 | grep DELETE
sys_waldump: fatal: error in WAL record at 0/17B07B8: invalid record length at 0/17B0830: wanted 24, got 0
rmgr: Heap len (rec/tot): 59/ 3227, tx: 552, lsn: 0/017AF9D0, prev 0/017AF9A0, desc: DELETE off 8 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16384 blk 0 FPW
[kingbase@postgres sys_wal]$

​ 3.4 进行删除数据恢复操作

--停止数据库
[kingbase@postgres ~]$ sys_ctl -D /home/kingbase/pg_data/ stop
waiting for server to shut down.... done
server stopped --备份当前数据目录data到新的路径或者新的目录 [kingbase@postgres ~]$ cp -r pg_data/ pg_data_bak
[kingbase@postgres ~]$ ll
total 808
drwxrwxr-x 3 kingbase kingbase 16 Nov 10 01:12 V8R6C6B21
-rwxr-xr-x 1 kingbase kingbase 3276 Nov 10 00:31 license.dat
-rw-rw-r-- 1 kingbase kingbase 252 Dec 6 16:48 my_test.c
-rw-rw-r-- 1 kingbase kingbase 10403 Dec 13 18:21 nohup.out
drwx------ 22 kingbase kingbase 4096 Dec 16 10:26 ora_data
drwx------ 23 kingbase kingbase 4096 Dec 16 11:47 pg_data
drwx------ 23 kingbase kingbase 4096 Dec 16 12:01 pg_data_bak --使用备份的data目录,通过指定事务号进行删除数据的恢复。
--使用sys_resetwal工具指定事务号552进行删除数据的恢复 [kingbase@postgres ~]$ sys_resetwal -x 552 /home/kingbase/pg_data_bak/
Write-ahead log reset --启动通过cp备份的数据库查询数据是否被恢复 [kingbase@postgres ~]$ sys_ctl -D /home/kingbase/pg_data_bak/ start
waiting for server to start....2022-12-16 12:06:48.993 CST [30197] LOG: sepapower extension initialized
2022-12-16 12:06:48.997 CST [30197] LOG: starting KingbaseES V008R006C006B0021 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-46), 64-bit
2022-12-16 12:06:48.997 CST [30197] LOG: listening on IPv4 address "0.0.0.0", port 54321
2022-12-16 12:06:48.997 CST [30197] LOG: listening on IPv6 address "::", port 54321
2022-12-16 12:06:49.001 CST [30197] LOG: listening on Unix socket "/tmp/.s.KINGBASE.54321"
2022-12-16 12:06:49.082 CST [30197] LOG: redirecting log output to logging collector process
2022-12-16 12:06:49.082 CST [30197] HINT: Future log output will appear in directory "sys_log".
done
server started [kingbase@postgres ~]$ ksql -Usystem -dtest
ksql (V8.0)
Type "help" for help. test=# select count(*) from tb;
count
-------
40
(1 row) test=# select * from tb where "MENU_ID"=300;
MENU_ID | SYSTEM_CODE | ORDER_NO | TITLE | OBJECT_CODE | ICO | ICO_LAYOUT | MENU_ID_PARENT
---------+-------------+----------+--------------+-------------+-----+------------+----------------
300 | SYS | 10 | 功能相关 | | | | 299
(1 row) 经确认进行删除表数据操作后未手动执行checkpoint命令,通过sys_resetwal工具可以恢复被删除的数据。
  1. 删除数据后执行checkpoint,不执行vacuum,恢复被删除的数据
--启动pg_data数据库
[kingbase@postgres ~]$ sys_ctl -D /home/kingbase/pg_data/ start
waiting for server to start....2022-12-16 12:13:51.096 CST [30580] LOG: sepapower extension initialized
2022-12-16 12:13:51.101 CST [30580] LOG: starting KingbaseES V008R006C006B0021 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-46), 64-bit
2022-12-16 12:13:51.101 CST [30580] LOG: listening on IPv4 address "0.0.0.0", port 54321
2022-12-16 12:13:51.101 CST [30580] LOG: listening on IPv6 address "::", port 54321
2022-12-16 12:13:51.105 CST [30580] LOG: listening on Unix socket "/tmp/.s.KINGBASE.54321"
2022-12-16 12:13:51.142 CST [30580] LOG: redirecting log output to logging collector process
2022-12-16 12:13:51.142 CST [30580] HINT: Future log output will appear in directory "sys_log".
done
server started --登录数据库删除TB表 [kingbase@postgres ~]$ ksql -Usystem -dtest
ksql (V8.0)
Type "help" for help. test=# drop table tb;
DROP TABLE --创建表
CREATE TABLE TB (
"MENU_ID" numeric(8,0) NOT NULL,
"SYSTEM_CODE" character varying(15) NULL,
"ORDER_NO" character varying(5) NULL,
"TITLE" character varying(50) NULL,
"OBJECT_CODE" character varying(12) NULL,
"ICO" character varying(36) NULL,
"ICO_LAYOUT" character varying(10) NULL,
"MENU_ID_PARENT" numeric(8,0) NOT NULL DEFAULT 0
);
--插入数据
INSERT INTO TB ("MENU_ID","SYSTEM_CODE","ORDER_NO","TITLE","OBJECT_CODE","ICO","ICO_LAYOUT","MENU_ID_PARENT") VALUES
(1320,'SYS','4D1','药品黑白名单管理','PASS-W01',NULL,NULL,1319),
(1321,'SYS','4D2','药品限制使用设置','PASS-W03',NULL,NULL,1319),
(293,'SYS','3843','医嘱排斥项设置','ORDER-W05',NULL,NULL,260),
(294,'SYS','385','化验医嘱',NULL,NULL,NULL,515),
(295,'SYS','3851','化验容器设置','ASSAY-W01',NULL,NULL,294),
(296,'SYS','3852','化验标本类型设置','ASSAY-W02',NULL,NULL,294),
(297,'SYS','3853','化验报告取单时间设置','ASSAY-W03',NULL,NULL,294),
(300,'SYS','10','功能相关',NULL,NULL,NULL,299),
(301,'SYS','101','系统对象设置','SYS-W01',NULL,NULL,300),
(302,'SYS','102','系统菜单设置','SYS-W02',NULL,NULL,300);
INSERT INTO TB ("MENU_ID","SYSTEM_CODE","ORDER_NO","TITLE","OBJECT_CODE","ICO","ICO_LAYOUT","MENU_ID_PARENT") VALUES
(303,'SYS','103','系统平台功能设置','SYS-W03',NULL,NULL,300),
(1172,'SYS','150','系统报表设置','RPT-W11',NULL,NULL,1171),
(1173,'SYS','151','病人出院列表','RPT-W01',NULL,NULL,1171),
(1174,'SYS','152','费用统计','RPT-W01',NULL,NULL,1171),
(1175,'SYS','153','医院业务统计报表','RPT-W01',NULL,NULL,1171),
(663,'SYS','3B6','体温单格式配置','ENR-W14',NULL,NULL,373),
(1176,'SYS','154','医院业务统计报表(病区)','RPT-W01',NULL,NULL,1171),
(665,'SYS','316','病案诊断类型设置','DIAG-W04',NULL,NULL,613),
(666,'SYS','132','报表格式打印机设置','SYS1-W04',NULL,NULL,405),
(669,'KTCIS_IDW','57','住院病历质量检查评分','EMR-W21',NULL,NULL,239);
INSERT INTO TB ("MENU_ID","SYSTEM_CODE","ORDER_NO","TITLE","OBJECT_CODE","ICO","ICO_LAYOUT","MENU_ID_PARENT") VALUES
(673,'SYS','4S','包药机相关设置',NULL,NULL,NULL,516),
(674,'SYS','4S1','包药机设置','MED-W05',NULL,NULL,673),
(373,'SYS','3B','电子护理记录基础设置',NULL,NULL,NULL,517),
(675,'SYS','4M','摆药包药相关设置',NULL,NULL,NULL,516),
(676,'SYS','4M0','药品包药字典设置','PACK_MED-W01',NULL,NULL,675),
(304,'SYS','104','系统参数设置','SYS-W04',NULL,NULL,300),
(305,'SYS','105','个人参数设置','SYS-W05',NULL,NULL,300),
(306,'SYS','106','机器参数设置','SYS-W06',NULL,NULL,300),
(307,'SYS','107','系统机器设置','SYS-W07',NULL,NULL,300),
(308,'SYS','108','通知发送','SYS-W08',NULL,NULL,300);
INSERT INTO TB ("MENU_ID","SYSTEM_CODE","ORDER_NO","TITLE","OBJECT_CODE","ICO","ICO_LAYOUT","MENU_ID_PARENT") VALUES
(309,'SYS','109','系统文件发布','SYS-W09',NULL,NULL,300),
(310,'SYS','10A','系统文件查询','SYS-W10',NULL,NULL,300),
(311,'SYS','10B','带功能的窗体测试','SYS-W99',NULL,NULL,300),
(312,'SYS','11','行政组织',NULL,NULL,NULL,299),
(325,'SYS','12','医院基础设置',NULL,NULL,NULL,299),
(326,'SYS','311','数据字典设置','PUB2-W01',NULL,NULL,613),
(327,'SYS','121','行政区域设置','PUB2-W02',NULL,NULL,325),
(329,'SYS','1251','自定义字段设置','PUB2-W05',NULL,NULL,614),
(330,'SYS','124','医疗工作点设置','PUB2-W06',NULL,NULL,325),
(331,'SYS','1252','系统条件参数设置','PUB2-W07',NULL,NULL,614);

​ 4.1 确认当前事务日志及事务(LSN)号

select pg_current_wal_lsn(),pg_walfile_name(pg_current_wal_lsn()),pg_walfile_name_offset(pg_current_wal_lsn());
--查询insert后,delete前的当前事务日志及事务(LSN)号
test=# select pg_current_wal_lsn(),pg_walfile_name(pg_current_wal_lsn()),pg_walfile_name_offset(pg_current_wal_lsn());
pg_current_wal_lsn | pg_walfile_name | pg_walfile_name_offset
--------------------+--------------------------+------------------------------------
0/17CD750 | 000000010000000000000001 | (000000010000000000000001,8181584)
(1 row)

​ 4.2 进行删除数据操作,手动执行chekpoint命令

test=# delete from tb ;
DELETE 40
test=# select count(*) from tb;
count
-------
0
(1 row) test=# checkpoint ;
CHECKPOINT --查询删除数据并且手动执行checkpoint命令后,当前事务日志及事务(LSN)号 test=# select pg_current_wal_lsn(),pg_walfile_name(pg_current_wal_lsn()),pg_walfile_name_offset(pg_current_wal_lsn());
pg_current_wal_lsn | pg_walfile_name | pg_walfile_name_offset
--------------------+--------------------------+------------------------------------
0/17CED90 | 000000010000000000000001 | (000000010000000000000001,8187280)
(1 row)

​ 4.3 根据查询到的事务号以及日志文件,查找WAL日志确定恢复数据的范围

--通过wal日志事物号找到delete的大概位置,这里位置是 559

# sys_waldump 000000010000000000000001 -s 0/17CD750
[kingbase@postgres sys_wal]$ sys_waldump 000000010000000000000001 -s 0/17CD750
rmgr: Heap len (rec/tot): 59/ 3227, tx: 559, lsn: 0/017CD750, prev 0/017CD720, desc: DELETE off 1 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0 FPW
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE408, prev 0/017CD750, desc: DELETE off 2 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE440, prev 0/017CE408, desc: DELETE off 3 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE478, prev 0/017CE440, desc: DELETE off 4 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE4B0, prev 0/017CE478, desc: DELETE off 5 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE4E8, prev 0/017CE4B0, desc: DELETE off 6 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE520, prev 0/017CE4E8, desc: DELETE off 7 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE558, prev 0/017CE520, desc: DELETE off 8 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE590, prev 0/017CE558, desc: DELETE off 9 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE5C8, prev 0/017CE590, desc: DELETE off 10 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE600, prev 0/017CE5C8, desc: DELETE off 11 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE638, prev 0/017CE600, desc: DELETE off 12 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE670, prev 0/017CE638, desc: DELETE off 13 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE6A8, prev 0/017CE670, desc: DELETE off 14 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE6E0, prev 0/017CE6A8, desc: DELETE off 15 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE718, prev 0/017CE6E0, desc: DELETE off 16 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE750, prev 0/017CE718, desc: DELETE off 17 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE788, prev 0/017CE750, desc: DELETE off 18 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE7C0, prev 0/017CE788, desc: DELETE off 19 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE7F8, prev 0/017CE7C0, desc: DELETE off 20 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE830, prev 0/017CE7F8, desc: DELETE off 21 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE868, prev 0/017CE830, desc: DELETE off 22 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE8A0, prev 0/017CE868, desc: DELETE off 23 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE8D8, prev 0/017CE8A0, desc: DELETE off 24 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE910, prev 0/017CE8D8, desc: DELETE off 25 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE948, prev 0/017CE910, desc: DELETE off 26 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE980, prev 0/017CE948, desc: DELETE off 27 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE9B8, prev 0/017CE980, desc: DELETE off 28 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE9F0, prev 0/017CE9B8, desc: DELETE off 29 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CEA28, prev 0/017CE9F0, desc: DELETE off 30 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CEA60, prev 0/017CEA28, desc: DELETE off 31 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CEA98, prev 0/017CEA60, desc: DELETE off 32 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CEAD0, prev 0/017CEA98, desc: DELETE off 33 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CEB08, prev 0/017CEAD0, desc: DELETE off 34 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CEB40, prev 0/017CEB08, desc: DELETE off 35 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CEB78, prev 0/017CEB40, desc: DELETE off 36 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CEBB0, prev 0/017CEB78, desc: DELETE off 37 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CEBE8, prev 0/017CEBB0, desc: DELETE off 38 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CEC20, prev 0/017CEBE8, desc: DELETE off 39 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CEC58, prev 0/017CEC20, desc: DELETE off 40 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Transaction len (rec/tot): 34/ 34, tx: 559, lsn: 0/017CEC90, prev 0/017CEC58, desc: COMMIT 2022-12-16 12:21:54.321583 CST
rmgr: Standby len (rec/tot): 46/ 46, tx: 0, lsn: 0/017CECB8, prev 0/017CEC90, desc: RUNNING_XACTS nextXid 560 latestCompletedXid 24964240 oldestRunningXid 559
rmgr: Standby len (rec/tot): 42/ 42, tx: 0, lsn: 0/017CECE8, prev 0/017CECB8, desc: RUNNING_XACTS nextXid 560 latestCompletedXid 24964328 oldestRunningXid 560
rmgr: XLOG len (rec/tot): 114/ 114, tx: 0, lsn: 0/017CED18, prev 0/017CECE8, desc: CHECKPOINT_ONLINE redo 0/17CECE8; tli 1; prev tli 1; fpw true; xid 0:560; oid 24580; multi 1; offset 0; oldest xid 519 in DB 1; oldest multi 1 in DB 1; oldest/newest commit timestamp xid: 0/0; oldest running xid 560; online
rmgr: Standby len (rec/tot): 42/ 42, tx: 0, lsn: 0/017CED90, prev 0/017CED18, desc: RUNNING_XACTS nextXid 560 latestCompletedXid 24964376 oldestRunningXid 560
rmgr: Heap len (rec/tot): 53/ 1081, tx: 0, lsn: 0/017CEDC0, prev 0/017CED90, desc: INPLACE off 3, blkref #0: rel 1663/13857/1259 blk 0 FPW
rmgr: Standby len (rec/tot): 90/ 90, tx: 0, lsn: 0/017CF200, prev 0/017CEDC0, desc: INVALIDATIONS ; inval msgs: catcache 62 catcache 61 relcache 16388
rmgr: Standby len (rec/tot): 42/ 42, tx: 0, lsn: 0/017CF260, prev 0/017CF200, desc: RUNNING_XACTS nextXid 560 latestCompletedXid 24965632 oldestRunningXid 560
rmgr: XLOG len (rec/tot): 114/ 114, tx: 0, lsn: 0/017CF290, prev 0/017CF260, desc: CHECKPOINT_SHUTDOWN redo 0/17CF290; tli 1; prev tli 1; fpw true; xid 0:560; oid 16392; multi 1; offset 0; oldest xid 519 in DB 1; oldest multi 1 in DB 1; oldest/newest commit timestamp xid: 0/0; oldest running xid 0; shutdown
sys_waldump: fatal: error in WAL record at 0/17CF290: invalid record length at 0/17CF308: wanted 24, got 0 --如果删除数据量大,涉及多个wal日志,也可以指定事务日志起始wal文件名,结束wal日志文件名称
--示例 sys_waldump 000000010000000000000001 000000010000000000000009 -s 0/17AF9D0 --通过以下命令进行在wal日志精确定位事务号 [kingbase@postgres sys_wal]$ sys_waldump 000000010000000000000001 | grep DELETE rmgr: Heap len (rec/tot): 59/ 3227, tx: 559, lsn: 0/017CD750, prev 0/017CD720, desc: DELETE off 1 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0 FPW
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE408, prev 0/017CD750, desc: DELETE off 2 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE440, prev 0/017CE408, desc: DELETE off 3 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE478, prev 0/017CE440, desc: DELETE off 4 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE4B0, prev 0/017CE478, desc: DELETE off 5 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE4E8, prev 0/017CE4B0, desc: DELETE off 6 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE520, prev 0/017CE4E8, desc: DELETE off 7 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE558, prev 0/017CE520, desc: DELETE off 8 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE590, prev 0/017CE558, desc: DELETE off 9 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE5C8, prev 0/017CE590, desc: DELETE off 10 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE600, prev 0/017CE5C8, desc: DELETE off 11 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE638, prev 0/017CE600, desc: DELETE off 12 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE670, prev 0/017CE638, desc: DELETE off 13 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE6A8, prev 0/017CE670, desc: DELETE off 14 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE6E0, prev 0/017CE6A8, desc: DELETE off 15 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE718, prev 0/017CE6E0, desc: DELETE off 16 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE750, prev 0/017CE718, desc: DELETE off 17 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE788, prev 0/017CE750, desc: DELETE off 18 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE7C0, prev 0/017CE788, desc: DELETE off 19 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE7F8, prev 0/017CE7C0, desc: DELETE off 20 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE830, prev 0/017CE7F8, desc: DELETE off 21 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE868, prev 0/017CE830, desc: DELETE off 22 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE8A0, prev 0/017CE868, desc: DELETE off 23 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE8D8, prev 0/017CE8A0, desc: DELETE off 24 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE910, prev 0/017CE8D8, desc: DELETE off 25 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE948, prev 0/017CE910, desc: DELETE off 26 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE980, prev 0/017CE948, desc: DELETE off 27 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE9B8, prev 0/017CE980, desc: DELETE off 28 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CE9F0, prev 0/017CE9B8, desc: DELETE off 29 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CEA28, prev 0/017CE9F0, desc: DELETE off 30 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CEA60, prev 0/017CEA28, desc: DELETE off 31 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CEA98, prev 0/017CEA60, desc: DELETE off 32 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CEAD0, prev 0/017CEA98, desc: DELETE off 33 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CEB08, prev 0/017CEAD0, desc: DELETE off 34 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CEB40, prev 0/017CEB08, desc: DELETE off 35 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CEB78, prev 0/017CEB40, desc: DELETE off 36 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CEBB0, prev 0/017CEB78, desc: DELETE off 37 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CEBE8, prev 0/017CEBB0, desc: DELETE off 38 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CEC20, prev 0/017CEBE8, desc: DELETE off 39 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 559, lsn: 0/017CEC58, prev 0/017CEC20, desc: DELETE off 40 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0

​ 4.4 进行恢复删除数据操作

--停止数据库
[kingbase@postgres ~]$ sys_ctl -D /home/kingbase/pg_data/ stop
waiting for server to shut down.... done
server stopped [kingbase@postgres ~]$ ls -l
total 804
drwxrwxr-x 3 kingbase kingbase 16 Nov 10 01:12 V8R6C6B21
-rwxr-xr-x 1 kingbase kingbase 3276 Nov 10 00:31 license.dat
-rw-rw-r-- 1 kingbase kingbase 252 Dec 6 16:48 my_test.c
-rw-rw-r-- 1 kingbase kingbase 10403 Dec 13 18:21 nohup.out
drwx------ 22 kingbase kingbase 4096 Dec 16 10:26 ora_data
drwx------ 23 kingbase kingbase 4096 Dec 16 12:23 pg_data --备份当前data数据目录到新目录
[kingbase@postgres ~]$ cp -r pg_data/ pg_data_bak
[kingbase@postgres ~]$ ls -l
total 808
drwxrwxr-x 3 kingbase kingbase 16 Nov 10 01:12 V8R6C6B21
-rwxr-xr-x 1 kingbase kingbase 3276 Nov 10 00:31 license.dat
-rw-rw-r-- 1 kingbase kingbase 252 Dec 6 16:48 my_test.c
-rw-rw-r-- 1 kingbase kingbase 10403 Dec 13 18:21 nohup.out
drwx------ 22 kingbase kingbase 4096 Dec 16 10:26 ora_data
drwx------ 23 kingbase kingbase 4096 Dec 16 12:23 pg_data
drwx------ 23 kingbase kingbase 4096 Dec 16 12:32 pg_data_bak --使用备份的data目录,通过指定事务号进行删除数据的恢复。
--使用sys_resetwal工具指定事务号552进行删除数据的恢复 [kingbase@postgres ~]$ sys_resetwal -x 559 /home/kingbase/pg_data_bak/
Write-ahead log reset --启动通过cp备份的数据库查询数据是否被恢复 [kingbase@postgres ~]$ sys_ctl -D /home/kingbase/pg_data_bak/ start
waiting for server to start....2022-12-16 12:34:36.822 CST [31721] LOG: sepapower extension initialized
2022-12-16 12:34:36.827 CST [31721] LOG: starting KingbaseES V008R006C006B0021 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-46), 64-bit
2022-12-16 12:34:36.828 CST [31721] LOG: listening on IPv4 address "0.0.0.0", port 54321
2022-12-16 12:34:36.828 CST [31721] LOG: listening on IPv6 address "::", port 54321
2022-12-16 12:34:36.831 CST [31721] LOG: listening on Unix socket "/tmp/.s.KINGBASE.54321"
2022-12-16 12:34:36.874 CST [31721] LOG: redirecting log output to logging collector process
2022-12-16 12:34:36.874 CST [31721] HINT: Future log output will appear in directory "sys_log".
done
server started
[kingbase@postgres ~]$ ksql -Usystem -dtest
ksql (V8.0)
Type "help" for help. test=# select count(*) from tb;
count
-------
40
(1 row) 经确认进行删除表数据操作后并且手动执行checkpoint命令后,通过sys_resetwal工具也是可以恢复被删除的数据。
  1. 恢复被删除的数据并且执行了Vacuum操作,数据是否可以被恢复
--启动pg_data数据库
[kingbase@postgres ~]$ sys_ctl -D /home/kingbase/pg_data/ start
waiting for server to start....2022-12-16 12:13:51.096 CST [30580] LOG: sepapower extension initialized
2022-12-16 12:13:51.101 CST [30580] LOG: starting KingbaseES V008R006C006B0021 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-46), 64-bit
2022-12-16 12:13:51.101 CST [30580] LOG: listening on IPv4 address "0.0.0.0", port 54321
2022-12-16 12:13:51.101 CST [30580] LOG: listening on IPv6 address "::", port 54321
2022-12-16 12:13:51.105 CST [30580] LOG: listening on Unix socket "/tmp/.s.KINGBASE.54321"
2022-12-16 12:13:51.142 CST [30580] LOG: redirecting log output to logging collector process
2022-12-16 12:13:51.142 CST [30580] HINT: Future log output will appear in directory "sys_log".
done
server started --登录数据库删除TB表 [kingbase@postgres ~]$ ksql -Usystem -dtest
ksql (V8.0)
Type "help" for help. test=# drop table tb;
DROP TABLE --创建表
CREATE TABLE TB (
"MENU_ID" numeric(8,0) NOT NULL,
"SYSTEM_CODE" character varying(15) NULL,
"ORDER_NO" character varying(5) NULL,
"TITLE" character varying(50) NULL,
"OBJECT_CODE" character varying(12) NULL,
"ICO" character varying(36) NULL,
"ICO_LAYOUT" character varying(10) NULL,
"MENU_ID_PARENT" numeric(8,0) NOT NULL DEFAULT 0
);
--插入数据
INSERT INTO TB ("MENU_ID","SYSTEM_CODE","ORDER_NO","TITLE","OBJECT_CODE","ICO","ICO_LAYOUT","MENU_ID_PARENT") VALUES
(1320,'SYS','4D1','药品黑白名单管理','PASS-W01',NULL,NULL,1319),
(1321,'SYS','4D2','药品限制使用设置','PASS-W03',NULL,NULL,1319),
(293,'SYS','3843','医嘱排斥项设置','ORDER-W05',NULL,NULL,260),
(294,'SYS','385','化验医嘱',NULL,NULL,NULL,515),
(295,'SYS','3851','化验容器设置','ASSAY-W01',NULL,NULL,294),
(296,'SYS','3852','化验标本类型设置','ASSAY-W02',NULL,NULL,294),
(297,'SYS','3853','化验报告取单时间设置','ASSAY-W03',NULL,NULL,294),
(300,'SYS','10','功能相关',NULL,NULL,NULL,299),
(301,'SYS','101','系统对象设置','SYS-W01',NULL,NULL,300),
(302,'SYS','102','系统菜单设置','SYS-W02',NULL,NULL,300);
INSERT INTO TB ("MENU_ID","SYSTEM_CODE","ORDER_NO","TITLE","OBJECT_CODE","ICO","ICO_LAYOUT","MENU_ID_PARENT") VALUES
(303,'SYS','103','系统平台功能设置','SYS-W03',NULL,NULL,300),
(1172,'SYS','150','系统报表设置','RPT-W11',NULL,NULL,1171),
(1173,'SYS','151','病人出院列表','RPT-W01',NULL,NULL,1171),
(1174,'SYS','152','费用统计','RPT-W01',NULL,NULL,1171),
(1175,'SYS','153','医院业务统计报表','RPT-W01',NULL,NULL,1171),
(663,'SYS','3B6','体温单格式配置','ENR-W14',NULL,NULL,373),
(1176,'SYS','154','医院业务统计报表(病区)','RPT-W01',NULL,NULL,1171),
(665,'SYS','316','病案诊断类型设置','DIAG-W04',NULL,NULL,613),
(666,'SYS','132','报表格式打印机设置','SYS1-W04',NULL,NULL,405),
(669,'KTCIS_IDW','57','住院病历质量检查评分','EMR-W21',NULL,NULL,239);
INSERT INTO TB ("MENU_ID","SYSTEM_CODE","ORDER_NO","TITLE","OBJECT_CODE","ICO","ICO_LAYOUT","MENU_ID_PARENT") VALUES
(673,'SYS','4S','包药机相关设置',NULL,NULL,NULL,516),
(674,'SYS','4S1','包药机设置','MED-W05',NULL,NULL,673),
(373,'SYS','3B','电子护理记录基础设置',NULL,NULL,NULL,517),
(675,'SYS','4M','摆药包药相关设置',NULL,NULL,NULL,516),
(676,'SYS','4M0','药品包药字典设置','PACK_MED-W01',NULL,NULL,675),
(304,'SYS','104','系统参数设置','SYS-W04',NULL,NULL,300),
(305,'SYS','105','个人参数设置','SYS-W05',NULL,NULL,300),
(306,'SYS','106','机器参数设置','SYS-W06',NULL,NULL,300),
(307,'SYS','107','系统机器设置','SYS-W07',NULL,NULL,300),
(308,'SYS','108','通知发送','SYS-W08',NULL,NULL,300);
INSERT INTO TB ("MENU_ID","SYSTEM_CODE","ORDER_NO","TITLE","OBJECT_CODE","ICO","ICO_LAYOUT","MENU_ID_PARENT") VALUES
(309,'SYS','109','系统文件发布','SYS-W09',NULL,NULL,300),
(310,'SYS','10A','系统文件查询','SYS-W10',NULL,NULL,300),
(311,'SYS','10B','带功能的窗体测试','SYS-W99',NULL,NULL,300),
(312,'SYS','11','行政组织',NULL,NULL,NULL,299),
(325,'SYS','12','医院基础设置',NULL,NULL,NULL,299),
(326,'SYS','311','数据字典设置','PUB2-W01',NULL,NULL,613),
(327,'SYS','121','行政区域设置','PUB2-W02',NULL,NULL,325),
(329,'SYS','1251','自定义字段设置','PUB2-W05',NULL,NULL,614),
(330,'SYS','124','医疗工作点设置','PUB2-W06',NULL,NULL,325),
(331,'SYS','1252','系统条件参数设置','PUB2-W07',NULL,NULL,614);

​ 5.1 确认insert后当前事务日志及wal事务号

select pg_current_wal_lsn(),pg_walfile_name(pg_current_wal_lsn()),pg_walfile_name_offset(pg_current_wal_lsn());
--查询insert后,delete前的当前事务日志及事务(LSN)号
test=# select pg_current_wal_lsn(),pg_walfile_name(pg_current_wal_lsn()),pg_walfile_name_offset(pg_current_wal_lsn());
pg_current_wal_lsn | pg_walfile_name | pg_walfile_name_offset
--------------------+--------------------------+------------------------------------
0/17D4E28 | 000000010000000000000001 | (000000010000000000000001,8212008)
(1 row)
test=# select count(*) from tb;
count
-------
40
(1 row)

​ 5.2 进行删除数据操作并且手动执行vacuum

test=# delete from tb;
DELETE 40
test=# vacuum tb;
VACUUM
test=# select count(*) from tb;
count
-------
0
(1 row) --查询删除数据并且手动执行checkpoint命令后,当前事务日志及事务(LSN)号 test=# select pg_current_wal_lsn(),pg_walfile_name(pg_current_wal_lsn()),pg_walfile_name_offset(pg_current_wal_lsn());
pg_current_wal_lsn | pg_walfile_name | pg_walfile_name_offset
--------------------+--------------------------+------------------------------------
0/17D9728 | 000000010000000000000001 | (000000010000000000000001,8230696)
(1 row)

​ 5.3 根据查询到的事务号以及日志文件,查找WAL日志确定恢复数据的范围

--通过wal日志事物号找到delete的大概位置,这里位置是 564

[kingbase@postgres sys_wal]$ pwd
/home/kingbase/pg_data/sys_wal
[kingbase@postgres sys_wal]$ ls -l
total 16384
-rw------- 1 kingbase kingbase 16777216 Dec 16 14:10 000000010000000000000001
drwx------ 2 kingbase kingbase 6 Dec 16 10:26 archive_status --如果删除数据量大,涉及多个wal日志,也可以指定事务日志起始wal文件名,结束wal日志文件名称
--示例 sys_waldump 000000010000000000000001 000000010000000000000009 -s 0/17AF9D0 [kingbase@postgres sys_wal]$ sys_waldump 000000010000000000000001 -s 0/17D4E28
rmgr: Heap len (rec/tot): 59/ 6339, tx: 564, lsn: 0/017D4E28, prev 0/017D4DF8, desc: DELETE off 41 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0 FPW
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6708, prev 0/017D4E28, desc: DELETE off 42 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6740, prev 0/017D6708, desc: DELETE off 43 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6778, prev 0/017D6740, desc: DELETE off 44 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D67B0, prev 0/017D6778, desc: DELETE off 45 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D67E8, prev 0/017D67B0, desc: DELETE off 46 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6820, prev 0/017D67E8, desc: DELETE off 47 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6858, prev 0/017D6820, desc: DELETE off 48 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6890, prev 0/017D6858, desc: DELETE off 49 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D68C8, prev 0/017D6890, desc: DELETE off 50 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6900, prev 0/017D68C8, desc: DELETE off 51 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6938, prev 0/017D6900, desc: DELETE off 52 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6970, prev 0/017D6938, desc: DELETE off 53 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D69A8, prev 0/017D6970, desc: DELETE off 54 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D69E0, prev 0/017D69A8, desc: DELETE off 55 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6A18, prev 0/017D69E0, desc: DELETE off 56 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6A50, prev 0/017D6A18, desc: DELETE off 57 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6A88, prev 0/017D6A50, desc: DELETE off 58 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6AC0, prev 0/017D6A88, desc: DELETE off 59 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6AF8, prev 0/017D6AC0, desc: DELETE off 60 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6B30, prev 0/017D6AF8, desc: DELETE off 61 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6B68, prev 0/017D6B30, desc: DELETE off 62 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6BA0, prev 0/017D6B68, desc: DELETE off 63 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6BD8, prev 0/017D6BA0, desc: DELETE off 64 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6C10, prev 0/017D6BD8, desc: DELETE off 65 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6C48, prev 0/017D6C10, desc: DELETE off 66 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6C80, prev 0/017D6C48, desc: DELETE off 67 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6CB8, prev 0/017D6C80, desc: DELETE off 68 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6CF0, prev 0/017D6CB8, desc: DELETE off 69 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6D28, prev 0/017D6CF0, desc: DELETE off 70 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6D60, prev 0/017D6D28, desc: DELETE off 71 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6D98, prev 0/017D6D60, desc: DELETE off 72 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6DD0, prev 0/017D6D98, desc: DELETE off 73 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6E08, prev 0/017D6DD0, desc: DELETE off 74 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6E40, prev 0/017D6E08, desc: DELETE off 75 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6E78, prev 0/017D6E40, desc: DELETE off 76 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6EB0, prev 0/017D6E78, desc: DELETE off 77 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6EE8, prev 0/017D6EB0, desc: DELETE off 78 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6F20, prev 0/017D6EE8, desc: DELETE off 79 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6F58, prev 0/017D6F20, desc: DELETE off 80 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Transaction len (rec/tot): 34/ 34, tx: 564, lsn: 0/017D6F90, prev 0/017D6F58, desc: COMMIT 2022-12-16 14:10:35.799685 CST
rmgr: Standby len (rec/tot): 46/ 46, tx: 0, lsn: 0/017D6FB8, prev 0/017D6F90, desc: RUNNING_XACTS nextXid 565 latestCompletedXid 24997776 oldestRunningXid 564
rmgr: Heap2 len (rec/tot): 214/ 214, tx: 0, lsn: 0/017D6FE8, prev 0/017D6FB8, desc: CLEAN remxid 564, blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap2 len (rec/tot): 214/ 214, tx: 0, lsn: 0/017D70C0, prev 0/017D6FE8, desc: CLEAN remxid 564, blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap2 len (rec/tot): 64/ 8256, tx: 0, lsn: 0/017D7198, prev 0/017D70C0, desc: VISIBLE cutoff xid 0 flags 0x03, blkref #0: rel 1663/13857/16388 fork vm blk 0 FPW, blkref #1: rel 1663/13857/16388 blk 0
rmgr: Standby len (rec/tot): 42/ 42, tx: 565, lsn: 0/017D91F0, prev 0/017D7198, desc: LOCK xid 565 db 13857 rel 16388
rmgr: Storage len (rec/tot): 46/ 46, tx: 565, lsn: 0/017D9220, prev 0/017D91F0, desc: TRUNCATE base/13857/16388 to 0 blocks flags 7
rmgr: Heap len (rec/tot): 53/ 1081, tx: 565, lsn: 0/017D9250, prev 0/017D9220, desc: INPLACE off 3, blkref #0: rel 1663/13857/1259 blk 0 FPW
rmgr: Transaction len (rec/tot): 98/ 98, tx: 565, lsn: 0/017D9690, prev 0/017D9250, desc: COMMIT 2022-12-16 14:10:46.815167 CST; inval msgs: catcache 62 catcache 61 relcache 16388
rmgr: Standby len (rec/tot): 42/ 42, tx: 0, lsn: 0/017D96F8, prev 0/017D9690, desc: RUNNING_XACTS nextXid 566 latestCompletedXid 25007760 oldestRunningXid 566
sys_waldump: fatal: error in WAL record at 0/17D96F8: invalid record length at 0/17D9728: wanted 24, got 0 --通过以下命令进行在wal日志精确定位事务号 rmgr: Heap len (rec/tot): 59/ 6339, tx: 564, lsn: 0/017D4E28, prev 0/017D4DF8, desc: DELETE off 41 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0 FPW
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6708, prev 0/017D4E28, desc: DELETE off 42 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6740, prev 0/017D6708, desc: DELETE off 43 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6778, prev 0/017D6740, desc: DELETE off 44 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D67B0, prev 0/017D6778, desc: DELETE off 45 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D67E8, prev 0/017D67B0, desc: DELETE off 46 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6820, prev 0/017D67E8, desc: DELETE off 47 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6858, prev 0/017D6820, desc: DELETE off 48 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6890, prev 0/017D6858, desc: DELETE off 49 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D68C8, prev 0/017D6890, desc: DELETE off 50 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6900, prev 0/017D68C8, desc: DELETE off 51 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6938, prev 0/017D6900, desc: DELETE off 52 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6970, prev 0/017D6938, desc: DELETE off 53 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D69A8, prev 0/017D6970, desc: DELETE off 54 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D69E0, prev 0/017D69A8, desc: DELETE off 55 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6A18, prev 0/017D69E0, desc: DELETE off 56 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6A50, prev 0/017D6A18, desc: DELETE off 57 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6A88, prev 0/017D6A50, desc: DELETE off 58 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6AC0, prev 0/017D6A88, desc: DELETE off 59 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6AF8, prev 0/017D6AC0, desc: DELETE off 60 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6B30, prev 0/017D6AF8, desc: DELETE off 61 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6B68, prev 0/017D6B30, desc: DELETE off 62 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6BA0, prev 0/017D6B68, desc: DELETE off 63 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6BD8, prev 0/017D6BA0, desc: DELETE off 64 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6C10, prev 0/017D6BD8, desc: DELETE off 65 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6C48, prev 0/017D6C10, desc: DELETE off 66 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6C80, prev 0/017D6C48, desc: DELETE off 67 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6CB8, prev 0/017D6C80, desc: DELETE off 68 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6CF0, prev 0/017D6CB8, desc: DELETE off 69 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6D28, prev 0/017D6CF0, desc: DELETE off 70 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6D60, prev 0/017D6D28, desc: DELETE off 71 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6D98, prev 0/017D6D60, desc: DELETE off 72 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6DD0, prev 0/017D6D98, desc: DELETE off 73 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6E08, prev 0/017D6DD0, desc: DELETE off 74 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6E40, prev 0/017D6E08, desc: DELETE off 75 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6E78, prev 0/017D6E40, desc: DELETE off 76 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6EB0, prev 0/017D6E78, desc: DELETE off 77 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6EE8, prev 0/017D6EB0, desc: DELETE off 78 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6F20, prev 0/017D6EE8, desc: DELETE off 79 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6F58, prev 0/017D6F20, desc: DELETE off 80 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0

​ 5.4 进行恢复删除数据操作

--停止数据库

[kingbase@postgres ~]$ sys_ctl -D pg_data/ stop
waiting for server to shut down.... done
server stopped --使用cp对当前data目录进行备份
[kingbase@postgres ~]$ cp -r pg_data/ pg_data_bak
[kingbase@postgres ~]$ ls -l
total 808
drwxrwxr-x 3 kingbase kingbase 16 Nov 10 01:12 V8R6C6B21
-rwxr-xr-x 1 kingbase kingbase 3276 Nov 10 00:31 license.dat
-rw-rw-r-- 1 kingbase kingbase 252 Dec 6 16:48 my_test.c
-rw-rw-r-- 1 kingbase kingbase 10403 Dec 13 18:21 nohup.out
drwx------ 22 kingbase kingbase 4096 Dec 16 10:26 ora_data
drwx------ 23 kingbase kingbase 4096 Dec 16 14:17 pg_data
drwx------ 23 kingbase kingbase 4096 Dec 16 14:17 pg_data_bak --使用备份的data目录,通过指定事务号进行删除数据的恢复。
--使用sys_resetwal工具指定事务号552进行删除数据的恢复 [kingbase@postgres ~]$ sys_resetwal -x 564 /home/kingbase/pg_data_bak/
Write-ahead log reset --启动通过cp备份的数据库查询数据是否被恢复
[kingbase@postgres ~]$ sys_ctl -D /home/kingbase/pg_data_bak/ start
waiting for server to start....2022-12-16 14:20:32.872 CST [5064] LOG: sepapower extension initialized
2022-12-16 14:20:32.875 CST [5064] LOG: starting KingbaseES V008R006C006B0021 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-46), 64-bit
2022-12-16 14:20:32.876 CST [5064] LOG: listening on IPv4 address "0.0.0.0", port 54321
2022-12-16 14:20:32.876 CST [5064] LOG: listening on IPv6 address "::", port 54321
2022-12-16 14:20:32.879 CST [5064] LOG: listening on Unix socket "/tmp/.s.KINGBASE.54321"
2022-12-16 14:20:32.915 CST [5064] LOG: redirecting log output to logging collector process
2022-12-16 14:20:32.915 CST [5064] HINT: Future log output will appear in directory "sys_log".
done
server started
[kingbase@postgres ~]$ ksql -Usystem -dtest
ksql (V8.0)
Type "help" for help. test=# select count(*) from tb;
count
-------
0
(1 row) 经确认,如果数据被删除并且手动执行了vacuum或者系统自动触发了vacuum操作,被删除的数据是无法被恢复。 --在实际的场景中需要留意在被删除数据事务号后wal日志是否有rmgr: Heap2或者CLEAN remxid 564(xid)记录.
--资源rmgr Heap2的记录与vacuum有关,这里是测试表TB的 vacuum操作 test=# select pg_relation_filepath('tb');
pg_relation_filepath
----------------------
base/13857/16388
(1 row) test=# select pg_relation_filenode('tb');
pg_relation_filenode
----------------------
16388
(1 row) rmgr: Heap len (rec/tot): 54/ 54, tx: 564, lsn: 0/017D6F58, prev 0/017D6F20, desc: DELETE off 80 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/13857/16388 blk 0
rmgr: Transaction len (rec/tot): 34/ 34, tx: 564, lsn: 0/017D6F90, prev 0/017D6F58, desc: COMMIT 2022-12-16 14:10:35.799685 CST
rmgr: Standby len (rec/tot): 46/ 46, tx: 0, lsn: 0/017D6FB8, prev 0/017D6F90, desc: RUNNING_XACTS nextXid 565 latestCompletedXid 24997776 oldestRunningXid 564
rmgr: Heap2 len (rec/tot): 214/ 214, tx: 0, lsn: 0/017D6FE8, prev 0/017D6FB8, desc: CLEAN remxid 564, blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap2 len (rec/tot): 214/ 214, tx: 0, lsn: 0/017D70C0, prev 0/017D6FE8, desc: CLEAN remxid 564, blkref #0: rel 1663/13857/16388 blk 0
rmgr: Heap2 len (rec/tot): 64/ 8256, tx: 0, lsn: 0/017D7198, prev 0/017D70C0, desc: VISIBLE cutoff xid 0 flags 0x03, blkref #0: rel 1663/13857/16388 fork vm blk 0 FPW, blkref #1: rel 1663/13857/16388 blk 0
rmgr: Standby len (rec/tot): 42/ 42, tx: 565, lsn: 0/017D91F0, prev 0/017D7198, desc: LOCK xid 565 db 13857 rel 16388
rmgr: Storage len (rec/tot): 46/ 46, tx: 565, lsn: 0/017D9220, prev 0/017D91F0, desc: TRUNCATE base/13857/16388 to 0 blocks flags 7
  1. 通过SYS_RESETWAL工具恢复被删除数据的测试总结:

​ 6.1. 数据被删除后,未执行checkpoint,vacuum操作,被删除数据可以通过sys_resetwal工具恢复。(前提是WAL日志文件存在且文件未损坏)。

​ 6.2. 数据被删除后,执行checkpoint,未进行vacuum操作,被删除数据可以通过sys_resetwal工具恢复。(前提是WAL日志文件存在且文件未损坏)。

​ 6.3. 数据被删除后,不管是手动执行还是系统自动执行vacuum操作,被删除的数据都无法通过sys_resetwal工具恢复。

注意:在进行了sys_resetwal操作后,设置的LSN 号后的数据都取消了,相当于回滚到指定的事务LSN 号的位置。(在生产进行此操作必须先备份data目录,然后在备份的data目录进行操作,一定要谨慎谨慎再谨慎)

SYS_RESETWAL操作会把DATA目录在线WAL 日志进行清理,原有的日志都会被清除

恢复之前的wal日志

 [kingbase@postgres sys_wal]$ ll
total 98304
-rw------- 1 kingbase kingbase 16777216 Nov 11 12:15 000000010000000000000007
-rw------- 1 kingbase kingbase 16777216 Nov 11 12:11 000000010000000000000008
-rw------- 1 kingbase kingbase 16777216 Nov 11 12:11 000000010000000000000009
-rw------- 1 kingbase kingbase 16777216 Nov 11 12:11 00000001000000000000000A
-rw------- 1 kingbase kingbase 16777216 Nov 11 12:11 00000001000000000000000B
-rw------- 1 kingbase kingbase 16777216 Nov 11 12:11 00000001000000000000000C
drwx------ 2 kingbase kingbase 6 Nov 11 12:08 archive_status

恢复之后的日志:

[kingbase@postgres sys_wal]$ ll
total 16384
-rw------- 1 kingbase kingbase 16777216 Nov 11 13:17 00000001000000000000000D
drwx------ 2 kingbase kingbase 6 Nov 11 12:08 archive_status
[kingbase@postgres sys_wal]$

WAL日志切换

自之前的WAL日志文件切换以后如果没有新的WAL日志生成,就不会创建新的 WAL 日志文件,也就是说手动切换wal日志,需要有新数据生成才会发生切换,如果没有新数据生成,手动执行sys_switch_wal也无法进行wal日志切换。
有新的数据产生,执行sys_switch_wal就可以切换wal日志:
SELECT sys_walfile_name(sys_switch_wal()), now(), sys_walfile_name(sys_switch_wal());

KingbaseES恢复被删除数据的更多相关文章

  1. Oracle恢复已删除数据

    Oracle恢复已删除的数据,主要有两种方法:SCN和时间戳. 一.通过SCN恢复删除且已提交的数据 1.获得当前数据库的scn号 select current_scn from v$database ...

  2. oracle 恢复table删除数据 恢复package(使用闪回)

    好久没写东西了,今天写一篇凑个数吧,来公司一年多了,感觉自己到了一个小瓶颈期了. 以前每天很多新东西,都是忙着学,感觉没时间写博客总结一下,大部分都是写笔记,现在又是没东西可以写,每天干着95%都是重 ...

  3. Oracle闪回查询恢复delete删除数据

    Flashback query(闪回查询)原理 Oracle根据undo信息,利用undo数据,类似一致性读取方法,可以把表置于一个删除前的时间点(或SCN),从而将数据找回. Flashback q ...

  4. Cassandra1.2文档学习(11)—— 删除数据

    参考文档:http://www.datastax.com/documentation/cassandra/1.2/webhelp/index.html#cassandra/dml/dml_about_ ...

  5. Oracle 恢复被删除的数据,解决误操作删除数据

    在删除数据的时候不小心,把delete语句执行错了,把别的表给delete,而且还执行了commit!真汗.......数据是相当的重要........废话少说了!赶快找方法吧: 第一种: 1.打开F ...

  6. linux删除数据文件无备份恢复

    参考 : http://www.lunar2013.com/2013/06/linux-%E8%AF%AF%E5%88%A0%E9%99%A4%E6%96%87%E4%BB%B6%E6%81%A2%E ...

  7. Oracle根据时间恢复已删除提交的数据

    Oracle 根据数据库的时间戳恢复已删除提交的数据 1.选择一个时间点查看表中数据是否是自己想要的数据 Select * from 表名 as of timestamp to_timestamp(‘ ...

  8. Oracle的闪回特性之恢复truncate删除表的数据

    Oracle的闪回特性之恢复truncate删除表的数据 SQL> show parameter flashback NAME                                 T ...

  9. 一句DELETE引发的加班(Mysql 恢复Delete删除的数据)

    本机用的Navicat连mysql测试DB又连了正式DB,因为本地与正式要频繁操作所以都打开了很多查询,本来要DELETE删除测试DB的数据,没看清在正式环境执行了.共删除了325条数据,然后在网上找 ...

  10. window下Mysql 恢复Delete删除的数据

    转载:https://www.cnblogs.com/q149072205/p/11940591.html 本机用的Navicat连mysql测试DB又连了正式DB,因为本地与正式要频繁操作所以都打开 ...

随机推荐

  1. Codeforces Round #802 (Div. 2)C. Helping the Nature(差分)

    题目链接 题目大意: 给你一个有n个元素的数组a,你可以通过一下三种操作使数组的每一个值都为0: 选择一个下标i,然后让a[1],a[2]....a[ i ] 都减一; 选择一个下标i,然后让a[i] ...

  2. 幂次方表达:p1010

    1 题目ID: P1010 [NOIP1998 普及组] 幂次方 2 题目描述: 任何一个正整数都可以用 22 的幂次方表示.例如 137=2^7+2^3+2^0137=27+23+20. 同时约定方 ...

  3. [ERROR] mariadbd: The table 'INNODB_BUFFER_PAGE' is full

    问题描述:将information_schema导出sql文件到新库中恢复,sql中的表都是临时表,存储引擎都是memory,在导入的过程中实际大量会占用临时表. 报错信息:ERROR 1114 (H ...

  4. vue项目中,alert使用

    methods:{ sendData() { ··· // 中间省略的代码 alert(`发送成功,连接失败的号码有:${failList}`) // failList里存的是后台返回的数据 } }

  5. -webkit-box-orient:vertical 编译报错之autoprefixer问题

    由于各大浏览器的兼容问题,autoprefixer 插件 就可以帮我们自动补齐前缀.它和 less.scss 这样的预处理器不同,它属于后置处理器. 预处理器:在打包之前进行处理 后置处理器:在代码打 ...

  6. Windows10下python3和python2同时安装(一)安装python3和python2

    Windows10下python3和python2同时安装(一) 安装python3和python2 特别说明,本文是在Windows64位系统下进行的,32位系统请下载相应版本的安装包,安装方法类似 ...

  7. <七>lambda表达式实现原理

    C++11 函数对象的升级版=>lambda表达式 函数对象的缺点: 使用在泛型算法,参数传递, 比较性质/自定义操作 优先级队列, 需要专门定义出一个类 //lambda表达式语法: //[捕 ...

  8. python 异步写入文件

    # -*- coding:utf-8 -*-import asyncioimport aiofilesimport time#异步操作时,函数名前必须加上asyncasync def func1(): ...

  9. 持续发烧,试试Dart语言的异步操作,效率提升500%

    前言 昨天发了篇文章<Dart开发服务端,我是不是发烧(骚)了>,承蒙小编看得起上了首页. 今天持续发烧,再来写写如何使用 Dart 语言的异步操作.说起异步操作,玩 NodeJS 的同学 ...

  10. 成功解决pycharm 的setting中的Error occurred when installing package 'Keras'

    成功解决pycharm 的setting中的Error occurred when installing package 'Keras' 刚刚开始学习python在安装package上碰了不上壁. M ...