前回、PostgreSQLに日本語全文検索のために中間一致検索インデックスを付ける方法として、pg_trgm と pg_bigm を導入することを決めた。今回は実際に、導入した作業のを進める。
実際に何をするかというと、pg_trgm を日本語で使うために、PostgreSQLのソースコードを入手し、半角英数のみに限定しているマクロであるKEEPONLYALNUMオプションをコメントアウトしてrpmパッケージをリビルドして入れ直す。それに伴い、以前の記事でバージョンアップした pdo_pgsql と pdo_pgsql も依存関係の問題でインストールされなおしてしまうので、この作業もやり直す。
その次に、pg_bigm をソースから修正せずにインストールする。こちらの作業はpg_trgm の作業に比べるとお手軽である。では実際の作業を行ってみる。
pg_trgm を日本語で使えるようにする
1.PostgreSQLのソースをyum-utilでタウンロード
yumでアクセスできるリポジトリからソースコードを入手するには、yumdownloaderコマンドを使う。yumdownloaderコマンドはyum-util に含まれているため、これをインストールする。
# yum install yum-utils
~省略~
Installed:
yum-utils.noarch 0:1.1.30-14.el6
Complete!
#
#
そんでもってPosgtreSQLのソースコードをpgdg92リポジトリから yumdownloaderコマンドを使って入手する。ソースコードはコマンドを実行したカレントディレクトリにダウンロードされる。
# yumdownloader --enablerepo=pgdg92 --source postgresql92-server
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: ftp.iij.ad.jp
* extras: ftp.iij.ad.jp
* updates: ftp.iij.ad.jp
Enabling pgdg92-source repository
postgresql92-9.2.4-1PGDG.rhel6.src.rpm | 21 MB 00:09
#
#
rpmコマンドでソースコードの rpmパッケージをインストールする。
# rpm -ivh postgresql92-9.2.4-1PGDG.rhel6.src.rpm
1:postgresql92 ########################################### [100%]
#
これで、~/rpmbuild/ ディレクトリにソースコードがインストールされているので、tarコマンドで展開する。
# cd ~/rpmbuild/SOURCES
# tar -xvf postgresql-9.2.4.tar.bz2
~省略~
#
#
これでPostgreSQLのソースコードを手に入れた!
2.PostgreSQLのソースコードを修正
ソースコードを修正と言っても大げさなことはしない。 ~/rpmbuild/SOURCES/postgresql-9.2.4/contrib/pg_trgm/trgm.h ファイルの中の”KEEPONLYALNUM”オプションを/* */でコメントアウトするだけだ。
# cd ~/rpmbuild/SOURCES/postgresql-9.2.4/contrib/pg_trgm/
# vi ./trgm.h
~省略~
/* options */
#define LPADDING2
#define RPADDING1
/* #define KEEPONLYALNUM */ ←コメントアウトする
~省略~
#
#
そして展開したソースコードをtar.bz2に戻しておく
# cd ../../..
# mv postgresql-9.2.4.tar.bz2 postgresql-9.2.4.tar.bz2.bk
# tar cjvf postgresql-9.2.4.tar.bz2 postgresql-9.2.4
~省略~
#
#
これで “KEEPONLYALNUM”オプションを無効にしたソースコードの出来上がりである。
3.PostgreSQLのRPMパッケージのビルド
修正したソースを使ってRPMパッケージを作る。specファイルに変更は加えずにいきなりビルドしてみる。
# cd ../SPECS/
# rpmbuild -bb ./postgresql-9.2.spec
エラー: ビルド依存性の失敗:
bison は postgresql92-9.2.4-1PGDG.el6.x86_64 に必要とされています
flex は postgresql92-9.2.4-1PGDG.el6.x86_64 に必要とされています
python-devel は postgresql92-9.2.4-1PGDG.el6.x86_64 に必要とされています
tcl-devel は postgresql92-9.2.4-1PGDG.el6.x86_64 に必要とされています
readline-devel は postgresql92-9.2.4-1PGDG.el6.x86_64 に必要とされています
e2fsprogs-devel は postgresql92-9.2.4-1PGDG.el6.x86_64 に必要とされています
gettext >= 0.10.35 は postgresql92-9.2.4-1PGDG.el6.x86_64 に必要とされています
pam-devel は postgresql92-9.2.4-1PGDG.el6.x86_64 に必要とされています
uuid-devel は postgresql92-9.2.4-1PGDG.el6.x86_64 に必要とされています
openldap-devel は postgresql92-9.2.4-1PGDG.el6.x86_64 に必要とされています
#
#
怒られてしまった。足りないと言われたものをインストールする。
# cd ../SPECS/
# yum isnstall bison flex python-devel tcl-devel readline-devel e2fsprogs-devel gettext pam-devel uuid-devel openldap-devel
~省略~
Installed:
bison.x86_64 0:2.4.1-5.el6 e2fsprogs-devel.x86_64 0:1.41.12-14.el6 flex.x86_64 0:2.5.35-8.el6 gettext.x86_64 0:0.17-16.el6 openldap-devel.x86_64 0:2.4.23-32.el6_4.1 pam-devel.x86_64 0:1.1.1-13.el6
python-devel.x86_64 0:2.6.6-36.el6 readline-devel.x86_64 0:6.0-4.el6 tcl-devel.x86_64 1:8.5.7-6.el6 uuid-devel.x86_64 0:1.6.1-10.el6
Dependency Installed:
cvs.x86_64 0:1.11.23-15.el6 cyrus-sasl-devel.x86_64 0:2.1.23-13.el6_3.1 ncurses-devel.x86_64 0:5.7-3.20090208.el6 tcl.x86_64 1:8.5.7-6.el6 uuid.x86_64 0:1.6.1-10.el6
Complete!
#
#
これで再挑戦する。
# rpmbuild -bb ./postgresql-9.2.spec
実行中(%prep): /bin/sh -e /var/tmp/rpm-tmp.U04Fat
+ umask 022
+ cd /root/rpmbuild/BUILD
+ cd /root/rpmbuild/BUILD
+ rm -rf postgresql-9.2.4
+ /usr/bin/bzip2 -dc /root/rpmbuild/SOURCES/postgresql-9.2.4.tar.bz2
+ /bin/tar -xf -
~省略~
Requires(post): /bin/sh
Requires: /bin/sh libc.so.6()(64bit) libc.so.6(GLIBC_2.2.5)(64bit) libc.so.6(GLIBC_2.3)(64bit) libc.so.6(GLIBC_2.3.3)(64bit) rtld(GNU_HASH)
伸張ファイルの検査中: /usr/lib/rpm/check-files /root/rpmbuild/BUILDROOT/postgresql92-9.2.4-1PGDG.el6.x86_64
書き込み完了: /root/rpmbuild/RPMS/x86_64/postgresql92-9.2.4-1PGDG.el6.x86_64.rpm
書き込み完了: /root/rpmbuild/RPMS/x86_64/postgresql92-libs-9.2.4-1PGDG.el6.x86_64.rpm
書き込み完了: /root/rpmbuild/RPMS/x86_64/postgresql92-server-9.2.4-1PGDG.el6.x86_64.rpm
書き込み完了: /root/rpmbuild/RPMS/x86_64/postgresql92-docs-9.2.4-1PGDG.el6.x86_64.rpm
書き込み完了: /root/rpmbuild/RPMS/x86_64/postgresql92-contrib-9.2.4-1PGDG.el6.x86_64.rpm
書き込み完了: /root/rpmbuild/RPMS/x86_64/postgresql92-devel-9.2.4-1PGDG.el6.x86_64.rpm
書き込み完了: /root/rpmbuild/RPMS/x86_64/postgresql92-plperl-9.2.4-1PGDG.el6.x86_64.rpm
書き込み完了: /root/rpmbuild/RPMS/x86_64/postgresql92-plpython-9.2.4-1PGDG.el6.x86_64.rpm
書き込み完了: /root/rpmbuild/RPMS/x86_64/postgresql92-pltcl-9.2.4-1PGDG.el6.x86_64.rpm
書き込み完了: /root/rpmbuild/RPMS/x86_64/postgresql92-test-9.2.4-1PGDG.el6.x86_64.rpm
実行中(%clean): /bin/sh -e /var/tmp/rpm-tmp.iDKqDB
+ umask 022
+ cd /root/rpmbuild/BUILD
+ cd postgresql-9.2.4
+ rm -rf /root/rpmbuild/BUILDROOT/postgresql92-9.2.4-1PGDG.el6.x86_64
+ exit 0
#
#
今度はうまくいった!これでディレクトリ ~/rpmbuild/RPMS/x86_64/ にPostgreSQLの各種rpmパッケージファイルが出力されているはずだ。
4.PostgreSQLの再インストール
PostgreSQLをアンインストールし、今回ビルドしたパッケージをインストールする。まずは、PostgreSQLをアンインストールしてみる。
# yum erase postgresql92
Loaded plugins: downloadonly, fastestmirror
Setting up Remove Process
Resolving Dependencies
--> Running transaction check
---> Package postgresql92.x86_64 0:9.2.3-2PGDG.rhel6 will be erased
--> Processing Dependency: postgresql92 = 9.2.3-2PGDG.rhel6 for package: postgresql92-devel-9.2.3-2PGDG.rhel6.x86_64
--> Processing Dependency: postgresql92 = 9.2.3-2PGDG.rhel6 for package: postgresql92-server-9.2.3-2PGDG.rhel6.x86_64
--> Running transaction check
---> Package postgresql92-devel.x86_64 0:9.2.3-2PGDG.rhel6 will be erased
--> Processing Dependency: postgresql92-devel for package: 1:libpqxx-devel-4.0-0.1.rhel6.x86_64
---> Package postgresql92-server.x86_64 0:9.2.3-2PGDG.rhel6 will be erased
--> Running transaction check
---> Package libpqxx-devel.x86_64 1:4.0-0.1.rhel6 will be erased
--> Finished Dependency Resolution
Dependencies Resolved
=======================================================================================================================================================================================================================
Package Arch Version Repository Size
=======================================================================================================================================================================================================================
Removing:
postgresql92 x86_64 9.2.3-2PGDG.rhel6 @pgdg92 5.0 M
Removing for dependencies:
libpqxx-devel x86_64 1:4.0-0.1.rhel6 @pgdg92 326 k
postgresql92-devel x86_64 9.2.3-2PGDG.rhel6 @pgdg92 6.4 M
postgresql92-server x86_64 9.2.3-2PGDG.rhel6 @pgdg92 15 M
Transaction Summary
=======================================================================================================================================================================================================================
Remove 4 Package(s)
Installed size: 27 M
Is this ok [y/N]: y
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Erasing : 1:libpqxx-devel-4.0-0.1.rhel6.x86_64 1/4
Erasing : postgresql92-devel-9.2.3-2PGDG.rhel6.x86_64 2/4
Erasing : postgresql92-server-9.2.3-2PGDG.rhel6.x86_64 3/4
Erasing : postgresql92-9.2.3-2PGDG.rhel6.x86_64 4/4
Verifying : postgresql92-server-9.2.3-2PGDG.rhel6.x86_64 1/4
Verifying : postgresql92-9.2.3-2PGDG.rhel6.x86_64 2/4
Verifying : 1:libpqxx-devel-4.0-0.1.rhel6.x86_64 3/4
Verifying : postgresql92-devel-9.2.3-2PGDG.rhel6.x86_64 4/4
Removed:
postgresql92.x86_64 0:9.2.3-2PGDG.rhel6
Dependency Removed:
libpqxx-devel.x86_64 1:4.0-0.1.rhel6 postgresql92-devel.x86_64 0:9.2.3-2PGDG.rhel6 postgresql92-server.x86_64 0:9.2.3-2PGDG.rhel6
Complete!
#
#
# yum erase postgresql92-libs.x86_64
Loaded plugins: downloadonly, fastestmirror
Setting up Remove Process
Resolving Dependencies
--> Running transaction check
---> Package postgresql92-libs.x86_64 0:9.2.3-2PGDG.rhel6 will be erased
--> Processing Dependency: libpq.so.5()(64bit) for package: 1:libpqxx-4.0-0.1.rhel6.x86_64
--> Processing Dependency: libpq.so.5()(64bit) for package: php-pgsql-5.4.12-1.el6.remi.x86_64
--> Running transaction check
---> Package libpqxx.x86_64 1:4.0-0.1.rhel6 will be erased
---> Package php-pgsql.x86_64 0:5.4.12-1.el6.remi will be erased
--> Processing Dependency: php-pgsql for package: php-pear-MDB2-Driver-pgsql-1.5.0-0.7.b4.el6.remi.noarch
--> Running transaction check
---> Package php-pear-MDB2-Driver-pgsql.noarch 0:1.5.0-0.7.b4.el6.remi will be erased
--> Finished Dependency Resolution
Dependencies Resolved
=======================================================================================================================================================================================================================
Package Arch Version Repository Size
=======================================================================================================================================================================================================================
Removing:
postgresql92-libs x86_64 9.2.3-2PGDG.rhel6 @pgdg92 613 k
Removing for dependencies:
libpqxx x86_64 1:4.0-0.1.rhel6 @pgdg92 641 k
php-pear-MDB2-Driver-pgsql noarch 1.5.0-0.7.b4.el6.remi @remi 213 k
php-pgsql x86_64 5.4.12-1.el6.remi @remi 311 k
Transaction Summary
=======================================================================================================================================================================================================================
Remove 4 Package(s)
Installed size: 1.7 M
Is this ok [y/N]: y
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Erasing : php-pear-MDB2-Driver-pgsql-1.5.0-0.7.b4.el6.remi.noarch 1/4
Erasing : php-pgsql-5.4.12-1.el6.remi.x86_64 2/4
Erasing : 1:libpqxx-4.0-0.1.rhel6.x86_64 3/4
Erasing : postgresql92-libs-9.2.3-2PGDG.rhel6.x86_64 4/4
Verifying : php-pear-MDB2-Driver-pgsql-1.5.0-0.7.b4.el6.remi.noarch 1/4
Verifying : postgresql92-libs-9.2.3-2PGDG.rhel6.x86_64 2/4
Verifying : php-pgsql-5.4.12-1.el6.remi.x86_64 3/4
Verifying : 1:libpqxx-4.0-0.1.rhel6.x86_64 4/4
Removed:
postgresql92-libs.x86_64 0:9.2.3-2PGDG.rhel6
Dependency Removed:
libpqxx.x86_64 1:4.0-0.1.rhel6 php-pear-MDB2-Driver-pgsql.noarch 0:1.5.0-0.7.b4.el6.remi php-pgsql.x86_64 0:5.4.12-1.el6.remi
Complete!
#
#
依存関係で削除されたものも含めてアンインストールされたものは以下の通り。
- postgresql92
- libpqxx-devel
- postgresql92-devel
- postgresql92-server
- postgresql92-libs
- libpqxx
- php-pear-MDB2-Driver-pgsql
- php-pgsql
これらの再インストールと、pg_trgmが入っているpostgresql92-contribのインストールを行う。まずはビルドしてできたパッケージから。
# rpm -ivh postgresql92-libs-9.2.4-1PGDG.el6.x86_64.rpm
準備中... ########################################### [100%]
1:postgresql92-libs ########################################### [100%]
#
#
# rpm -ivh postgresql92-9.2.4-1PGDG.el6.x86_64.rpm
準備中... ########################################### [100%]
1:postgresql92 ########################################### [100%]
#
#
# rpm -ivh postgresql92-devel-9.2.4-1PGDG.el6.x86_64.rpm
準備中... ########################################### [100%]
1:postgresql92-devel ########################################### [100%]
#
#
# rpm -ivh postgresql92-server-9.2.4-1PGDG.el6.x86_64.rpm
準備中... ########################################### [100%]
1:postgresql92-server ########################################### [100%]
#
#
#
# rpm -ivh postgresql92-contrib-9.2.4-1PGDG.el6.x86_64.rpm
準備中... ########################################### [100%]
1:postgresql92-contrib ########################################### [100%]
#
依存関係で削除されちゃったものを再インストール
# yum --enablerepo=pgdg92 libpqxx-devel
~省略~
Installed:
libpqxx-devel.x86_64 1:4.0-0.1.rhel6
Dependency Installed:
libpqxx.x86_64 1:4.0-0.1.rhel6
Complete!
#
#
# yum --enablerepo=remi install php-pear-MDB2-Driver
~省略~
Installed:
php-pear-MDB2-Driver-pgsql.noarch 0:1.5.0-0.7.b4.el6.remi
Dependency Installed:
php-pgsql.x86_64 0:5.4.15-1.el6.remi
Dependency Updated:
php.x86_64 0:5.4.15-1.el6.remi php-cli.x86_64 0:5.4.15-1.el6.remi php-common.x86_64 0:5.4.15-1.el6.remi php-devel.x86_64 0:5.4.15-1.el6.remi php-fpm.x86_64 0:5.4.15-1.el6.remi
php-mbstring.x86_64 0:5.4.15-1.el6.remi php-pdo.x86_64 0:5.4.15-1.el6.remi php-xml.x86_64 0:5.4.15-1.el6.remi
Complete!
#
#
自動起動を設定し、起動確認
# chkconfig postgresql-9.2 on
postgresql-9.2 0:off1:off2:on3:on4:on5:on6:off
#
# /etc/init.d/postgresql-9.2 start
postgresql-9.2 サービスを開始中: [ OK ]
#
#
これで実は、再インストールの一連の作業の中ではPostgreSQLのDATAフォルダには一切影響が無く、アンインストール前のDBや設定が引き継がれているので特にDBのリストアを行ったり初期化を行ったりはしていない。ロールもそのまま使ってログインができるはずだ。ただ、PostgreSQLのアンインストール時にPHP関連も一部再インストールされてしまい、またPHP関連モジュール全部がアップグレードされてしまっており、以前に行ったpdo_pgsql、pgsqlのバージョン合わせ作業が上書きされてしまっているので、もう一度行う必要がある。これを行わない場合、PHPからPostgreSQLへ投げるSQLのエスケープの仕方次第ではトラブルが起こる可能性がある。
pg_bigmの追加手順も書こうと思ったが記事が長くなって分かりにくいので次回に譲ることにする。pg_bigmの追加は今回のような長い作業にはならない。ちょちょいのちょいで済む。尤も今回のこの作業も、実際におおなってみれば一瞬で終わるのだが、手順を文字に起こしてみると長く感じる。これでインデックスを作成したら全文検索が早くなってるといいなぁ。