PostgreSQL 8.1 (tarball)

2007/10/27


ソースからインストール

PostgreSQL8.1でテストすることになった。
Fedora7においてPostgreSQL8はyumでインストールするのが一般的であるが、 最新版8.2.xがインストールされてしまう。
仕方ないのでソースからインストールした。
ソース(tarボール)は日本PostgreSQLユーザ会から入手できる。

rootで実行
su -
adduser postgres
cd /usr/local/src
tar xvzf postgresql-8.1.10.tar.gz
chown -R postgres:postgres /usr/local/src/postgresql-8.1.10
yum install -y gcc bison flex
ユーザpostgresで実行
su - postgres
cd /usr/local/src/postgres-8.1.10
./configure --without-readline --without-zlib;make;make check
cd contrib;make
rootで実行
su -
cd /usr/local/src/postgresql-8.1.10
make install
cd contrib;make install
chown -R postgres:postgres /usr/local/pgsql
~postgres/.bashrcに環境変数を設定
PG=/usr/local/pgsql
export PGLIB=$PG/lib
export PGDATA=$PG/data
export LD_LIBRARY_PATH=$LD_LIBRARY:$PGLIB
PATH=$PATH:$PG/bin 
ユーザpostgresで実行
su - postgres
initdb
postmaster -S
createdb test

トランザクション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,* from table00;


各SQLを実行するごとに、トランザクションID(xmin)が1づつ増えていることが確認できる。
トランザクションid(xid)SQLmemo
613insert into table00(nr,name) values (1,'abc');1レコード挿入
614insert into table00(nr,name) values (2,'d');1レコード挿入
615insert into table00(nr,name) values (3,'g');1レコード挿入
616insert into table00(nr,name) values (4,'j');1レコード挿入
617insert into table00(nr,name) values (5,'m');1レコード挿入
618select xmin,xmax,* from table00;select文でテーブルを表示
619update table00 set name='def' where nr=2 ;1レコード更新
620update table00 set name='ghi' where nr=3 ;1レコード更新
621update table00 set name='jkl' where nr=4 ;1レコード更新
622update table00 set name='mno' where nr=5 ;1レコード更新
623select xmin,xmax,* from table00;select文でテーブルを表示

vacumeの効果

vacumeの効果を見るため、現在のテーブルの状態を確認する。
データベース(sample_db)に対して、pgstattuple.sqlをバッチ処理する。
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');
実行結果は、つぎのようになる。
項目カラムvacuum前vacuum後
更新したために参照されなくなったタプルdead_tuple_count4件0件
データサイズdead_tuple_len164バイト0バイト
テーブル全体に占める割合dead_tuple_percent2%0%



pgstattuple.sqlの中身はこのようなもの
[postgres@po ~]$ cat /usr/local/src/postgresql-8.1.10/contrib/pgstattuple/pgstattuple.sql
-- Adjust this setting to control where the objects get created.
SET search_path = public;

CREATE TYPE pgstattuple_type AS (
        table_len BIGINT,               -- physical table length in bytes
        tuple_count BIGINT,             -- number of live tuples
        tuple_len BIGINT,               -- total tuples length in bytes
        tuple_percent FLOAT,            -- live tuples in %
        dead_tuple_count BIGINT,        -- number of dead tuples
        dead_tuple_len BIGINT,          -- total dead tuples length in bytes
        dead_tuple_percent FLOAT,       -- dead tuples in %
        free_space BIGINT,              -- free space in bytes
        free_percent FLOAT              -- free space in %
);

CREATE OR REPLACE FUNCTION pgstattuple(text)
RETURNS pgstattuple_type
AS '$libdir/pgstattuple', 'pgstattuple'
LANGUAGE 'C' STRICT;

CREATE OR REPLACE FUNCTION pgstattuple(oid)
RETURNS pgstattuple_type
AS '$libdir/pgstattuple', 'pgstattuplebyid'
LANGUAGE 'C' STRICT;

PostgreSQLインサイド概要