编译 `PostGIS` 新版 against PostgreSQL 15
解读
面试官抛出“编译 PostGIS 新版 against PostgreSQL 15”并不是让你背命令,而是考察国内真实交付场景下的三条能力链:
- 能否在国产操作系统(麒麟、统信 UOS、Anolis、openEuler)与信创 ARM/x86 双架构上,把 PostgreSQL 15 的-devel 包、proj、gdal、geos、protobuf-c、json-c 等依赖一次性找全;
- 是否理解 PostGIS 3.3+ 引入的CMake 构建体系与旧版 GNU Autotools 差异,能否在无外网的隔离机房内用本地 yum/apt 源、conda 离线包或自制 rpmbuild/spec 完成 reproducible build;
- 是否具备运行时验证思维:编译成功后,能否用
SELECT postgis_full_version()打印出符合信创验收标准的“USE_STATS=1 PROJ=9.3 GDAL=3.6 GEOS=3.11 SFCGAL=1.4.1”字样,并解释每一条依赖的ABI 兼容性与国家安全合规(国密算法、GIS 数据不出境)要求。
答不到这三层,只能算“能跑”,拿不到“能交付”的评分。
知识点
- PostgreSQL 15 扩展接口:PGXS 已被 CMake 取代,需掌握
pg_config --pgxs与find_package(PostgreSQL 15 REQUIRED)的混用边界。 - 国产 CPU 架构陷阱:ARM64 下 GEOS 必须打 “-DCMAKE_CXX_FLAGS=-fsigned-char” 补丁,否则 WKB 解析因 char 默认 unsigned 而错位。
- 隔离机房依赖闭环:proj-data 的
proj-datumgrid-1.8.zip需提前放至/usr/local/share/proj,否则make check时 396 个 case 直接失败,信创验收一票否决。 - 运行时符号冲突:PostGIS 3.3 默认链接 protobuf-c,若系统自带 libprotobuf 与 Anaconda 的 libprotobuf 版本分叉,会在
CREATE EXTENSION postgis;时报 “symbol protobuf_c_version” 未定义,需LD_PRELOAD或rpath固化。 - 性能基线:编译后必须跑通
tools/geography_benchmark与raster/load_outdb.py,确保 GIS 索引构建耗时与官方 x86_64 基线偏差 < 5%,否则云厂商 SLA 无法签字。
答案
-
准备离线依赖(以 Anolis OS 8.6 ARM64 为例)
a) 在能联网的构建机用yumdownloader --resolve拉齐:
postgresql15-devel-15.4-1.an8.aarch64.rpm
cmake3-3.23 gcc-toolset-12-gcc-c++ geos-devel-3.11 gdal-devel-3.6 proj-devel-9.3 protobuf-c-devel json-c-devel sfcgal-devel
b) 打包成本地 repo,createrepo 后刻录至光盘,带入隔离机房。 -
源码获取与校验
wget -c https://download.osgeo.org/postgis/source/postgis-3.3.4.tar.gz sha256sum postgis-3.3.4.tar.gz | grep -q 6a3b6a1d7f3e3b9a8e8e8e8e8e8e8e8e8e8e8e8e8e8e8e8e8e8e8e8e8e8e8e8e8 -
CMake 交叉编译(信创 ARM64)
export CC=/opt/rh/gcc-toolset-12/root/usr/bin/gcc export CXX=/opt/rh/gcc-toolset-12/root/usr/bin/g++ cmake -B build \ -DCMAKE_BUILD_TYPE=Release \ -DPostgreSQL_TYPE_INCLUDE_DIR=/usr/pgsql-15/include/server \ -DPROJ_INCLUDE_DIR=/usr/local/include \ -DGEOS_INCLUDE_DIR=/usr/local/include \ -DGDAL_INCLUDE_DIR=/usr/local/include \ -DProtobuf_INCLUDE_DIR=/usr/local/include \ -DCMAKE_INSTALL_PREFIX=/usr/pgsql-15 \ -DCMAKE_CXX_FLAGS="-fsigned-char -O3 -march=armv8.2-a+crc+fp16" cmake --build build -j$(nproc) -
离线回归测试
export PGDATA=/var/lib/pgsql/15/data pg_ctl -D $PGDATA start make -C build check RUNTESTFLAGS="--verbose --datadir=/usr/local/share/proj"确保 396/396 tests passed,零失败方可继续。
-
安装与版本固化
cmake --install build echo '/usr/pgsql-15/lib' > /etc/ld.so.conf.d/postgis-3.3.conf ldconfig在目标库执行:
CREATE EXTENSION postgis VERSION '3.3.4'; SELECT postgis_full_version();返回行必须包含 "POSTGIS="3.3.4 3.3.4" [EXTENSION] PGSQL="150" GEOS="3.11.0-CAPI-1.17.0" PROJ="9.3.0" GDAL="GDAL 3.6.2" LIBXML="2.9.13" LIBJSON="0.15" LIBPROTOBUF="1.4.1" (core procs from "3.3.4" need upgrade)" 字样,方可签字验收。
-
打包 rpm 供批量部署
rpmbuild -ba postgis33-15.spec \ --define 'pgmajor 15' \ --define 'postgismajor 3.3' \ --define '_topdir /root/rpmbuild'产出 postgis33_15-3.3.4-1.an8.aarch64.rpm 与 postgis33_15-debuginfo-3.3.4-1.an8.aarch64.rpm,上传至信创 yum 源,完成闭环。
拓展思考
- 若客户要求国密 SM4 加密 GIS 坐标,需把 PostGIS 的
lwgeom_median等核心函数改为调用国密库,重新编译后如何验证空间精度误差仍在 1 mm 以内? - 在云原生场景下,PostGIS 作为 sidecar 容器镜像,如何基于 multi-stage build 把最终镜像压到 120 MB 以内,且保留 proj-data 最小子集(仅含中国区域 108 个网格)?
- PostgreSQL 16 已移除
heap_parallelscan私有符号,PostGIS 3.4 nightly 出现 “undefined symbol” 链接失败,如何在不回退大版本的前提下,用 dlsym RTLD_DEFAULT 动态解析并兼容 15/16 双版本?