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)SQLmemo
598insert into table00(nr,name) values (1,'abc');1レコード挿入
599insert into table00(nr,name) values (2,'d');1レコード挿入
600insert into table00(nr,name) values (3,'g');1レコード挿入
601insert into table00(nr,name) values (4,'j');1レコード挿入
602insert into table00(nr,name) values (5,'m');1レコード挿入
603select xmin,xmax,* from table00;select文でテーブルを表示
604update table00 set name='def' where nr=2 ;1レコード更新
605update table00 set name='ghi' where nr=3 ;1レコード更新
606update table00 set name='jkl' where nr=4 ;1レコード更新
607update table00 set name='mno' where nr=5 ;1レコード更新
608select 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_count4件0件
データサイズdead_tuple_len164バイト0バイト
テーブル全体に占める割合dead_tuple_percent2%0%



まとめ


PostgreSQLインサイド概要