WALを無理やり壊してみた。
こんばんは!!!
無理やりWALの壊して、PostgreSQLが起動できない状態にしてみました。
以下の手順を実施しました。
1. WALの現在の書き込み位置を確認
postgres=# SELECT pg_current_xlog_location();
pg_current_xlog_location
--------------------------
6/1559C4E8
(1 row)
2.WALの書き込み位置をファイル名に変更
postgres=# SELECT pg_xlogfile_name('6/1559C4E8');
pg_xlogfile_name
--------------------------
000000010000000600000015
(1 row)
000000010000000600000015とういWALファイルに書き込み中だというとことが
わかった。ので、次のこのファイルを壊します。
$ cd $PGDATA/pg_xlog/
$ echo 1 > 000000010000000600000015
WALファイルを壊してもPostgreSQLは停止しないので、停止させる。
4.PostgreSQL停止
$ pg_ctl stop -m i
waiting for server to shut down... done
server stopped
5.PostgreSQLを起動
$ pg_ctl start
server starting
6.psqlで接続
$ psql
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
起動していない?
7.ログを確認
(ログの抜粋)
LOG: database system was interrupted; last known up at 2013-01-30 21:57:41 JST
LOG: could not read from log file 6, segment 21, offset 0: No such file or directory
LOG: invalid primary checkpoint record
LOG: could not read from log file 6, segment 21, offset 0: No such file or directory
LOG: invalid secondary checkpoint record
PANIC: could not locate a valid checkpoint record
LOG: startup process (PID 31213) was terminated by signal 6: Aborted
LOG: aborting startup due to startup process failure
→起動していない。。
8.pg_resetxlogを実行
-n をつけて、実際の実行の前に確認します。
$ pg_resetxlog -n -f $PGDATA
pg_control values:
First log file ID after reset: 6
First log file segment after reset: 29
pg_control version number: 903
Catalog version number: 201105231
Database system identifier: 5824051138782248887
Latest checkpoint's TimeLineID: 1
Latest checkpoint's NextXID: 0/502509
Latest checkpoint's NextOID: 33059
Latest checkpoint's NextMultiXactId: 1
Latest checkpoint's NextMultiOffset: 0
Latest checkpoint's oldestXID: 1795
Latest checkpoint's oldestXID's DB: 1
Latest checkpoint's oldestActiveXID: 502509
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
Date/time type storage: 64-bit integers
Float4 argument passing: by value
Float8 argument passing: by value
9.実際に実行
$ pg_resetxlog -f $PGDATA
Transaction log reset
10.PostgreSQLを起動
$ pg_ctl start
server starting
11.PostgreSQLに接続
$ psql
psql (9.1.6)
Type "help" for help.
postgres=#
問題なく接続できた。が、、
pg_resetxlogにドキュメントに以下の記載があるのが、気になるので引き続き調査する。不完全にコミットされたトランザクションとは??
---マニュアル抜粋
このコマンドを実行すると、サーバが開始できるようになるはずです。 ただし、不完全にコミットされたトランザクションが原因でデータベースのデータに矛盾が起こる可能性があることに注意してください。 コマンドの実行後は、データをただちにダンプし、initdbを実行し、リロードすべきです。 リロード後、矛盾がないか検査し、必要に応じて修復を行ってください。
---
参考URL:
http://www.postgresql.jp/document/9.2/html/functions-admin.html
http://www.postgresql.jp/document/9.2/html/app-pgresetxlog.html