有個開發環境,因為磁盤空間滿了,有人直接將pg_wal下的文件rm了,然后,重啟報錯
2020-06-28 18:13:59.148 CST [10387]: LOG: database system was shut down at 2020-06-28 18:12:41 CST 2020-06-28 18:13:59.148 CST [10387]: LOG: invalid primary checkpoint record 2020-06-28 18:13:59.148 CST [10387]: PANIC: could not locate a valid checkpoint record 2020-06-28 18:13:59.560 CST [10342]: LOG: startup process (PID 10387) was terminated by signal 6: Aborted 2020-06-28 18:13:59.560 CST [10342]: LOG: aborting startup due to startup process failure 2020-06-28 18:13:59.581 CST [10342]: LOG: database system is shut down
從日志文件也可以看到因為wal segment找不到,而無法啟動:
$ more postgresql-Sun.log 2020-06-28 18:17:25.882 CST [12259]: LOG: database system was shut down at 2020-06-28 18:16:48 CST 2020-06-28 18:17:25.882 CST [12259]: LOG: recovered replication state of node 1 to 2/F6A001C0 2020-06-28 18:17:25.887 CST [12216]: LOG: database system is ready to accept connections 2020-06-28 18:17:25.912 CST [12269]: LOG: logical replication apply worker for subscription "b_sub" has started 2020-06-28 18:17:25.922 CST ap a [12272]: LOG: starting logical decoding for slot "publication_abce" 2020-06-28 18:17:25.922 CST ap a [12272]: DETAIL: Streaming transactions committing after F/CD215498, reading WAL from F/CD215498. 2020-06-28 18:17:25.922 CST ap a [12272]: ERROR: requested WAL segment 000000010000000F000000CD has already been removed 2020-06-28 18:17:25.922 CST [12269]: ERROR: could not receive data from WAL stream: ERROR: requested WAL segment 000000010000000F000000CD has already been removed 2020-06-28 18:17:25.923 CST [12216]: LOG: background worker "logical replication worker" (PID 12269) exited with exit code 1 2020-06-28 18:17:30.935 CST [12324]: LOG: logical replication apply worker for subscription "b_sub" has started 2020-06-28 18:17:30.941 CST ap a [12325]: LOG: starting logical decoding for slot "publication_abce" 2020-06-28 18:17:30.941 CST ap a [12325]: DETAIL: Streaming transactions committing after F/CD215498, reading WAL from F/CD215498. 2020-06-28 18:17:30.941 CST ap a [12325]: ERROR: requested WAL segment 000000010000000F000000CD has already been removed 2020-06-28 18:17:30.941 CST [12324]: ERROR: could not receive data from WAL stream: ERROR: requested WAL segment 000000010000000F000000CD has already been removed 2020-06-28 18:17:30.943 CST [12216]: LOG: background worker "logical replication worker" (PID 12324) exited with exit code 1
因為不是重要環境,直接使用pg_resetwal直接重置了wal和其他控制信息,然后重啟。
$ pg_resetwal -f /data/pgdata/11/data Write-ahead log reset
pg_resetwal會清空存儲在pg_control文件中的wal和其他可選的控制信息。不到萬不得已,可別使用這個命令。
執行該工具之后,數據庫可以啟動,但是可能會包含不一致的數據,因為會有事務部分提交。重啟后,建議立即將數據dump出來,運行initdb並reload數據。檢查數據一致性並根據需要進行數據修復。
需要顯式的指定目錄,pg_resetwal不會使用環境變量PGDATA。
參數-f表示強制執行pg_resetwal。