去年,大约在这个时候,有一篇博客的主题是关于使用 Meson 构建 PostgreSQL。当时我对 Meson 一无所知,虽然现在也不知道更多,但由于它似乎与构建时间和获得测试结果的时间有关,所以我会看看(还有线程中提到的更严重的原因,您可以自己阅读)。
将Meson 和Ninja(默认情况下 Meson 的后端系统)安装到 Debian 11 系统上只是安装软件包(对于 Red Hat 基本系统几乎相同):
postgres@debian11pg:/home/postgres/ [pg16] sudo apt install -y meson ninja-build
复制
Meson 的一个要求是,构建必须在源代码树之外进行,因此让我们为此创建一个空目录:
postgres@debian11pg:/home/postgres/ [pg16] mkdir postgresql-build postgres@debian11pg:/home/postgres/ [pg16] cd postgresql-build/
复制
本例中 PostgreSQL 的源代码与构建目录处于同一级别:
postgres@debian11pg:/home/postgres/postgresql-build/ [pg16] pwd /home/postgres/postgresql-build 11:58:49 postgres@debian11pg:/home/postgres/postgresql-build/ [pg16] ls ../postgresql aclocal.m4 config configure configure.ac contrib COPYRIGHT doc
复制
我们(dbi)通常定义我们想要安装 PostgreSQL 的位置,段大小和块大小定义这些变量:
postgres@debian11pg:/home/postgres/postgresql-build/ [pg16] PGHOME=/var/tmp/pgsql postgres@debian11pg:/home/postgres/postgresql-build/ [pg16] SEGSIZE=2 postgres@debian11pg:/home/postgres/postgresql-build/ [pg16] BLOCKSIZE=8
复制
通常接下来是配置阶段,我们这样做:
./configure --prefix=${PGHOME} \
--exec-prefix=${PGHOME} \
--bindir=${PGHOME}/bin \
--libdir=${PGHOME}/lib \
--sysconfdir=${PGHOME}/etc \
--includedir=${PGHOME}/include \
--datarootdir=${PGHOME}/share \
--datadir=${PGHOME}/share \
--with-pgport=5432 \
--with-perl \
--with-python \
--with-openssl \
--with-pam \
--with-ldap \
--with-libxml \
--with-libxslt \
--with-segsize=${SEGSIZE} \
--with-blocksize=${BLOCKSIZE} \
--with-llvm LLVM_CONFIG='/usr/bin/llvm-config' \
--with-uuid=ossp \
--with-lz4 \
--with-zstd \
--with-gssapi \
--with-systemd \
--with-icu \
--with-system-tzdata=/usr/share/zoneinfo
复制
有了 Meson ,这种情况发生了变化。首先,我们需要初始化构建目录(第一个点是构建目录,第二个路径是源代码的位置):
postgres@debian11pg:/home/postgres/postgresql-build/ [pg16] meson setup . ../postgresql The Meson build system Version: 0.56.2 Source dir: /home/postgres/postgresql Build dir: /home/postgres/postgresql-build Build type: native build Project name: postgresql Project version: 16devel C compiler for the host machine: cc (gcc 10.2.1 "cc (Debian 10.2.1-6) 10.2.1 20210110") C linker for the host machine: cc ld.bfd 2.35.2 Host machine cpu family: x86_64 ... Program xmllint found: NO Configuring version.sgml using configuration Message: checking for file conflicts between source and build directory Build targets in project: 384 Found ninja-1.10.1 at /usr/bin/ninja
复制
我们可能已经在设置阶段配置了构建选项,但现在我们可以轻松检查可用的内容:
postgres@debian11pg:/home/postgres/postgresql-build/ [pg16] meson configure
Core properties:
Source dir /home/postgres/postgresql
Build dir /home/postgres/postgresql-build
Main project options:
Core options Current Value Possible Values Description
------------ ------------- --------------- -----------
auto_features auto [enabled, disabled, auto] Override value of all 'auto' features
backend ninja [ninja, vs, vs2010, vs2015, vs2017, vs2019, xcode] Backend to use
buildtype release [plain, debug, debugoptimized, release, minsize, custom] Build type to use
debug false [true, false] Debug
default_library shared [shared, static, both] Default library type
force_fallback_for [] Force fallback for those subprojects
install_umask 0022 [preserve, 0000-0777] Default umask to apply on permissions of installed files
layout mirror [mirror, flat] Build directory layout
optimization 3 [0, g, 1, 2, 3, s] Optimization level
strip false [true, false] Strip targets on install
unity off [on, off, subprojects] Unity build
unity_size 4 >=2 Unity block size
warning_level 1 [0, 1, 2, 3] Compiler warning level to use
werror false [true, false] Treat warnings as errors
wrap_mode default [default, nofallback, nodownload, forcefallback] Wrap mode
cmake_prefix_path [] List of additional prefixes for cmake to search
pkg_config_path [] List of additional paths for pkg-config to search
Backend options Current Value Possible Values Description
--------------- ------------- --------------- -----------
backend_max_links 0 >=0 Maximum number of linker processes to run or 0 for no limit
Base options Current Value Possible Values Description
------------ ------------- --------------- -----------
b_asneeded true [true, false] Use -Wl,--as-needed when linking
b_colorout always [auto, always, never] Use colored output
b_coverage false [true, false] Enable coverage tracking.
b_lto false [true, false] Use link time optimization
b_lundef true [true, false] Use -Wl,--no-undefined when linking
b_ndebug false [true, false, if-release] Disable asserts
b_pch false [true, false] Use precompiled headers
b_pgo off [off, generate, use] Use profile guided optimization
b_pie false [true, false] Build executables as position independent
b_sanitize none [none, address, thread, undefined, memory, address,undefined] Code sanitizer to use
b_staticpic true [true, false] Build static libraries as position independent
Compiler options Current Value Possible Values Description
---------------- ------------- --------------- -----------
c_args [] Extra arguments passed to the c compiler
c_link_args [] Extra arguments passed to the c linker
c_std none [none, c89, c99, c11, c17, c18, c2x, gnu89, gnu99, gnu11, C language standard to use
gnu17, gnu18, gnu2x]
Directories Current Value Possible Values Description
----------- ------------- --------------- -----------
bindir bin Executable directory
datadir share Data file directory
includedir include Header file directory
infodir share/info Info page directory
libdir lib64 Library directory
libexecdir libexec Library executable directory
localedir share/locale Locale data directory
localstatedir var Localstate data directory
mandir share/man Manual page directory
prefix /usr/local/pgsql Installation prefix
sbindir sbin System executable directory
sharedstatedir com Architecture-independent data directory
sysconfdir etc Sysconf data directory
Testing options Current Value Possible Values Description
--------------- ------------- --------------- -----------
errorlogs true [true, false] Whether to print the logs from failing tests
stdsplit true [true, false] Split stdout and stderr in test logs
Project options Current Value Possible Values Description
--------------- ------------- --------------- -----------
BISON [bison, win_bison] path to bison binary
DTRACE dtrace path to dtrace binary
FLEX [flex, win_flex] path to flex binary
GZIP gzip path to gzip binary
LZ4 lz4 path to lz4 binary
PERL perl path to perl binary
PG_TEST_EXTRA Enable selected extra tests
PROVE prove path to prove binary
PYTHON [python3, python] path to python binary
SED gsed path to sed binary
TAR tar path to tar binary
XMLLINT xmllint path to xmllint binary
XSLTPROC xsltproc path to xsltproc binary
ZIC zic path to zic binary, when cross-compiling
ZSTD zstd path to zstd binary
atomics true [true, false] whether to use atomic operations
blocksize 8 [1, 2, 4, 8, 16, 32] set relation block size in kB
bonjour auto [enabled, disabled, auto] build with Bonjour support
bsd_auth auto [enabled, disabled, auto] build with BSD Authentication support
cassert false [true, false] enable assertion checks (for debugging)
darwin_sysroot select a non-default sysroot path
dtrace disabled [enabled, disabled, auto] DTrace support
extra_include_dirs [] non-default directories to be searched for headers
extra_lib_dirs [] non-default directories to be searched for libs
extra_version append STRING to the PostgreSQL version number
gssapi auto [enabled, disabled, auto] GSSAPI support
icu auto [enabled, disabled, auto] ICU support
krb_srvnam postgres Default Kerberos service principal for GSSAPI
ldap auto [enabled, disabled, auto] LDAP support
libedit_preferred false [true, false] Prefer BSD Libedit over GNU Readline
libxml auto [enabled, disabled, auto] XML support
libxslt auto [enabled, disabled, auto] XSLT support in contrib/xml2
llvm disabled [enabled, disabled, auto] whether to use llvm
lz4 auto [enabled, disabled, auto] LZ4 support
nls auto [enabled, disabled, auto] native language support
pam auto [enabled, disabled, auto] build with PAM support
pgport 5432 >=1, <=65535 Default port number for server and clients
plperl auto [enabled, disabled, auto] build Perl modules (PL/Perl)
plpython auto [enabled, disabled, auto] build Python modules (PL/Python)
pltcl auto [enabled, disabled, auto] build with TCL support
readline auto [enabled, disabled, auto] use GNU Readline or BSD Libedit for editing
segsize 1 Segment size, in gigabytes
selinux disabled [enabled, disabled, auto] build with SELinux support
spinlocks true [true, false] whether to use spinlocks
ssl none [none, openssl] use LIB for SSL/TLS support (openssl)
system_tzdata use system time zone data in specified directory
systemd auto [enabled, disabled, auto] build with systemd support
tap_tests auto [enabled, disabled, auto] Whether to enable tap tests
tcl_version tcl specify TCL version
uuid none [none, bsd, e2fs, ossp] build contrib/uuid-ossp using LIB
wal_blocksize 8 [1, 2, 4, 8, 16, 32, 64] WAL block size, in kilobytes
zlib auto [enabled, disabled, auto] whether to use zlib
zstd auto [enabled, disabled, auto] whether to use zstd
复制
这是一个非常好的、人类可读的、可用选项的概述。为了获得与上面的“./configure”所做的类似的东西,我们需要这个:
postgres@debian11pg:/home/postgres/postgresql-build/ [pg16] meson configure -Dprefix=${PGHOME} \ -Dbindir=${PGHOME}/bin \ -Ddatadir=${PGHOME}/share \ -Dincludedir=${PGHOME}/include \ -Dlibdir=${PGHOME}/lib \ -Dsysconfdir=${PGHOME}/etc \ -Dpgport=5432 \ -Dplperl=enabled \ -Dplpython=enabled \ -Dssl=openssl \ -Dpam=enabled \ -Dldap=enabled \ -Dlibxml=enabled \ -Dlibxslt=enabled \ -Dsegsize=${SEGSIZE} \ -Dblocksize=${BLOCKSIZE} \ -Dllvm=enabled \ -Duuid=ossp \ -Dzstd=enabled \ -Dlz4=enabled \ -Dgssapi=enabled \ -Dsystemd=enabled \ -Dicu=enabled \ -Dsystem_tzdata=/usr/share/zoneinfo
复制
就是这样,准备编译(默认检测并行):
postgres@debian11pg:/home/postgres/postgresql-build/ [pg16] ninja [0/1] Regenerating build files. The Meson build system Version: 0.56.2 Source dir: /home/postgres/postgresql Build dir: /home/postgres/postgresql-build Build type: native build Project name: postgresql Project version: 16devel ... Configuring version.sgml using configuration Message: checking for file conflicts between source and build directory Build targets in project: 390 Option prefix is: /var/tmp/pgsql [default: /usr/local/pgsql] Found ninja-1.10.1 at /usr/bin/ninja [236/1876] Compiling C object src/interfaces/libpq/libpq.a.p/fe-print.c.o [1876/1876] Linking target src/interfaces/ecpg/test/thread/alloc
复制
准备安装:
postgres@debian11pg:/home/postgres/postgresql-build/ [pg16] ninja install
[0/1] Installing files.
Installing subdir /home/postgres/postgresql/src/include/access to /var/tmp/pgsql/include/postgresql/server/access
Installing /home/postgres/postgresql/src/include/access/rmgrlist.h to /var/tmp/pgsql/include/postgresql/server/access
Installing /home/postgres/postgresql/src/include/access/attmap.h to /var/tmp/pgsql/include/postgresql/server/access
Installing /home/postgres/postgresql/src/include/access/xlogrecovery.h to /var/tmp/pgsql/include/postgresql/server/access
Installing /home/postgres/postgresql/src/include/access/xlogarchive.h to /var/tmp/pgsql/include/postgresql/server/access
...
Installing /home/postgres/postgresql/src/test/perl/PostgreSQL/Test/Utils.pm to /var/tmp/pgsql/lib/postgresql/pgxs/src/test/perl/PostgreSQL/Test
Installing /home/postgres/postgresql/src/test/perl/PostgreSQL/Test/SimpleTee.pm to /var/tmp/pgsql/lib/postgresql/pgxs/src/test/perl/PostgreSQL/Test
Installing /home/postgres/postgresql/src/test/perl/PostgreSQL/Test/RecursiveCopy.pm to /var/tmp/pgsql/lib/postgresql/pgxs/src/test/perl/PostgreSQL/Test
Installing /home/postgres/postgresql/src/test/perl/PostgreSQL/Test/Cluster.pm to /var/tmp/pgsql/lib/postgresql/pgxs/src/test/perl/PostgreSQL/Test
20:07:21 postgres@debian11pg:/home/postgres/postgresql-build/ [pg16]
复制
很简单,一切都在那里:
postgres@debian11pg:/home/postgres/postgresql-build/ [pg16] /var/tmp/pgsql/bin/initdb -D /var/tmp/dummy
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
creating directory /var/tmp/dummy ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Europe/Zurich
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
initdb: warning: enabling "trust" authentication for local connections
initdb: hint: You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb.
Success. You can now start the database server using:
/var/tmp/pgsql/bin/pg_ctl -D /var/tmp/dummy -l logfile start
postgres@debian11pg:/home/postgres/postgresql-build/ [pg16] export PGPORT=8888
postgres@debian11pg:/home/postgres/postgresql-build/ [pg16] /var/tmp/pgsql/bin/pg_ctl -D /var/tmp/dummy start
waiting for server to start....2022-10-18 20:10:29.674 CEST [8469] LOG: starting PostgreSQL 16devel on x86_64, compiled by gcc-10.2.1
2022-10-18 20:10:29.675 CEST [8469] LOG: listening on IPv6 address "::1", port 8888
2022-10-18 20:10:29.675 CEST [8469] LOG: listening on IPv4 address "127.0.0.1", port 8888
2022-10-18 20:10:29.689 CEST [8469] LOG: listening on Unix socket "/tmp/.s.PGSQL.8888"
2022-10-18 20:10:29.718 CEST [8472] LOG: database system was shut down at 2022-10-18 20:09:40 CEST
2022-10-18 20:10:29.735 CEST [8469] LOG: database system is ready to accept connections
done
server started
postgres@debian11pg:/home/postgres/postgresql-build/ [pg16] /var/tmp/pgsql/bin/psql
psql (16devel)
Type "help" for help.
postgres=# select version();
version
------------------------------------------------------
PostgreSQL 16devel on x86_64, compiled by gcc-10.2.1
(1 row)
复制
没有比传统方式更多的工作,但我喜欢各个步骤的输出。它更容易阅读。我没有做任何测试,如果这真的更快并且没有看这种方法的其他优点。也许这将成为后续帖子的主题。这应该给你足够的信息来开始。
最后,PostgreSQL wiki 中的“旧”命令与“新”命令进行了很好的比较。问题是:“旧”是否意味着构建 PostgreSQL 的传统方式将消失?查看 Wiki 页面的底部以了解可能发生的情况。
原文标题:Building PostgreSQL with Meson
原文作者:Daniel Westermann
原文地址:https://www.dbi-services.com/blog/building-postgresql-with-meson/