PostgreSQL
 sql >> Base de données >  >> RDS >> PostgreSQL

Compilation de l'extension pg_repack au format binaire de l'installation de PostgreSQL

Ce blog concerne la compilation de l'extension pg_repack au format binaire de l'installation de PostgreSQL. La plupart d'entre vous le savent, l'extension pg_repack est l'une des extensions PostgreSQL bien connues, elle est particulièrement utilisée pour récupérer de l'espace [bloats] EN LIGNE sans détenir un VERROU EXCLUSIF sur les tables/index.

Pour activer l'extension pg_repack dans la base de données PostgreSQL, elle doit être compilée à partir des sources. C'est assez facile et simple à compiler à partir de la source sur toutes les variantes installées (source, rpm, binaire) de PostgreSQL, mais c'est légèrement différent si c'est avec le format binaire de PostgreSQL [One Click Installer] car ils sont pré-construits bundle binaire avec dépendance bibliothèques. Compilons et voyons.

Sur la machine virtuelle CentOS 7, j'ai installé le format binaire de PostgreSQL 9.4 (lien de téléchargement) et son répertoire personnel "/opt/PostgreSQL/9.4/". Ensuite, nous devons télécharger la source de pg_repack depuis leur site officiel.

[root@localhost ~]# git clone https://github.com/reorg/pg_repack.git

Avant de compiler, pg_config de PostgreSQL 9.4 doit être défini dans le PATH.

[root@localhost pg_repack]# export PATH=/opt/PostgreSQL/9.4/bin:$PATH
[root@localhost pg_repack]# type pg_config
pg_config is /opt/PostgreSQL/9.4/bin/pg_config

Nous sommes maintenant prêts à exécuter les commandes d'installation source "make" et "make install". Exécutons "make"

[root@localhost ~]# cd pg_repack/
[root@localhost pg_repack]# make
make[1]: Entering directory `/root/pg_repack/bin'
....
....
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -O2 pg_repack.o pgut/pgut.o pgut/pgut-fe.o -L/opt/PostgreSQL/9.4/lib -lpq -L/opt/PostgreSQL/9.4/lib -L/opt/local/Current/lib -Wl,--as-needed -Wl,-rpath,'/opt/PostgreSQL/9.4/lib',--enable-new-dtags -lpgcommon -lpgport -lssl -lcrypto -lgssapi_krb5 -lz -ledit -lrt -lcrypt -ldl -lm -o pg_repack
/usr/bin/ld: cannot find -ledit
collect2: ld returned 1 exit status
make[1]: *** [pg_repack] Error 1
make[1]: Leaving directory `/root/pg_repack/bin'
make: *** [all] Error 2

Oups… !!, il semble qu'il y ait une erreur liée à la bibliothèque -ledit (libedit) manquante dans le répertoire lib de PostgreSQL. Exécutons la même commande "ld -ledit" en mode verbeux (-verbose) pour plus d'informations, essayé et échoué par la commande "make".

[root@localhost pg_repack]# ld -ledit -verbose
GNU ld version 2.20.51.0.2-5.42.el6 20100205
...
...
...
==================================================
attempt to open /usr/x86_64-redhat-linux/lib64/libedit.so failed
attempt to open /usr/x86_64-redhat-linux/lib64/libedit.a failed
attempt to open /usr/local/lib64/libedit.so failed
attempt to open /usr/local/lib64/libedit.a failed
attempt to open /lib64/libedit.so failed
attempt to open /lib64/libedit.a failed
attempt to open /usr/lib64/libedit.so failed
attempt to open /usr/lib64/libedit.a failed
attempt to open /usr/x86_64-redhat-linux/lib/libedit.so failed
attempt to open /usr/x86_64-redhat-linux/lib/libedit.a failed
attempt to open /usr/lib64/libedit.so failed
attempt to open /usr/lib64/libedit.a failed
attempt to open /usr/local/lib/libedit.so failed
attempt to open /usr/local/lib/libedit.a failed
attempt to open /lib/libedit.so failed
attempt to open /lib/libedit.a failed
attempt to open /usr/lib/libedit.so failed
attempt to open /usr/lib/libedit.a failed
ld: cannot find -ledit

Bon, maintenant il est clair qu'il recherche la bibliothèque libedit.so dans le répertoire lib de PostgreSQL [/opt/PostgreSQL/9.4/lib]. Vérifions la bibliothèque dans ce répertoire.

[root@localhost pg_repack]# cd /opt/PostgreSQL/9.4/lib
[root@localhost lib]# ls -l libedit*
-rwxr-xr-x. 1 root daemon 254702 Mar 22 23:32 libedit.so.0

Ah, nous avons "libedit.so.0" mais pas "libedit.so" requis par la commande "make". La création d'un lien symbolique devrait être une solution rapide.

[root@localhost lib]# ln -s libedit.so.0 libedit.so
[root@localhost lib]# ls -l libedit*
lrwxrwxrwx. 1 root root 12 May 19 22:25 libedit.so -> libedit.so.0
-rwxr-xr-x. 1 root daemon 254702 Mar 22 23:32 libedit.so.0

Réexécutez la commande "make".

[root@localhost pg_repack]# make
make[1]: Entering directory `/root/pg_repack/bin'
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -O2 pg_repack.o pgut/pgut.o pgut/pgut-fe.o -L/opt/PostgreSQL/9.4/lib -lpq -L/opt/PostgreSQL/9.4/lib -L/opt/local/Current/lib -Wl,--as-needed -Wl,-rpath,'/opt/PostgreSQL/9.4/lib',--enable-new-dtags -lpgcommon -lpgport -lssl -lcrypto -lgssapi_krb5 -lz -ledit -lrt -lcrypt -ldl -lm -o pg_repack
/usr/bin/ld: warning: libssl.so.1.0.0, needed by /opt/PostgreSQL/9.4/lib/libpq.so, may conflict with libssl.so.10
/lib64/libldap_r-2.4.so.2: undefined reference to `ber_sockbuf_io_udp'
collect2: ld returned 1 exit status
make[1]: *** [pg_repack] Error 1
make[1]: Leaving directory `/root/pg_repack/bin'
make: *** [all] Error 2

Oups…!!! intéressant, il a réussi l'erreur -ledit et maintenant son rapport sur la bibliothèque libldap [ libldap_r-2.4.so.2 ]. Je ne sais pas pourquoi il recherche dans le répertoire /lib64 lorsque mon pg_config pointe vers /opt/PostgreSQL/9.4/lib. Vérifions ce que nous avons dans les deux emplacements.

[root@localhost pg_repack]# ls -l /lib64/libldap*
lrwxrwxrwx. 1 root root 21 Jan 6 22:05 libldap-2.4.so.2 -> libldap-2.4.so.2.10.2
-rwxr-xr-x. 1 root root 329696 Oct 15 2014 libldap-2.4.so.2.10.2
lrwxrwxrwx. 1 root root 23 May 19 06:43 libldap_r-2.4.so.2 -> libldap_r-2.4.so.2.10.2
-rwxr-xr-x. 1 root root 351920 Oct 15 2014 libldap_r-2.4.so.2.10.2

[root@localhost pg_repack]# ls -l /opt/PostgreSQL/9.4/lib/libldap*
-rwxr-xr-x. 1 root daemon 404761 Mar 22 23:32 /opt/PostgreSQL/9.4/lib/libldap-2.4.so.2
-rwxr-xr-x. 1 root daemon 442657 Mar 22 23:32 /opt/PostgreSQL/9.4/lib/libldap_r-2.4.so.2

Il semble qu'il existe deux copies de "libldap_r-2.4.so.2", une sous forme de lien symbolique et une autre sous forme de copie papier. Je suppose que c'est dû à plusieurs copies de la bibliothèque, supprimons le lien symbolique et conservons une copie papier de la bibliothèque et réessayons.

[root@localhost lib64]# unlink libldap_r-2.4.so.2

[root@localhost pg_repack]# make
make[1]: Entering directory `/root/pg_repack/bin'
....
....
....
sed 's,REPACK_VERSION,1.3.1,g' pg_repack.sql.in > pg_repack--1.3.1.sql;
sed 's,REPACK_VERSION,1.3.1,g' pg_repack.control.in > pg_repack.control
make[1]: Leaving directory `/root/pg_repack/lib'
make[1]: Entering directory `/root/pg_repack/regress'
make[1]: Nothing to be done for `all'.
make[1]: Leaving directory `/root/pg_repack/regress'

wow… enfin, il a compilé. Exécutez "make install" pour les binaires et bibliothèques pg_repack.

[root@localhost pg_repack]# make install
make[1]: Entering directory `/root/pg_repack/bin'
/bin/mkdir -p '/opt/PostgreSQL/9.4/bin'
/usr/bin/install -c pg_repack '/opt/PostgreSQL/9.4/bin'
make[1]: Leaving directory `/root/pg_repack/bin'
make[1]: Entering directory `/root/pg_repack/lib'
/bin/mkdir -p '/opt/PostgreSQL/9.4/lib/postgresql'
/bin/mkdir -p '/opt/PostgreSQL/9.4/share/postgresql/extension'
/bin/mkdir -p '/opt/PostgreSQL/9.4/share/postgresql/extension'
/usr/bin/install -c -m 755 pg_repack.so '/opt/PostgreSQL/9.4/lib/postgresql/pg_repack.so'
/usr/bin/install -c -m 644 pg_repack.control '/opt/PostgreSQL/9.4/share/postgresql/extension/'
/usr/bin/install -c -m 644 pg_repack--1.3.1.sql pg_repack.control '/opt/PostgreSQL/9.4/share/postgresql/extension/'
make[1]: Leaving directory `/root/pg_repack/lib'
make[1]: Entering directory `/root/pg_repack/regress'
make[1]: Nothing to be done for `install'.
make[1]: Leaving directory `/root/pg_repack/regress'

Après la compilation, il y aura l'utilitaire pg_repack dans $PGHOME/bin et la bibliothèque pg_repack.so dans le répertoire $PGHOME/lib/postgresql/.

[root@localhost pg_repack]# ls -l /opt/PostgreSQL/9.4/bin/pg_rep*
-rwxr-xr-x. 1 root root 84030 May 20 00:07 /opt/PostgreSQL/9.4/bin/pg_repack

[root@localhost postgresql]# ls -l /opt/PostgreSQL/9.4/lib/postgresql/pg_rep*
-rwxr-xr-x. 1 root root 31028 May 20 00:07 /opt/PostgreSQL/9.4/lib/postgresql/pg_repack.so

Nous sommes maintenant prêts à créer l'extension pg_repack dans la base de données.

-bash-4.1$ psql
Password:
psql.bin (9.4.1)
Type "help" for help.

postgres=# select * from pg_available_extensions where name='pg_repack';
name | default_version | installed_version | comment
-----------+-----------------+-------------------+--------------------------------------------------------------
pg_repack | 1.3.1 | 1.3.1 | Reorganize tables in PostgreSQL databases with minimal locks
(1 row)

postgres=# create extension pg_repack;
CREATE EXTENSION

De même, j'ai tenté de compiler avec le produit EnterpriseDB PostgresPlus Advanced Server 9.4 [PPAS] un paquet binaire pré-construit. Face à des problèmes de bibliothèque similaires, j'ai donc utilisé l'option de l'éditeur de liens "LDFLAGS" pointant vers le répertoire de la bibliothèque et compilé.

[root@localhost ~]# export PATH=/opt/PostgresPlus/9.4AS/bin:$PATH
[root@localhost ~]# export LDFLAGS=-L/opt/PostgresPlus/9.4AS/lib
[root@localhost ~]# cd pg_repack/
[root@localhost pg_repack]# make
[root@localhost pg_repack]# make install


[root@localhost pg_repack]# ls -l /opt/PostgresPlus/9.4AS/bin/pg_rep*
-rwxr-xr-x. 1 root root 201877 May 15 11:06 /opt/PostgresPlus/9.4AS/bin/pg_repack
[root@localhost pg_repack]# ls -l /opt/PostgresPlus/9.4AS/lib/pg_rep*
-rwxr-xr-x. 1 root root 94516 May 15 11:06 /opt/PostgresPlus/9.4AS/lib/pg_repack.so

Cool, cela aussi compilé en douceur. Créez maintenant une extension dans PPAS 9.4

-bash-4.1$ psql
Welcome
psql.bin (9.4.1.4)
Type "help" for help.

edb=# create extension pg_repack;
CREATE EXTENSION
edb=#

Merci.