因为搭建 Lobe Chat 服务端数据库部署,仅支持 Postgres 数据库,并且需要 pgvector 插件。我之前使用的是 OpenWrt 系统软件包中自带的 Postgres 数据库,并且软件包内不提供其他相关插件安装,我寻思自己编译一个。

准备阶段

因为我使用的是 iStoreOS,从 issues 中搜到了官方提供的 feeds 和 config。issues #1704

开一个虚拟机准备下载源码。

git clone https://github.com/istoreos/istoreos.git
cd istoreos
wget https://fw0.koolcenter.com/iStoreOS/x86_64_efi/feeds.conf
wget https://fw0.koolcenter.com/iStoreOS/x86_64_efi/config.seed
cp config.seed .config
./scripts/feeds update -a
./scripts/feeds install -a
make menuconfig
# 选择 Utilities -> Database -> pgsql-cli,pgsql-cli-extra,pgsql-server 保存退出

1733069194273.png

make download -j8
make -j8 V=s

先尝试编译一遍,不着急改东西,静等...

1733069716476.png

修改打包配置

等了很久后,第一遍打包成功,尝试修改打包配置,搜一下配置文件在哪里。

vagrant@bookworm:~/istoreos$ find . -name *postgres*
...
./feeds/packages/libs/postgresql
./feeds/packages/libs/postgresql/files/postgresql.init
./feeds/packages/libs/postgresql/files/postgresql.config
./feeds/packages/libs/postgresql/files/postgresql.sh
...

看一下这个 feeds 里面的就好了,这里本人不太熟练,原本是想要打包成插件的形式,单独的 plugin 的 ipk 包,但是折腾了许久,均倒在编译上面。最终在 pgvector 仓库被作者关闭的两个 PR 中找到了可以和 contrib 一起作为自带插件进行打包。#98

下载 v0.8.0, 直接本地修改包内的 Makefile 文件,按照 PR 的同步一下修改,发现 contrib 其他的插件也是这样的结构,会判断 USE_PGXS。修改后上传到 ./dl/ 文件夹下面,文件名 pgvector-0.8.0.tar.gz,与其他源代码呆在一起。

对于 ./feeds/packages/libs/postgresql/Makefile 修改一下,添加一个准备阶段将插件包解压进去。

#...

define Build/Prepare
	$(call Build/Prepare/Default)
	rm -rf $(PKG_BUILD_DIR)/contrib/pgvector && \
	    mkdir $(PKG_BUILD_DIR)/contrib/pgvector && \
        $(TAR) -xzf $(DL_DIR)/pgvector-0.8.0.tar.gz --strip-components=1 -C $(PKG_BUILD_DIR)/contrib/pgvector && \
        sed -i 's/vacuumlo/vacuumlo pgvector/' $(PKG_BUILD_DIR)/contrib/Makefile
endef

#...

重新 make -j8 V=s 打包。

1733070563122.png
1733070596160.png

完美,将自己打包的 ipk 重新安装上,看一下效果。

启动插件

Navicat 连一下 OpenWrt 的 PostgresSQL 将插件启动。

select * from pg_available_extensions;
CREATE EXTENSION vector;

这里完犊子了,可以看到插件列表中有 vector 插件,但是启用的时候整个服务都会停掉,提示:server process (PID xxxxx) was terminated by signal 4: Illegal instruction,这里又在 issues #130 中找到了,似乎是编译优化的问题,直接找到 pgvector 的 Makefile 进行修改,把多余的大概都删了一下,最后如下:

EXTENSION = vector
EXTVERSION = 0.8.0

MODULE_big = vector
DATA = $(wildcard sql/*--*--*.sql)
DATA_built = sql/$(EXTENSION)--$(EXTVERSION).sql
OBJS = src/bitutils.o src/bitvec.o src/halfutils.o src/halfvec.o src/hnsw.o src/hnswbuild.o src/hnswinsert.o src/hnswscan.o src/hnswutils.o src/hnswvacuum.o src/ivfbuild.o src/ivfflat.o src/ivfinsert.o src/ivfkmeans.o src/ivfscan.o src/ivfutils.o src/ivfvacuum.o src/sparsevec.o src/vector.o
HEADERS = src/halfvec.h src/sparsevec.h src/vector.h

TESTS = $(wildcard test/sql/*.sql)
REGRESS = $(patsubst test/sql/%.sql,%,$(TESTS))
REGRESS_OPTS = --inputdir=test --load-extension=$(EXTENSION)

all: sql/$(EXTENSION)--$(EXTVERSION).sql

sql/$(EXTENSION)--$(EXTVERSION).sql: sql/$(EXTENSION).sql
	cp $< $@

ifdef USE_PGXS
    PG_CONFIG ?= pg_config
    PGXS := $(shell $(PG_CONFIG) --pgxs)
    include $(PGXS)
else
    subdir = contrib/pgvector
    top_builddir = ../..
    include $(top_builddir)/src/Makefile.global
    include $(top_srcdir)/contrib/contrib-global.mk
endif

重新编译...

重新安装...

启动插件...

1733071281524.png

打工告成!其实还是有点麻烦的!!!

最后 PostgresSQL 的 Makefile 总计就添加了一步,对于插件包中修改都是本地修改好传送到服务器上的,这里也备份一个完成的文件。

# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#

include $(TOPDIR)/rules.mk

PKG_NAME:=postgresql
PKG_VERSION:=14.9
PKG_RELEASE:=$(AUTORELEASE)
PKG_MAINTAINER:=Daniel Golle <[email protected]>
PKG_LICENSE:=PostgreSQL
PKG_CPE_ID:=cpe:/a:postgresql:postgresql

PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.bz2
PKG_SOURCE_URL:=\
	https://ftp.postgresql.org/pub/source/v$(PKG_VERSION) \
	http://ftp.postgresql.org/pub/source/v$(PKG_VERSION) \
	ftp://ftp.postgresql.org/pub/source/v$(PKG_VERSION)

PKG_HASH:=b1fe3ba9b1a7f3a9637dd1656dfdad2889016073fd4d35f13b50143cbbb6a8ef

PKG_USE_MIPS16:=0
PKG_FIXUP:=autoreconf
PKG_MACRO_PATHS:=config
PKG_BUILD_DEPENDS:=postgresql/host
PKG_INSTALL:=1

include $(INCLUDE_DIR)/host-build.mk
include $(INCLUDE_DIR)/package.mk

define Package/libpq
  SECTION:=libs
  CATEGORY:=Libraries
  DEPENDS:=+libpthread
  TITLE:=PostgreSQL client library
  URL:=http://www.postgresql.org/
  SUBMENU:=Database
endef

define Package/libpq/description
PostgreSQL client library.
endef

define Package/pgsql-cli
  SECTION:=utils
  CATEGORY:=Utilities
  DEPENDS:=+libncursesw +libpq +libreadline +librt +zlib
  TITLE:=Command Line Interface (CLI) to PostgreSQL databases
  URL:=http://www.postgresql.org/
  SUBMENU:=Database
endef

define Package/pgsql-cli/description
Command Line Interface (CLI) to PostgreSQL databases.
endef

define Package/pgsql-cli-extra
  SECTION:=utils
  CATEGORY:=Utilities
  DEPENDS:=+libncursesw +libpq +libreadline +librt +zlib
  TITLE:=Command Line extras for PostgreSQL databases
  URL:=http://www.postgresql.org/
  SUBMENU:=Database
endef

define Package/pgsql-cli-extra/description
Command Line extras for PostgreSQL databases.
endef

define Package/pgsql-server
  SECTION:=utils
  CATEGORY:=Utilities
  DEPENDS:=+pgsql-cli +blockd
  TITLE:=PostgreSQL databases Server
  URL:=http://www.postgresql.org/
  SUBMENU:=Database
  USERID:=postgres=5432:postgres=5432
endef

define Package/pgsql-server/description
PostgreSQL databases Server.
endef

PGSQL_SERVER_BIN := \
	pg_amcheck \
	pg_archivecleanup \
	pg_basebackup \
	pg_checksums \
	pg_controldata \
	pg_ctl \
	pg_dump \
	pg_dumpall \
	pg_isready \
	pg_recvlogical \
	pg_receivewal \
	pg_resetwal \
	pg_restore \
	pg_rewind \
	pg_upgrade \
	pg_verifybackup \
	pg_waldump \
	postgres \
	initdb

PGSQL_CLI_EXTRA_BIN := \
	clusterdb	\
	createdb	\
	createuser	\
	dropdb		\
	dropuser	\
	oid2name	\
	pgbench		\
	reindexdb	\
	vacuumdb	\
	vacuumlo

PGSQL_CONFIG_VARS:= \
	pgac_cv_snprintf_long_long_int_format="%lld" \
	pgac_cv_snprintf_size_t_support=yes \
	USE_DEV_URANDOM=1 \
	ac_cv_file__dev_urandom="/dev/urandom" \
	ZIC=zic

ifeq ($(CONFIG_USE_UCLIBC),y)
# PostgreSQL does not build against uClibc with locales
# enabled, due to an uClibc bug, see
# http://lists.uclibc.org/pipermail/uclibc/2014-April/048326.html
# so overwrite automatic detection and disable locale support
PGSQL_CONFIG_VARS+= \
		pgac_cv_type_locale_t=no
endif

TARGET_CONFIGURE_OPTS+=$(PGSQL_CONFIG_VARS)

HOST_CONFIGURE_ARGS += \
			--disable-nls \
			--disable-rpath \
			--without-bonjour \
			--without-gssapi \
			--without-ldap \
			--without-openssl \
			--without-pam \
			--without-perl \
			--without-python \
			--without-readline \
			--without-tcl \
			--without-systemd \
			--with-zlib="yes" \
			--enable-depend

CONFIGURE_ARGS += \
			$(DISABLE_NLS) \
			--disable-rpath \
			--without-bonjour \
			--without-gssapi \
			--without-ldap \
			--without-openssl \
			--without-pam \
			--without-perl \
			--without-python \
			--without-tcl \
			--without-systemd \
			--with-zlib="yes" \
			--enable-depend \
			$(if $(CONFIG_arc),--disable-spinlocks)

HOST_CFLAGS += -std=gnu99

# Need a native zic and pg_config for build
define Host/Compile
	+$(HOST_MAKE_VARS) MAKELEVEL=0 $(MAKE) -C $(HOST_BUILD_DIR)/src/bin/pg_config CC="$(HOSTCC)"
	+$(HOST_MAKE_VARS) MAKELEVEL=0 $(MAKE) -C $(HOST_BUILD_DIR)/src/timezone CC="$(HOSTCC)"
endef

define Host/Install
	$(INSTALL_DIR) $(STAGING_DIR_HOSTPKG)/lib/
	$(INSTALL_BIN) $(HOST_BUILD_DIR)/src/common/libpgcommon.a $(STAGING_DIR_HOSTPKG)/lib/
	$(INSTALL_BIN) $(HOST_BUILD_DIR)/src/port/libpgport.a $(STAGING_DIR_HOSTPKG)/lib/
	$(INSTALL_BIN) $(HOST_BUILD_DIR)/src/bin/pg_config/pg_config $(STAGING_DIR_HOSTPKG)/lib/
	$(INSTALL_DIR) $(STAGING_DIR_HOSTPKG)/bin/
	$(INSTALL_BIN) $(HOST_BUILD_DIR)/src/timezone/zic $(STAGING_DIR_HOSTPKG)/bin/
endef

# 主要是修改了这里,从dl文件夹里面把pgvector插件解压到contrib,一起编译
define Build/Prepare
	$(call Build/Prepare/Default)
	rm -rf $(PKG_BUILD_DIR)/contrib/pgvector && \
	    mkdir $(PKG_BUILD_DIR)/contrib/pgvector && \
        $(TAR) -xzf $(DL_DIR)/pgvector-0.8.0.tar.gz --strip-components=1 -C $(PKG_BUILD_DIR)/contrib/pgvector && \
        sed -i 's/vacuumlo/vacuumlo pgvector/' $(PKG_BUILD_DIR)/contrib/Makefile
endef

define Build/Compile
	$(MAKE) -C $(PKG_BUILD_DIR) MAKELEVEL=0 all contrib
endef

# because PROFILE means something else in the project Makefile
unexport PROFILE

define Package/libpq/install
	$(INSTALL_DIR) $(1)/usr/lib
	$(CP) $(PKG_INSTALL_DIR)/usr/lib/libpq.so.* $(1)/usr/lib/
endef

define Package/pgsql-cli/install
	$(INSTALL_DIR) $(1)/usr/bin
	$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/bin/psql $(1)/usr/bin/
endef

define Package/pgsql-cli-extra/install
	$(INSTALL_DIR) $(1)/usr/bin
	$(INSTALL_BIN) $(foreach bin,$(PGSQL_CLI_EXTRA_BIN),$(PKG_INSTALL_DIR)/usr/bin/$(bin)) $(1)/usr/bin/
endef

define Package/pgsql-server/conffiles
/etc/config/postgresql
endef

define Package/pgsql-server/install
	$(INSTALL_DIR) $(1)/usr/bin
	$(INSTALL_BIN) $(foreach bin,$(PGSQL_SERVER_BIN),$(PKG_INSTALL_DIR)/usr/bin/$(bin)) $(1)/usr/bin/

	ln -sf postgres $(1)/usr/bin/postmaster

	$(INSTALL_DIR) $(1)/usr/share/postgresql
	$(CP) $(PKG_INSTALL_DIR)/usr/share/postgresql/* \
		$(1)/usr/share/postgresql

	$(INSTALL_DIR) $(1)/usr/lib
	$(CP) $(PKG_INSTALL_DIR)/usr/lib/postgresql \
		$(1)/usr/lib/

	$(INSTALL_DIR) $(1)/lib/functions
	$(INSTALL_BIN) ./files/postgresql.sh $(1)/lib/functions/

	$(INSTALL_DIR) $(1)/etc/config
	$(INSTALL_DATA) ./files/postgresql.config $(1)/etc/config/postgresql

	$(INSTALL_DIR) $(1)/etc/init.d
	$(INSTALL_BIN) ./files/postgresql.init $(1)/etc/init.d/postgresql
endef

define Build/InstallDev
	$(INSTALL_DIR) $(1)/usr/bin
	$(CP) $(STAGING_DIR_HOSTPKG)/lib/pg_config $(1)/usr/bin
	$(INSTALL_DIR) $(1)/host/bin/
	$(LN) $(STAGING_DIR)/usr/bin/pg_config $(1)/host/bin
	$(INSTALL_DIR) $(1)/usr/include
	$(CP) $(PKG_INSTALL_DIR)/usr/include/libpq $(1)/usr/include/
	$(CP) $(PKG_INSTALL_DIR)/usr/include/libpq-fe.h $(1)/usr/include/
	$(CP) $(PKG_INSTALL_DIR)/usr/include/pg_config.h $(1)/usr/include/
	$(CP) $(PKG_INSTALL_DIR)/usr/include/pg_config_manual.h $(1)/usr/include/
	$(CP) $(PKG_INSTALL_DIR)/usr/include/postgres_ext.h $(1)/usr/include/
	$(CP) $(PKG_INSTALL_DIR)/usr/include/pg_config_ext.h $(1)/usr/include/
	$(CP) $(PKG_INSTALL_DIR)/usr/include/postgresql $(1)/usr/include/
	$(INSTALL_DIR) $(1)/usr/lib
	$(CP) $(PKG_INSTALL_DIR)/usr/lib/libpq.{a,so*} $(1)/usr/lib/
	$(INSTALL_DIR) $(1)/usr/lib/pkgconfig
	$(CP) $(PKG_INSTALL_DIR)/usr/lib/pkgconfig/libpq.pc $(1)/usr/lib/pkgconfig/
endef

$(eval $(call HostBuild))
$(eval $(call BuildPackage,libpq))
$(eval $(call BuildPackage,pgsql-cli))
$(eval $(call BuildPackage,pgsql-cli-extra))
$(eval $(call BuildPackage,pgsql-server))