kuniku’s diary

はてなダイアリーから移行(旧 d.hatena.ne.jp/kuniku/)、表示がおかしな箇所はコメントをお願いします。記載されている内容は日付およびバージョンに注意してください。直近1年以上前は古い情報の可能性が高くなります。

Windows版のPostgreSQL8.1でのfunction(pgstattuple)を使う

postgreのWindowsインストーラでEnable contrib modules で選択してインストールして
いないと pgstattuple をデフォルトのままでは使用することができない。

インストーラpostgresql-8.1-ja.msi から追加で入れられるかなと
試してみたけどど、「修正」「削除」のみで、その後ツリー上の
インストールするものを選択するんだけど、何かよくわからずいろいろ
選択してみたけどpgstattupleは、インストールできなかった。


http://nt.hakodate-ct.ac.jp/~takahasi/technicalnote/F7/postgres81.html
のサイトを見るとLinux版だけど

psql -f /usr/local/src/postgresql-8.1.10/contrib/pgstattuple/pgstattuple.sql sample_db
psql sample_db
select * from pgstattuple('table00');
vacuum table00;
select * from pgstattuple('table00');

とあるので、windowsでも同じようにやればいいんちゃぅ?ってことで

Windows版のpostgreをインストールした箇所に移動して
psql の引数に合わせて、 psql -f データベース名 ユーザ名 で functionをインストール
psql -f は、このページでは改行されて表示されるかもしれないけど本来は1行)

cd "C:\Program Files\PostgreSQL\8.1\bin"
C:\Program Files\PostgreSQL\8.1\bin>psql -f "C:\Program Files\PostgreSQL\8.1\share\contrib\pgstattuple.sql" databaseName po
stgres
SET
CREATE TYPE
CREATE FUNCTION
CREATE FUNCTION

インストールできたか?使用できるかの確認

C:\Program Files\PostgreSQL\8.1\bin>psql.exe databaseName postgres
Welcome to psql 8.1.4, the PostgreSQL interactive terminal.

Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit

databaseName=#  select * from pgstattuple('テーブル名');
 table_len | tuple_count | tuple_len | tuple_percent | dead_tuple_count | dead_tuple_len | dead_tuple_percent | free_spa
ce | free_percent
                                                                                                                                                                                                                                              • -
                                  • -
106496 | 58 | 4912 | 4.61 | 2 | 168 | 0.16 | 966 44 | 90.75 (1 row)

とタプルの状況が表示できるようになった。
フルVACUUM(バキューム処理の詳細な報告を出力)の実行をして、タプルの確認

databaseName=# vacuum FULL VERBOSE テーブル名;
・・・・

実行結果確認
dead_tuple_countが0になり、不要領域が回収された

databaseName=# select * from pgstattuple('テーブル名');
 table_len | tuple_count | tuple_len | tuple_percent | dead_tuple_count | dead_tuple_len | dead_tuple_percent | free_spa
ce | free_percent
                                                                                                                                                                                                                                              • -
                                  • -
8192 | 58 | 4912 | 59.96 | 0 | 0 | 0 | 28 92 | 35.3 (1 row)