pg_resetxlog、pg_resetwal工具的兩個用法
pg_resetwal — reset the write-ahead log and other control information of a PostgreSQL database cluster
pg_resetwal [ --force | -f ] [ --dry-run | -n ] [option...] [ --pgdata | -D ] datadir
一、PostgreSQL恢復pg_control文件:
1.需要下面四個參數:
-x XID set next transaction ID
在pg_clog下面,找到最大的文件編號,+1 后面跟上5個0
如:0000
-x = 0x000100000
-m MXID set next and oldest multitransaction ID
在pg_multixact/offsets下面,找到最大的文件編號,+1 后面跟上4個0
如:0000
-m = 0x00010000
-O OFFSET set next multitransaction offset
在pg_multixact/members下面,找到最大的文件編號,+1 后面跟上4個0
如:0000
-m = 0x00010000
-l XLOGFILE force minimum WAL starting location for new transaction log
找到pg_wal下面最新的日志文件,編號+1,然后分別去時間線、高32位、低32位:
如:000000010000000000000002
那么最新的日志文件就是000000010000000000000003
那么參數為:
-l 000000010000000000000003
2.執行恢復:
1)touch pg_control
2)pg_resetxlog -x 0x000100000 -m 0x00010000 -O 0x00010000 -l 000000010000000000000003 -f $PGDATA
當然,-m參數如果報錯,也可以不要:
pg_resetxlog -x 0x000100000 -O 0x00010000 -l 000000010000000000000003 -f ¥PGDATA
二、安全清理不必要的日志文件:
1)cd $PGDATA/pg_xlog/
2)pg_ctl stop -D $PGDATA -m fast
3)pg_controldata記錄清理前的信息,並記錄:NextXID NextOID給下面使用
4)pg_resetxlog -o 24584 -x 1745 -f $PGDATA
5)查看清理后大小
du -sh
三、使用pg_resetxlog來重置事務ID來訪問被修改的數據
例如刪除數據的xid為100,那么我們回退到99,那么刪除到操作還不可見,因此就能看到被刪除的數據,但是刪除是已經發生的,當我們提升xid到100時,刪除就生效,你將無法訪問到刪除的數據。
被重置的xid之后的操作還是存在,無法抹除。當在xid為99時,我們再插入一條數據,那么這個時候訪問表,我們將得到原來刪除了表,在插入一條記錄的情況。刪除和插入將在一個xid下。
因此,使用重置xid的方式,我們也必須在重置之后,將現在的表備份出來,簡單方法是create test_old as select * from test;的方式來做。因為隨着xid的增長,誤操作也會被重現。
參考我之前的:https://www.cnblogs.com/kuang17/p/10615164.html
四、參數詳解:
-x: A safe value for the next transaction ID (-x) can be determined by looking for the numerically largest file name in the directory pg_clog under the data directory, adding one, and then multiplying by 1048576. Note that the file names are in hexadecimal. It is usually easiest to specify the option value in hexadecimal too. For example, if 0011 is the largest entry in pg_clog, -x 0x1200000 will work (five trailing zeroes provide the proper multiplier). -m: A safe value for the next multitransaction ID (-m) can be determined by looking for the numerically largest file name in the directory pg_multixact/offsets under the data directory, adding one,and then multiplying by 65536. As above, the file names are in hexadecimal, so the easiest way to do this is to specify the option value in hexadecimal and add four zeroes -O: A safe value for the next multitransaction offset (-O) can be determined by looking for the numerically largest file name in the directory pg_multixact/members under the data directory, adding one, and then multiplying by 65536. As above, the file names are in hexadecimal, so the easiest way to do this is to specify the option value in hexadecimal and add four zeroes -l: The WAL starting address (-l) should be larger than any WAL segment file name currently existing in the directory pg_xlog under the data directory. These names are also in hexadecimal and have three parts. The first part is the “timeline ID” and should usually be kept the same. Do not choose a value larger than 255 (0xFF) for the third part; instead increment the second part and reset the third part to 0. For example, if 00000001000000320000004A is the largest entry in pg_xlog, -l 0x1,0x32,0x4B will work; but if the largest entry is 000000010000003A000000FF, choose -l 0x1,0x3B,0x0 or more.
https://www.postgresql.org/docs/12/app-pgresetwal.html