PostgreSQL
 sql >> Base de Dados >  >> RDS >> PostgreSQL

Compilando a extensão pg_repack no formato binário da instalação do PostgreSQL

Este blog é sobre a compilação da extensão pg_repack no formato binário da instalação do PostgreSQL. A maioria de vocês sabe, a extensão pg_repack é uma das extensões bem conhecidas do PostgreSQL, é especialmente usada para recuperar espaço [bloats] ONLINE sem segurar um EXCLUSIVE LOCK em Tables/Indexes.

Para habilitar a extensão pg_repack no banco de dados PostgreSQL, ela deve ser compilada a partir de fontes. É bastante fácil e simples de compilar a partir da fonte em qualquer variante instalada (fonte, rpm, binário) do PostgreSQL, no entanto, é um pouco diferente se for com o formato binário do PostgreSQL [One Click Installer], pois são pacotes binários pré-construídos com dependência bibliotecas. Vamos compilar e ver.

Na Máquina Virtual CentOS 7, instalei o formato binário do PostgreSQL 9.4 (link para download) e seu diretório inicial “/opt/PostgreSQL/9.4/”. Em seguida, precisamos baixar a fonte pg_repack de seu site oficial.
[root@localhost ~]# git clone https://github.com/reorg/pg_repack.git

Antes de compilar, o pg_config do PostgreSQL 9.4 deve ser configurado no 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

Agora estamos prontos para executar os comandos de instalação de origem “make” e “make install”. Vamos executar “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

Ops…!!, parece que há um erro relacionado à biblioteca -ledit(libedit) ausente no diretório lib do PostgreSQL. Vamos executar o mesmo comando “ld -ledit” no modo detalhado (-verbose) para obter mais informações, tentado e falhado pelo comando “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

Ok, agora está claro que está procurando a biblioteca libedit.so no diretório lib do PostgreSQL [/opt/PostgreSQL/9.4/lib]. Vamos verificar a biblioteca nesse diretório.
[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, temos “libedit.so.0” mas não “libedit.so” requerido pelo comando “make”. Criar um link simbólico deve ser uma solução rápida.
[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

Execute novamente o comando “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

Opa…!!! interessante, ele passou o erro -ledit e agora está relatando na biblioteca libldap [ libldap_r-2.4.so.2 ]. Não sei por que está procurando no diretório /lib64 quando meu pg_config aponta para /opt/PostgreSQL/9.4/lib. Vamos verificar o que temos em ambos os locais.
[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

Parece que existem duas cópias de “libldap_r-2.4.so.2”, uma na forma de link simbólico e outra em papel. Estou supondo que é devido a várias cópias da biblioteca, vamos remover o link simbólico e reter uma cópia impressa da biblioteca e tentar novamente.
[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'

uau… finalmente, compilou. Execute “make install” para os binários e bibliotecas 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'

Após a compilação, haverá o utilitário pg_repack em $PGHOME/bin e a biblioteca pg_repack.so no diretório $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

Agora estamos configurados para criar a extensão pg_repack dentro do banco de dados.
-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

Da mesma forma, tentei compilar com o produto EnterpriseDB PostgresPlus Advanced Server 9.4[PPAS] um pacote binário pré-construído. Enfrentei problemas de biblioteca semelhantes, portanto, usei a opção de vinculador “LDFLAGS” apontando para o diretório da biblioteca e compilei.
[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

Legal, isso também compilado sem problemas. Agora crie extensão no 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=#

Obrigada.