PostgreSQL DBA(18) - pg_waldump工具簡介
按照先前幾節對WAL檔案結構的介紹,我們可以自行寫一個解析事務日誌的小程式用於檢視日誌檔案中的內容,不過PG已經幫我們考慮到了,PG提供了dump事務日誌的工具:pg_waldump.
注:pg_waldump for PG 10.x+,如果是PG 9.x或以下版本,則使用pg_xlogdump.
一、pg_waldump簡介
在Linux下執行,使用--help檢視幫助.
[xdb@localhost pg_wal]$ pg_waldump --help
pg_waldump decodes and displays PostgreSQL write-ahead logs for debugging.
Usage:
pg_waldump [OPTION]... [STARTSEG [ENDSEG]]
Options:
-b, --bkp-details output detailed information about backup blocks
-e, --end=RECPTR stop reading at WAL location RECPTR
-f, --follow keep retrying after reaching end of WAL
-n, --limit=N number of records to display
-p, --path=PATH directory in which to find log segment files or a
directory with a ./pg_wal that contains such files
(default: current directory, ./pg_wal, $PGDATA/pg_wal)
-r, --rmgr=RMGR only show records generated by resource manager RMGR;
use --rmgr=list to list valid resource manager names
-s, --start=RECPTR start reading at WAL location RECPTR
-t, --timeline=TLI timeline from which to read log records
(default: 1 or the value used in STARTSEG)
-V, --version output version information, then exit
-x, --xid=XID only show records with transaction ID XID
-z, --stats[=record] show statistics instead of records
(optionally, show per-record statistics)
-?, --help show this help, then exit
[xdb@localhost pg_wal]$
-b, --bkp-details output detailed information about backup blocks
輸出backup blocks即full-write-page的詳細資訊.
-e, --end=RECPTR stop reading at WAL location RECPTR
搜尋在此LSN偏移處結束
-f, --follow keep retrying after reaching end of WAL
在到達WAL末尾時仍繼續嘗試
-n, --limit=N number of records to display
XLOG Record的輸出個數
-p, --path=PATH directory in which to find log segment files or a
directory with a ./pg_wal that contains such files
(default: current directory, ./pg_wal, $PGDATA/pg_wal)
在哪個目錄下尋找WAL segment files
-r, --rmgr=RMGR only show records generated by resource manager RMGR;
use --rmgr=list to list valid resource manager names
只顯示指定的RMGR的XLOG Record
-s, --start=RECPTR start reading at WAL location RECPTR
在此LSN偏移處開始搜尋
-t, --timeline=TLI timeline from which to read log records
(default: 1 or the value used in STARTSEG)
指定的時間線timeline
-V, --version output version information, then exit
輸出版本資訊,然後退出
-x, --xid=XID only show records with transaction ID XID
只輸出指定的事務ID的XLOG Record
-z, --stats[=record] show statistics instead of records
(optionally, show per-record statistics)
輸出統計資訊
-?, --help show this help, then exit
輸出幫助資訊
二、pg_waldump使用
下面是測試機上pg_wal目錄中的檔案
[xdb@localhost pg_wal]$ ll
total 98332
-rw-------. 1 xdb xdb 16777216 Dec 20 12:02 000000010000000100000048
-rw-------. 1 xdb xdb 16777216 Dec 19 16:47 000000010000000100000049
-rw-------. 1 xdb xdb 16777216 Dec 19 16:47 00000001000000010000004A
-rw-------. 1 xdb xdb 16777216 Dec 19 16:47 00000001000000010000004B
-rw-------. 1 xdb xdb 16777216 Dec 19 16:47 00000001000000010000004C
-rw-------. 1 xdb xdb 16777216 Dec 19 16:47 00000001000000010000004D
drwx------. 2 xdb xdb 6 Nov 16 15:48 archive_status
輸出檔案000000010000000100000048最開始的4個XLOG Record
命令:pg_waldump -p ./ -s 1/48000000 -n 4
[xdb@localhost pg_wal]$ pg_waldump -p ./ -s 1/48000000 -n 4
rmgr: Heap len (rec/tot): 77/ 77, tx: 1964, lsn: 1/48000070, prev 1/47FFFFF8, desc: INSERT off 117, blkref #0: rel 1663/16402/17028 blk 1110
rmgr: Heap len (rec/tot): 77/ 77, tx: 1964, lsn: 1/480000C0, prev 1/48000070, desc: INSERT off 7, blkref #0: rel 1663/16402/17031 blk 1111
rmgr: Heap len (rec/tot): 77/ 77, tx: 1964, lsn: 1/48000110, prev 1/480000C0, desc: INSERT off 8, blkref #0: rel 1663/16402/17031 blk 1111
rmgr: Heap len (rec/tot): 77/ 77, tx: 1964, lsn: 1/48000160, prev 1/48000110, desc: INSERT off 9, blkref #0: rel 1663/16402/17031 blk 1111
注意第一條記錄,上一個LSN為1/47FFFFF8(prev 1/47FFFFF8),提示上一page最後一個XLOG Record儲存在本頁的XLogLongPageHeaderData中,儲存的空間大小可以從該XLOG Record的LSN(1/48000070)和XLogLongPageHeaderData的大小(40B)推算獲得,有興趣的同學可以自行計算.
注:LSN的計算請參照PostgreSQL DBA(15) - WAL檔案結構
檢視Redo point後的XLOG Record
首先使用pg_controldata命令檢視Redo point --> 1/484336A0
[xdb@localhost pg_wal]$ pg_controldata
pg_control version number: 1100
Catalog version number: 201809051
Database system identifier: 6624362124887945794
Database cluster state: in production
pg_control last modified: Thu 20 Dec 2018 12:17:39 PM CST
Latest checkpoint location: 1/484336D8
Latest checkpoint's REDO location: 1/484336A0
Latest checkpoint's REDO WAL file: 000000010000000100000048
Latest checkpoint's TimeLineID: 1
...
然後使用pg_waldump檢視
命令:pg_waldump -p ./ -s 1/484336A0
[xdb@localhost pg_wal]$ pg_waldump -p ./ -s 1/484336A0
rmgr: Standby len (rec/tot): 50/ 50, tx: 0, lsn: 1/484336A0, prev 1/48433668, desc: RUNNING_XACTS nextXid 1971 latestCompletedXid 1970 oldestRunningXid 1971
rmgr: XLOG len (rec/tot): 106/ 106, tx: 0, lsn: 1/484336D8, prev 1/484336A0, desc: CHECKPOINT_ONLINE redo 1/484336A0; tli 1; prev tli 1; fpw true; xid 0:1971; oid 17046; multi 1; offset 0; oldest xid 561 in DB 16402; oldest multi 1 in DB 16402; oldest/newest commit timestamp xid: 0/0; oldest running xid 1971; online
rmgr: Standby len (rec/tot): 50/ 50, tx: 0, lsn: 1/48433748, prev 1/484336D8, desc: RUNNING_XACTS nextXid 1971 latestCompletedXid 1970 oldestRunningXid 1971
pg_waldump: FATAL: error in WAL record at 1/48433748: invalid record length at 1/48433780: wanted 24, got 0
[xdb@localhost pg_wal]$
三、參考資料
PG 11 Document:pg_waldump
PostgreSQL 原始碼解讀(109)- WAL#5(相關資料結構)
PostgreSQL DBA(15) - WAL檔案結構
PostgreSQL DBA(16) - WAL segment file內部結構
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/6906/viewspace-2374775/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- PostgreSQL DBA(87) - benchmarksql簡介SQL
- PostgreSQL DBA(8) - sysbench簡介SQL
- PostgreSQL DBA(3) - 日誌分析工具pgbadger簡介SQL
- PostgreSQL DBA(7) - pgbench簡介SQL
- PostgreSQL DBA(90) - Linux(stress-ng簡介)SQLLinux
- PostgreSQL DBA(182) - HOTSQL
- PostgreSQL DBA(185) - watchSQL
- PostgreSQL DBA(184) - Extension(hypoPG)SQL
- PostgreSQL DBA(186) - SQL Group BySQL
- PostgreSQL DBA(181) - Using PostgreSQL as a Data WarehouseSQL
- PostgreSQL DBA(187) - TCP keepaliveSQLTCP
- PostgreSQL DBA(180) - What is locktype=transactionidSQL
- PostgreSQL DBA(183) - PG 14(Better JSON)SQLJSON
- PostgreSQL DBA(189) - PG 14 Monitoring ImprovementsSQL
- PostgreSQL DBA(188) - PG 14 enable_memoizeSQL
- PostgreSQL:引數簡介SQL
- PostgreSQL DBA(118) - Develop(Seamless Application Failover)SQLdevAPPAI
- PostgreSQL簡介及安裝SQL
- PostgreSQL DBA(45) - Hypothetical Indexes in PostgreSQLSQLIndex
- PostgreSQL中索引與CTE簡介SQL索引
- 《MySQL 8 DBA基礎教程》簡介MySql
- PostgreSQL DBA(35) - CTESQL
- PostgreSQL DBA(42) - localeSQL
- PostgreSQL DBA(133) - Extension(postgresql_anonymizer)SQL
- CRM工具簡介
- PostgreSQL簡介及安裝步驟SQL
- PostgreSQL DBA(59) - Index(Bloom)SQLIndexOOM
- PostgreSQL DBA(53) - Index(BRIN)SQLIndex
- PostgreSQL DBA(58) - DBLinkSQL
- PostgreSQL DBA(48) - Index(GiST)SQLIndex
- PostgreSQL DBA(51) - Index(GIN)SQLIndex
- PostgreSQL DBA(52) - Index(RUM)SQLIndex
- PostgreSQL DBA(47) - Index(Btree)SQLIndex
- PostgreSQL DBA(43) - Index(Hash)SQLIndex
- PostgreSQL DBA(191) - CollationSQL
- redis-18.事物簡介Redis
- 面向 DBA 的 Linux Shell 指令碼簡介Linux指令碼
- PostgreSQL 原始碼解讀(240)- HTAB簡介SQL原始碼