关键时候救你命(续): 简要介绍PostgreSQL 插件(2) - walminer4.0 简介

1、前言

上回我们介绍到3.0版的walminer,它的常规功能也差不多足够应付日常碰到的问题了。到了4.0版本,已经变成了闭源的了。这个完全可以理解。毕竟额外开发,需要付出很多艰辛和努力。有时候付出与回报并不成比例,参与过开源开发的朋友一定深有体会。

如果想拿到可用的4.0版本,可以直接联系原作者,获得相应版权。50元的赞助可以拿到Personal License,除了有限速以外,没有别的什么限制,可以终身使用。

4.0声称:之前的walminer版本需要在数据库安装walminer插件生成数据字典后才能完成解析,这种插件的方式可能在部署使用上有些不便,为了安装、部署、使用的方便,walminer4.0改为bin工具,无需在目标数据库安装任何插件。另外walminer工具的编译安装不再依赖任何数据库版本,因此 一份walminer工具可以支持多个PG版本的解析。

2、使用及验证

直接从网站上下载4.3.0:  walminer_x86_64_centos_v4.3.0.tar.gz.  从:https://gitee.com/movead/XLogMiner/tags 上边找到第一行,就是最新的Release版本。

然后,联系作者,弄到license.或者直接试用。

首先解压安装,可将其安装至目录:/var/lib/pgsql/walminer,形成这样的目录结构:(这里安装主要是CentOS发行版,别的linux,如果不能运行,可以联系作者获取。)

[17:06:19-postgres@centos1:/var/lib/pgsql/walminer]$ tree
├── bin
│   └── walminer
├── lib
│   ├── libpq.so -> libpq.so.5.15
│   ├── libpq.so.5 -> libpq.so.5.15
│   └── libpq.so.5.15
├── readme
├── storeimage
├── walminer.license
└── xact

当然,你的license文件不能放到这个位置(这是我一厢情愿),license文件必须放到:/usr/local/walminer下边。

最省事的话,可以把walminer整个直接安装到/usr/local/walminer下边。

配置好环境变量:

export PATH=/var/lib/pgsql/walminer/bin:$PATH
export LD_LIBRARY_PATH=/var/lib/pgsql/walminer/lib:$LD_LIBRARY_PATH

只要license文件放置的正确的话,基本上不会有什么问题。可以简要看看它的帮助信息:

[17:13:55-postgres@centos1:/var/lib/pgsql]$ walminer help
#################################################
Walminer for PostgreSQL wal
Persional License for Sean(iihero@qq.com)
#################################################
walminer [command] [options]
COMMANDS
---------
#wal2sql
  options
    -D dic file for miner
    -a out detail info for catalog change
    -w wal file path to miner
    -t dest of miner result(1 stdout, 2 file3 db)(stdout default)
    -k boundary kind(1 all, 2 lsn, 3 time4 xid)(all default)
    -m miner mode(0 nomal miner, 1 accurate miner)(nomal defaultif k=2
    -r the relname for single table miner
    -s start location if k=2 or k=3or xid if k = 4
          if k=2 default the min lsn of input wals
          if k=3 or k=4 you need input this
    -e end wal location if k=2 or k=3
          if k=2 default the max lsn of input wals
          if k=3 you need input this
    -f file to store miner result if t = 2
    -d target database name if t=3(default postgres)
    -h target database host if t=3(default localhost)
    -p target database port if t=3(default 5432)
    -u target database user if t=3(default postgres)
    -W target user password if t=3
---------
#builtdic
  options
    -d target database name(default postgres)
    -h target database host(default localhost)
    -p target database port(default 5432)
    -u target database user(default postgres)
    -W target user password
    -D dic produce path
    -f rewrite walminer dic if exists
---------
#showdic
  options
    -D dic file to show
---------
#avatardic
  options
    -r avtar rel that new created
    -n avtared relfilenode
    -D avtared walminer dic path
---------
#regress(not support for user)
  options
    -w test database wal path(default postgres)
    -d test database name(default postgres)
    -h test database host(default localhost)
    -p test database port(default 5432)
    -u test database user(default postgres)
    -P apply database port
    -W test user password
---------
#fosync
  options
    -D dic file for miner
    -w wal file path to miner
    -t dest of miner result(1 stdout, 2 file3 db, 4 apply)(stdout default)
    -f file to store miner result if t = 2
    -l lsn it start fync
    -d target database name if t=3 or 4(default postgres)
    -h target database host if t=3 or 4(default localhost)
    -p target database port if t=3 or 4(default 5432)
    -u target database user if t=3 or 4(default postgres)
    -W target user password if t=3 or 4
---------
#pgto
  options
    -c configure path
    -i to init a CDC configure
    -r to run a CDC configure
    Below is needed when -i
    -d source database name(default postgres)
    -h source database host(default localhost)
    -p source database port(default 5432)
    -u source database user(default postgres)
    -w source user password
    -D target database name
    -H target database host
    -P target database port
    -U target database user
    -W target user password
    -K target database type(1 postgres) (support postgres only currently)
    -s slot name need for CDC
---------
#################################################

现在在mydb数据库里头模拟一些操作,比如中途误删了id=4的那条记录,现在要把它找回来。

mydb=# create table t(id int primary key, col2 varchar(32));
CREATE TABLE
mydb=# insert into t values(1, 'wang'), (2, 'AB'), (3, 'CDEF');
INSERT 0 3
mydb=# insert into t values(4, 'missing');
INSERT 0 1
mydb=# insert into t values(5, 'good');
INSERT 0 1
mydb=# delete from t where id = 4;
DELETE 1
mydb=# insert into t select n, 'test' || n from generate_series(6, 20) as n;
INSERT 0 15

与从前一样,使用walminer,建字典

mydb=# select pg_switch_wal();
 pg_switch_wal
---------------
 0/176E8D0
(1 row)
mydb=# checkpoint;
CHECKPOINT
mydb=# select pg_walfile_name('0/176E8D0');
     pg_walfile_name
--------------------------
 000000010000000000000001
(1 row)

1) 先建字典

walminer builtdic -D ~/walminer.dic -f -h localhost -p 5555 -d mydb -u postgres -W 
#################################################
Walminer for PostgreSQL wal
Persional License for Sean(iihero)
#################################################
DIC INFO#
sysid:7217379317242387276 dboid:14486 timeline:1 dbversion:140007 walminer:4.3
2) 加入解析
 walminer wal2sql -D ~/walminer.dic -w /var/lib/pgsql/14/data/pg_wal
#################################################
Walminer for PostgreSQL wal

后这紧跟着解析的内容在控制台打出来:
#################################################
[XID]=738, [TOPXID]=0
[SQLNO]=1
[SQL]=INSERT INTO public.t(id ,col2) VALUES(1 ,'wang')
[UNDO]=DELETE FROM public.t WHERE id=1
[COMPLETE]=true
[LSN]=0/176dbe0
[COMMITLSN]=0/176ddc0
[COMMITTIME]=2023-04-02 09:18:33.128534+00
------------------------------------------------------
[XID]=738, [TOPXID]=0
[SQLNO]=2
[SQL]=INSERT INTO public.t(id ,col2) VALUES(2 ,'AB')
[UNDO]=DELETE FROM public.t WHERE id=2
[COMPLETE]=true
[LSN]=0/176dcc0
[COMMITLSN]=0/176ddc0
[COMMITTIME]=2023-04-02 09:18:33.128534+00
------------------------------------------------------
[XID]=738, [TOPXID]=0
[SQLNO]=3
[SQL]=INSERT INTO public.t(id ,col2) VALUES(3 ,'CDEF')
[UNDO]=DELETE FROM public.t WHERE id=3
[COMPLETE]=true
[LSN]=0/176dd40
[COMMITLSN]=0/176ddc0
[COMMITTIME]=2023-04-02 09:18:33.128534+00
------------------------------------------------------
[XID]=739, [TOPXID]=0
[SQLNO]=1
[SQL]=INSERT INTO public.t(id ,col2) VALUES(4 ,'missing')
[UNDO]=DELETE FROM public.t WHERE id=4
[COMPLETE]=true
[LSN]=0/176de20
[COMMITLSN]=0/176dea8
[COMMITTIME]=2023-04-02 09:18:44.791551+00
------------------------------------------------------
[XID]=740, [TOPXID]=0
[SQLNO]=1
[SQL]=INSERT INTO public.t(id ,col2) VALUES(5 ,'good')
[UNDO]=DELETE FROM public.t WHERE id=5
[COMPLETE]=true
[LSN]=0/176ded0
[COMMITLSN]=0/176df50
[COMMITTIME]=2023-04-02 09:18:52.956721+00
------------------------------------------------------
[XID]=741, [TOPXID]=0
[SQLNO]=1
[SQL]=DELETE FROM public.t WHERE id=4
[UNDO]=INSERT INTO public.t(id ,col2) VALUES(4 ,'missing')
[COMPLETE]=true
[LSN]=0/176dfb0
[COMMITLSN]=0/176dfe8
[COMMITTIME]=2023-04-02 09:18:59.009109+00
------------------------------------------------------
[XID]=742, [TOPXID]=0
[SQLNO]=1
[SQL]=INSERT INTO public.t(id ,col2) VALUES(6 ,'test6')
[UNDO]=DELETE FROM public.t WHERE id=6
[COMPLETE]=true
[LSN]=0/176e060
[COMMITLSN]=0/176e858
[COMMITTIME]=2023-04-02 09:20:16.096341+00
.......
------------------------------------------------------

如果加上参数 -t 3,表示输出为第一种类型,输出到表。详细说明如下:

walminer wal2sql [options]

-D 数据字典文件路径

-w 需要解析的wal所在的目录

-t 解析结果输出位置(1 标准输出, 2 输出到文件, 3 输出到数据库)(默认1)

-k 解析范围类型(1 解析所有, 2 指定lsn范围, 3 指定时间范围, 4 指定事务ID)(默认1)

-m 解析模式(0 普通模式:对-s指定的范围进行解析; 1 精确模式:k大于1时保证-s指定范围内数据的完全解析)(nomal default)

-r 单表解析的表名

-s 当k=2时为开始lsn; 当k=3时为开始时间;当k=4时为xid

-e 当k=2时为结束lsn; 当k=3时为结束时间;

-f 当t=2时指定解析结果的输出文件

-d 当t=3时指定目标数据库的数据库名(default postgres)

-h 当t=3时指定目标数据库的地址(default localhost)

-p 当t=3时指定目标数据库的端口(default 5432)

-u 当t=3时指定目标数据库的连接用户名(default postgres)

-W 当t=3时指定目标数据库的连接用户的密码
walminer wal2sql -D ~/walminer.dic -w /var/lib/pgsql/14/data/pg_wal -t 3 -p 5555 -u postgres -W  -d mydb
#################################################
Walminer for PostgreSQL wal
Persional License for Sean(iihero)
#################################################
NOTICE:  table "walminer_contents" does not exist, skipping

指定-t 3时,后边也需要带上连接信息,否则表建不成了。最后有一张表walminer_contents建在目标数据库mydb里头。

我们再连上数据库,就可以查到对应的误删的那条信息了。 不再缀述。

这里只是介绍最基本的用法。还有很多骚操作用法,可以看下那个工程的官网上的说明。都比较详细。

参考:

  1.  https://gitee.com/movead/XLogMiner

  2. 关键时候救你命: 简要介绍PostgreSQL 插件(2) - walminer - 抢救误删数据 (https://mp.weixin.qq.com/s/JPIQ6ybkfEhDM7y2A2ReMA)

  3. walminer4.0使用方法简介 (https://zhuanlan.zhihu.com/p/581671637)

  ---END---

个人观点:对于开源项目,多多支持,同时也尊重项目作者的辛勤劳动。
严格遵守开源license中的相关规则,这样才能形成良性互动。

请使用浏览器的分享功能分享到微信等