pg_resetwal(9.6及之前版本叫pg_resetxlog)是用于清理不需要的WAL文件,还可以用于篡改当前事务ID,可以恢复未被vacuum回收的元组。
语法:
pg_resetwal [OPTION]… DATADIR
参数:
-c, --commit-timestamp-ids=XID,XID
[-D, --pgdata=]DATADIR:数据目录路径
-e, --epoch=XIDEPOCH:epoch xid
-l, --next-wal-file=WALFILE:下一个WAL文件名称
-m, --multixact-ids=MXID,MXID:设置下一个和最老的多事务ID
-n, --dry-run:预执行
-o, --next-oid=OID:设置下一个oid
-O, --multixact-offset=OFFSET:设置下一个多事务偏移量
-V, --version:版本信息
-x, --next-transaction-id=XID:设置下一个事务XID
–wal-segsize=SIZE:WAL segments大小,单位MB
-?, --help:帮助信息
测试:
-- 查看记录的事务号
select xmin,* from aa;
xmin | id | province | city
------+----+----------+--------
600 | 1 | 广东 | 广州
601 | 2 | 广东 | 深圳
603 | 4 | 湖南 | 长沙
606 | 5 | 四川 | 成都
607 | 6 | 湖南 | 怀化
627 | 7 | 陕西 | 西安
628 | 8 | 河南 | 洛阳
629 | 9 | 河南 | 开封
637 | 10 | 黑龙江 | 哈尔滨
638 | 11 | 黑龙江 | 佳木斯
639 | 12 | 甘肃 | 宁夏
(11 rows)
select xmin,* from bb;
xmin | id
------+----
627 | 1
627 | 3
640 | 7
(3 rows)
-- 删除aa表的某条数据
delete from aa where id=7;
DELETE 1
-- 停止pg服务
sudo -u postgres /usr/local/pgsql/bin/pg_ctl -D /data/pg_data/data -l /data/pg_data/log/pg.log stop
waiting for server to shut down.... done
server stopped
-- 执行pg_resetwal
./pg_resetwal -D /data/pg_data/data -x 627
Write-ahead log reset
-- 查看当前的事务xid
txid_current
--------------
627
(1 row)
-- 重新启动并查看,可以发现回退到之前xmin之前,影响的整个实例中的所有事务
postgres=# select xmin,* from aa;
-----+----+----------+------
600 | 1 | 广东 | 广州
601 | 2 | 广东 | 深圳
602 | 3 | 广东 | 佛山
603 | 4 | 湖南 | 长沙
606 | 5 | 四川 | 成都
607 | 6 | 湖南 | 怀化
624 | | 河南 | 开封
627 | 7 | 陕西 | 西安
628 | 8 | 河南 | 洛阳
(9 rows)
select * from bb;
id
----
(0 rows)
预执行信息查看:
./pg_resetwal -D /data/pg_data/data -x 640 -n
Current pg_control values:
pg_control version number: 1100
Catalog version number: 201809051
Database system identifier: 7018374463638270579
Latest checkpoint's TimeLineID: 3
Latest checkpoint's full_page_writes: on
Latest checkpoint's NextXID: 0:629
Latest checkpoint's NextOID: 41209
Latest checkpoint's NextMultiXactId: 1
Latest checkpoint's NextMultiOffset: 0
Latest checkpoint's oldestXID: 561
Latest checkpoint's oldestXID's DB: 13287
Latest checkpoint's oldestActiveXID: 0
Latest checkpoint's oldestMultiXid: 1
Latest checkpoint's oldestMulti's DB: 13287
Latest checkpoint's oldestCommitTsXid:0
Latest checkpoint's newestCommitTsXid:0
Maximum data alignment: 8
Database block size: 8192
Blocks per segment of large relation: 131072
WAL block size: 8192
Bytes per WAL segment: 16777216
Maximum length of identifiers: 64
Maximum columns in an index: 32
Maximum size of a TOAST chunk: 1996
Size of a large-object chunk: 2048
Date/time type storage: 64-bit integers
Float4 argument passing: by value
Float8 argument passing: by value
Data page checksum version: 0
Values to be changed:
First log segment after reset: 00000003000000000000000C
NextXID: 640
OldestXID: 2294967936
OldestXID's DB: 0