PostgreSQLインストールメモ(バージョン 9.2)
PostgreSQLをインストールした際のメモ
環境 バージョン
CentOS 6.4
PostgreSQL 9.2.4
1.環境準備
1.1.make
バージョンが3.8以上であることを確認
# gmake --version
GNU Make 3.81
1.2.gcc
インストールされていることを確認
# gcc -v
1.3. Readline
Readlineライブラリをインストール
# yum install readline
# yum install readline-devel
2.PostgreSQLインストール
2.1.postgresユーザ作成
# useradd postgres
2.2.ソースの入手と解答
# su - postgres
$ cd /usr/local/src/
$ wget http://ftp.postgresql.org/pub/source/v9.2.4/postgresql-9.2.4.tar.gz
$ tar xvzf postgresql-9.2.4.tar.gz
2.3.コンパイル
$ cd postgresql-9.2.4/
$ ./configure
$ gmake world
(省略)
PostgreSQL, contrib, and documentation successfully made. Ready to install.
※ドキュメント(HTMLやman)や追加モジュール(contrib)を含め、構築可能なもの全てを構築したい場合、上記の様に実施します
2.4.リグレーションテスト
$ gmake check
(省略)
=======================
All 131 tests passed.
=======================
(省略)
2.5.インストール
$ su
# gmake install-world
(省略)
PostgreSQL, contrib, and documentation installation complete.
2.6.インストールディレクトリの所有者変更
# cd /usr/local/
# chown -R postgres:postgres pgsql/
2.7.postgresユーザの環境変数設定
# su - postgres
$ vi .bash_profile (下記を設定)
# PostgreSQL PATH
export POSTGRES_HOME=/usr/local/pgsql
export PGDATA=/home/postgres/data
export PATH=$PATH:$POSTGRES_HOME/bin
export PGLIB=/usr/local/pgsql/lib
export MANPATH="$MANPATH":$POSTGRES_HOME/man
export
2.7.postgresユーザの環境変数設定読み込み
$ source .bash_profile
2.8.データベースクラスタの作成
$ initdb -E UTF8 --no-locale
2.9.PostgreSQL起動
$ initdb -E UTF8 --no-locale
以上
PostgreSQLでHINT句が使えるらしい!!
こんばんは。
pg_hint_planというモジュールがリリースされたみたいなので、インストールしてみました!!
----pg_hint_planのドキュメント抜粋----
pg_hint_planの概要
PostgreSQLはコストベースオプティマイザを採用しており、SQL文と統計情報を元に可能なアクセスパスのコストを見積もり、最もコストの低い実行計画を選択します。オプティマイザは可能な限りよい実行計画を作成使用としますが、カラム間の相関関係などは考慮しないため、複雑なクエリでは常に最適なプランが選択されるとは限りません。pg_hint_planを用いると、ヒントを記述したブロックコメントをSQL文の前に加えることで、SQL文やGUCパラメータを変更することなく実行計画を制御することができます。
--------
インストール手順
1.ソースのダウンロード
http://en.sourceforge.jp/projects/pghintplan/releases/
2. tarボールを解凍
$ tar xvzf pg_hint_plan.tar.gz
3.インストール
$ cd pg_hint_plan
$ make USE_PGXS=1 PG_CONFIG=/usr/local/pgsql/bin/pg_config
$ make USE_PGXS=1 PG_CONFIG=/usr/local/pgsql/bin/pg_config install
4.pg_hint_planのロード
$ vi $PGDATA/postgresql.conf
(以下を追加)
shared_preload_libraries = 'pg_hint_plan'
5.PostgreSQL再起動
$ pg_ctl restart
waiting for server to shut down.... done
server stopped server starting
$ [2013-02-05 23:49:03.032 JST][00000][19777] LOG: loaded library "pg_hint_plan"
HINT句を使用してみた。
・HINT句を使用しない場合
=# EXPLAIN ANALYZE SELECT * from pgbench_accounts ;
QUERY PLAN -------------------------------------------------------------------------------- -----------------------------------------
Seq Scan on pgbench_accounts (cost=0.00..2640.00 rows=100000 width=97) (actual time=0.026..30.762 rows=100000 loops=1) Total runtime: 46.029 ms (2 rows)
・HINT句使った場合
/*+ IndexScan(pgbench_accounts) */EXPLAIN ANALYZE SELECT * from pgben ch_accounts ; QUERY PLAN
-------------------------------------------------------------------------------- ----------------------------------------------------------
Seq Scan on pgbench_accounts (cost=10000000000.00..10000002640.00 rows=100000 width=97) (actual time=0.034..35.974 rows=100000 loops=1) Total runtime:
インデックスを定義していないため、IndexSCanとならない。(当然であるが)
よく見ると、Seq Scan on pgbench_accountsnの
cost=10000000000となっており、かなり大きな値になっている。
enable_seqscanをoffに設定した場合と同様に、costの値を大きくしているようです。
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