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) | SQL | memo |
613 | insert into table00(nr,name) values (1,'abc'); | 1レコード挿入 |
614 | insert into table00(nr,name) values (2,'d'); | 1レコード挿入 |
615 | insert into table00(nr,name) values (3,'g'); | 1レコード挿入 |
616 | insert into table00(nr,name) values (4,'j'); | 1レコード挿入 |
617 | insert into table00(nr,name) values (5,'m'); | 1レコード挿入 |
618 | select xmin,xmax,* from table00; | select文でテーブルを表示 |
619 | update table00 set name='def' where nr=2 ; | 1レコード更新 |
620 | update table00 set name='ghi' where nr=3 ; | 1レコード更新 |
621 | update table00 set name='jkl' where nr=4 ; | 1レコード更新 |
622 | update table00 set name='mno' where nr=5 ; | 1レコード更新 |
623 | select 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_count | 4件 | 0件 |
データサイズ | dead_tuple_len | 164バイト | 0バイト |
テーブル全体に占める割合 | dead_tuple_percent | 2% | 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インサイド概要