1. 工具
a. pg10版本以前使用pg_resetxlog工具
b. pg10及以后版本pg_resetwal工具
2. 命令语法
Usage:
pg_resetwal [OPTION]... DATADIR
Options:
-c, --commit-timestamp-ids=XID,XID
set oldest and newest transactions bearing
commit timestamp (zero means no change)
[-D, --pgdata=]DATADIR data directory
-e, --epoch=XIDEPOCH set next transaction ID epoch
-f, --force force update to be done
-l, --next-wal-file=WALFILE set minimum starting location for new WAL
-m, --multixact-ids=MXID,MXID set next and oldest multitransaction ID
-n, --dry-run no update, just show what would be done
-o, --next-oid=OID set next OID
-O, --multixact-offset=OFFSET set next multitransaction offset
-u, --oldest-transaction-id=XID set oldest transaction ID
-V, --version output version information, then exit
-x, --next-transaction-id=XID set next transaction ID
--wal-segsize=SIZE size of WAL segments, in megabytes
-?, --help show this help, then exit
3. 重建控制具体操作
a. 模拟pg_control文件丢失
[postgres@pg15tar global]$ mv pg_control pg_control.bak
[postgres@pg15tar global]$ pg_ctl start
waiting for server to start....postgres: could not find the database system
Expected to find it in the directory "/u01/app/postgresql/pgdata",
but could not open file "/u01/app/postgresql/pgdata/global/pg_control": No such file or directory
stopped waiting
pg_ctl: could not start server
Examine the log output.
b. 获取-l参数
-l XLOGFILE: force minimum WAL starting location for new transaction log(XLOGFILE强制新事务日志的最小WAL起始位置)
定位办法:pg_wal下面创建的最大的日志文件,编号+1
[postgres@pg15tar pgdata]$ ll pg_wal
total 114712
-rw------- 1 postgres postgres 41 Aug 4 12:46 00000002.history
-rw------- 1 postgres postgres 93 Aug 4 12:46 00000003.history
-rw------- 1 postgres postgres 348 Aug 4 12:46 0000000400000000000000AF.00000028.backup
-rw------- 1 postgres postgres 145 Aug 5 08:27 00000004.history
-rw------- 1 postgres postgres 16777216 Aug 5 12:09 0000000500000000000000B1
-rw------- 1 postgres postgres 16777216 Aug 4 12:46 0000000500000000000000B2
-rw------- 1 postgres postgres 16777216 Aug 4 12:46 0000000500000000000000B3
-rw------- 1 postgres postgres 16777216 Aug 4 12:46 0000000500000000000000B4
-rw------- 1 postgres postgres 16777216 Aug 4 12:46 0000000500000000000000B5
-rw------- 1 postgres postgres 16777216 Aug 4 12:46 0000000500000000000000B6
-rw------- 1 postgres postgres 16777216 Aug 5 08:27 0000000500000000000000B7
-rw------- 1 postgres postgres 197 Aug 5 11:02 00000005.history
drwx------ 2 postgres postgres 4096 Aug 5 12:09 archive_status
那么这里的-l=0000000500000000000000B8 (编号+1)
c. 获取-O参数
-O OFFSET: set next multitransaction offset
在pg_multixact/members下面,取最大值加1然后乘以65536, 转换成16进制,然后在末尾添加4个0
#步骤
*进入相应目录
cd $PGDATA/pg_multixact/memebers/
*查看文件
[postgres@pg15tar members]$ ll
-rwx------ 1 postgres postgres 8192 Aug 4 12:46 0000
*计算
(0+1)*65536=65536
65536转换成16进制为10000
那么-O =0x100000000
d. 获取-m
-m=MXID:MXID set next and oldest multitransaction ID
zai pg_multixact/offsets下面,找到最大的文件编号,+1(文件编号+1) 后面跟上4个0
#步骤
*进入相应目录
cd $PGDATA/pg_multixact/offsets
*查看文件
[postgres@pg15tar offsets]$ ll
-rwx------ 1 postgres postgres 8192 Aug 5 11:02 0000
*计算结果
-m=0x00010000
e. 获取-x
-x XID :set next transaction ID
在pg_xact下面,找到最大的文件编号,+1后面跟上5个0
步骤
*进入相应目录
cd $PGDATA/pg_xact
*查看当前文件
[postgres@pg15tar pg_xact]$ ll
-rwx------ 1 postgres postgres 8192 Aug 5 11:02 0000
*计算-x值
-x=0x000100000
f. 重建控制文件
cd $PGDATA/global
touch pg_control
[postgres@pg15tar global]$ pg_resetwal -l 0000000500000000000000B8 -O 0x100000000 -m 0x00010000,0x00010000 -x 0x000100000 -f $PGDATA
pg_resetwal: warning: pg_control exists but is broken or wrong version; ignoring it
Write-ahead log reset
g. 启动数据库
pg_ctl start
[postgres@pg15tar global]$ pg_controldata
pg_control version number: 1300
Catalog version number: 202209061
Database system identifier: 7399511071694827642
Database cluster state: in production
pg_control last modified: Mon 05 Aug 2024 12:45:14 PM CST
Latest checkpoint location: 0/B8000028
Latest checkpoint's REDO location: 0/B8000028
Latest checkpoint's REDO WAL file: 0000000500000000000000B8
Latest checkpoint's TimeLineID: 5
Latest checkpoint's PrevTimeLineID: 5
Latest checkpoint's full_page_writes: off
Latest checkpoint's NextXID: 0:1048576
Latest checkpoint's NextOID: 10000
Latest checkpoint's NextMultiXactId: 65536
Latest checkpoint's NextMultiOffset: 0
Latest checkpoint's oldestXID: 3
Latest checkpoint's oldestXID's DB: 0
Latest checkpoint's oldestActiveXID: 0
Latest checkpoint's oldestMultiXid: 65536
Latest checkpoint's oldestMulti's DB: 0
Latest checkpoint's oldestCommitTsXid:0
Latest checkpoint's newestCommitTsXid:0
Time of latest checkpoint: Mon 05 Aug 2024 12:42:05 PM CST
Fake LSN counter for unlogged rels: 0/3E8
Minimum recovery ending location: 0/0
Min recovery ending loc's timeline: 0
Backup start location: 0/0
Backup end location: 0/0
End-of-backup record required: no
wal_level setting: replica
wal_log_hints setting: off
max_connections setting: 100
max_worker_processes setting: 8
max_wal_senders setting: 10
max_prepared_xacts setting: 0
max_locks_per_xact setting: 64
track_commit_timestamp setting: on
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
Float8 argument passing: by value
Data page checksum version: 0
Mock authentication nonce: 0000000000000000000000000000000000000000000000000000000000000000
标签:文件,postgres,Aug,--,checkpoint,Latest,pg,重建,PG
From: https://blog.51cto.com/ablewang/12167789