PostgreSQL 8.2 (yum)
2007/10/27
インストール
PostgreSQL8.1に続いて、PostgreSQL8.2でもテストすることになった。
PostgreSQL8はyumでインストールする。
トランザクションに燗する情報などいろいろなツールを使いたいのでcontribを追加する。
後々にjava言語PostgreSQLに接続するのでJDBCドライバを追加する。
rootで実行
su -
yum groupinstall -y 'PostgreSQL Database'
yum install -y postgresql-contrib postgresql-jdbc
PostgreSQL Databaseでインストールされるものはつぎのとおり。

postgrsql-contribは/usr/share/pgsql/contrib/以下にインストールされる。
JDBCドライバは/usr/lib/gcj/postgresql-jdbc/以下にインストールされる。
PostgreSQLを初期化
データベースを初期化
su - postgres
initdb
su -
service postgresql start
トランザクションIDをみたい
su - postgres
createdb sample_db
psql sample_db
テーブルを作り、データを挿入
create table table00(id serial primary key,nr int,name text);
insert into table00(nr,name) values (1,'abc');
insert into table00(nr,name) values (2,'d');
insert into table00(nr,name) values (3,'g');
insert into table00(nr,name) values (4,'j');
insert into table00(nr,name) values (5,'m');
トランザクションIDを含むレコードを表示
select xmin,xmax,* from table00;
データを更新して、トランザクションIDを確認
update table00 set name='def' where nr=2 ;
update table00 set name='ghi' where nr=3 ;
update table00 set name='jkl' where nr=4 ;
update table00 set name='mno' where nr=5 ;
トランザクションIDを含むレコードを表示
select xmin,xmax,* from table00;
各SQLを実行するごとに、トランザクションID(xmin)が1づつ増えていることが確認できる。
トランザクションid(xid) | SQL | memo |
598 | insert into table00(nr,name) values (1,'abc'); | 1レコード挿入 |
599 | insert into table00(nr,name) values (2,'d'); | 1レコード挿入 |
600 | insert into table00(nr,name) values (3,'g'); | 1レコード挿入 |
601 | insert into table00(nr,name) values (4,'j'); | 1レコード挿入 |
602 | insert into table00(nr,name) values (5,'m'); | 1レコード挿入 |
603 | select xmin,xmax,* from table00; | select文でテーブルを表示 |
604 | update table00 set name='def' where nr=2 ; | 1レコード更新 |
605 | update table00 set name='ghi' where nr=3 ; | 1レコード更新 |
606 | update table00 set name='jkl' where nr=4 ; | 1レコード更新 |
607 | update table00 set name='mno' where nr=5 ; | 1レコード更新 |
608 | select xmin,xmax,* from table00; | select文でテーブルを表示 |
vacumeの効果
vacumeの効果を見るため、現在のテーブルの状態を確認する。
データベース(sample_db)に対して、pgstattuple.sqlをバッチ処理する。
psql -f /usr/share/pgsql/contrib/pgstattuple.sql sample_db
psql sample_db
select * from pgstattuple('table00');
vacuum table00;
select * from pgstattuple('table00');

実行結果は、つぎのようになる。
項目 | カラム | vacuum前 | vacuum後 |
更新したために参照されなくなったタプル | dead_tuple_count | 4件 | 0件 |
データサイズ | dead_tuple_len | 164バイト | 0バイト |
テーブル全体に占める割合 | dead_tuple_percent | 2% | 0% |

まとめ
- トランザクションIDを表示する → select xmin,* from tablename;
- 更新等により発生するdead_tupleの情報を表示するにはpgstattuple.sql(contrib/pgstattuple.sql)が必要である。
- dead_tupleの情報を表示する → select dead_tuple_count from pgstattuple('tablename');
- vacuum tablenameでvacumm → dead_tupleが除去される。
PostgreSQLインサイド概要