Skip to content

Commit 070a5db

Browse files
committed
feat: add pg_documentdb and pg_cron extensions to sql image
Multi-stage Debian build with pgvector, pg_cron, and pg_documentdb (core + api). Enables FerretDB/docdb MongoDB compatibility and scheduled job support. PostGIS included as documentdb runtime dep.
1 parent 4fb03ac commit 070a5db

File tree

3 files changed

+120
-22
lines changed

3 files changed

+120
-22
lines changed

Dockerfile

Lines changed: 110 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,128 @@
11
ARG PG_MAJOR=18
22

3-
# Stage 1: Build pgvector extension from Hanzo fork
4-
FROM postgres:${PG_MAJOR}-alpine AS builder
3+
# Stage 1: Build all extensions on Debian (documentdb requires glibc toolchain)
4+
FROM postgres:${PG_MAJOR}-bookworm AS builder
55
ARG PG_MAJOR
66

7-
RUN apk add --no-cache --virtual .build-deps \
8-
build-base \
9-
clang19 \
10-
llvm19 \
11-
postgresql${PG_MAJOR}-dev && \
12-
cd /tmp && \
13-
wget -qO- https://github.com/hanzoai/sql-vector/archive/refs/heads/master.tar.gz | tar xz && \
7+
ENV DEBIAN_FRONTEND=noninteractive
8+
9+
# Install build dependencies for pgvector, pg_cron, and pg_documentdb
10+
RUN apt-get update && apt-get install -y --no-install-recommends \
11+
build-essential \
12+
cmake \
13+
git \
14+
curl \
15+
ca-certificates \
16+
pkg-config \
17+
postgresql-server-dev-${PG_MAJOR} \
18+
libpq-dev \
19+
libicu-dev \
20+
libkrb5-dev \
21+
# PostGIS (runtime dep for documentdb CREATE EXTENSION)
22+
postgresql-${PG_MAJOR}-postgis-3 \
23+
&& rm -rf /var/lib/apt/lists/*
24+
25+
# --- pgvector ---
26+
RUN cd /tmp && \
27+
curl -sL https://github.com/hanzoai/sql-vector/archive/refs/heads/master.tar.gz | tar xz && \
1428
cd sql-vector-master && \
1529
make clean && \
16-
make OPTFLAGS="" PG_CONFIG=/usr/local/bin/pg_config && \
17-
make install PG_CONFIG=/usr/local/bin/pg_config && \
18-
cd / && rm -rf /tmp/sql-vector-master && \
19-
apk del .build-deps
30+
make OPTFLAGS="" PG_CONFIG=/usr/bin/pg_config && \
31+
make install PG_CONFIG=/usr/bin/pg_config && \
32+
cd / && rm -rf /tmp/sql-vector-master
33+
34+
# --- pg_cron ---
35+
ARG PG_CRON_VERSION=v1.6.7
36+
RUN cd /tmp && \
37+
curl -sL https://github.com/citusdata/pg_cron/archive/refs/tags/${PG_CRON_VERSION}.tar.gz | tar xz && \
38+
cd pg_cron-* && \
39+
make PG_CONFIG=/usr/bin/pg_config && \
40+
make install PG_CONFIG=/usr/bin/pg_config && \
41+
cd / && rm -rf /tmp/pg_cron-*
42+
43+
# --- pg_documentdb dependencies: libbson, Intel Decimal Math Lib, pcre2 ---
44+
45+
ARG LIBBSON_VERSION=1.28.0
46+
RUN cd /tmp && \
47+
curl -sL https://github.com/mongodb/mongo-c-driver/releases/download/${LIBBSON_VERSION}/mongo-c-driver-${LIBBSON_VERSION}.tar.gz | tar xz && \
48+
cd mongo-c-driver-${LIBBSON_VERSION}/build && \
49+
cmake -DENABLE_MONGOC=ON -DMONGOC_ENABLE_ICU=OFF -DENABLE_ICU=OFF \
50+
-DCMAKE_C_FLAGS="-fPIC -g" -DCMAKE_BUILD_TYPE=Release \
51+
-DCMAKE_INSTALL_PREFIX=/usr .. && \
52+
make -j$(nproc) install && \
53+
cd / && rm -rf /tmp/mongo-c-driver-*
54+
55+
ARG INTEL_MATH_LIB_VERSION=applied/2.0u3-1
56+
RUN cd /tmp && \
57+
mkdir -p intelmathlib/lib/intelmathlib && \
58+
cd intelmathlib/lib/intelmathlib && \
59+
git init && \
60+
git remote add origin https://git.launchpad.net/ubuntu/+source/intelrdfpmath && \
61+
git fetch --depth 1 origin "${INTEL_MATH_LIB_VERSION}" && \
62+
git checkout FETCH_HEAD && \
63+
cd LIBRARY && \
64+
make -j$(nproc) _CFLAGS_OPT=-fPIC CC=gcc CALL_BY_REF=0 GLOBAL_RND=0 GLOBAL_FLAGS=0 UNCHANGED_BINARY_FLAGS=0 && \
65+
cd /tmp/intelmathlib && \
66+
# Create pkg-config file
67+
printf 'prefix=/usr/lib/intelmathlib\nlibdir=${prefix}/LIBRARY\nincludedir=${prefix}/LIBRARY/src\n\nName: intelmathlib\nDescription: Intel Decimal Floating point math library\nVersion: 2.0 Update 2\nCflags: -I${includedir}\nLibs: -L${libdir} -lbid\n' > intelmathlib.pc && \
68+
cp -R lib/intelmathlib /usr/lib/ && \
69+
cp intelmathlib.pc /usr/lib/pkgconfig/ && \
70+
cd / && rm -rf /tmp/intelmathlib
71+
72+
ARG PCRE2_VERSION=10.40
73+
RUN cd /tmp && \
74+
curl -sL https://github.com/PCRE2Project/pcre2/releases/download/pcre2-${PCRE2_VERSION}/pcre2-${PCRE2_VERSION}.tar.gz | tar xz && \
75+
cd pcre2-${PCRE2_VERSION} && \
76+
./configure --prefix=/usr --disable-shared --enable-static --enable-jit && \
77+
make -j$(nproc) AM_CFLAGS=-fPIC install && \
78+
cd / && rm -rf /tmp/pcre2-*
2079

21-
# Stage 2: Runtime image
22-
FROM postgres:${PG_MAJOR}-alpine
80+
# --- pg_documentdb (core + api) ---
81+
ARG DOCUMENTDB_VERSION=main
82+
RUN cd /tmp && \
83+
curl -sL https://github.com/microsoft/documentdb/archive/refs/heads/${DOCUMENTDB_VERSION}.tar.gz | tar xz && \
84+
cd documentdb-* && \
85+
make -C pg_documentdb_core PG_CONFIG=/usr/bin/pg_config install && \
86+
make -C pg_documentdb PG_CONFIG=/usr/bin/pg_config install && \
87+
cd / && rm -rf /tmp/documentdb-*
88+
89+
# Stage 2: Runtime image (Debian for glibc compat with documentdb)
90+
FROM postgres:${PG_MAJOR}-bookworm
2391

2492
LABEL maintainer="dev@hanzo.ai"
2593
LABEL org.opencontainers.image.source="https://github.com/hanzoai/sql"
26-
LABEL org.opencontainers.image.description="Hanzo SQL - PostgreSQL with pgvector for AI workloads"
94+
LABEL org.opencontainers.image.description="Hanzo SQL - PostgreSQL with pgvector, pg_cron, and pg_documentdb"
95+
96+
# Runtime deps: libpq (pg_cron), PostGIS (documentdb geospatial), ICU
97+
ARG PG_MAJOR=18
98+
RUN apt-get update && apt-get install -y --no-install-recommends \
99+
libpq5 \
100+
postgresql-${PG_MAJOR}-postgis-3 \
101+
&& rm -rf /var/lib/apt/lists/*
102+
103+
# Copy Intel Decimal Math Library (runtime dep for documentdb)
104+
COPY --from=builder /usr/lib/intelmathlib /usr/lib/intelmathlib
105+
106+
# Copy pgvector extension
107+
COPY --from=builder /usr/lib/postgresql/${PG_MAJOR}/lib/vector.so /usr/lib/postgresql/${PG_MAJOR}/lib/
108+
COPY --from=builder /usr/share/postgresql/${PG_MAJOR}/extension/vector* /usr/share/postgresql/${PG_MAJOR}/extension/
109+
110+
# Copy pg_cron extension
111+
COPY --from=builder /usr/lib/postgresql/${PG_MAJOR}/lib/pg_cron.so /usr/lib/postgresql/${PG_MAJOR}/lib/
112+
COPY --from=builder /usr/share/postgresql/${PG_MAJOR}/extension/pg_cron* /usr/share/postgresql/${PG_MAJOR}/extension/
113+
114+
# Copy pg_documentdb_core extension
115+
COPY --from=builder /usr/lib/postgresql/${PG_MAJOR}/lib/pg_documentdb_core.so /usr/lib/postgresql/${PG_MAJOR}/lib/
116+
COPY --from=builder /usr/share/postgresql/${PG_MAJOR}/extension/documentdb_core* /usr/share/postgresql/${PG_MAJOR}/extension/
27117

28-
# Copy pgvector extension from builder (shared object + SQL files)
29-
COPY --from=builder /usr/local/lib/postgresql/vector.so /usr/local/lib/postgresql/
30-
COPY --from=builder /usr/local/share/postgresql/extension/vector* /usr/local/share/postgresql/extension/
118+
# Copy pg_documentdb extension
119+
COPY --from=builder /usr/lib/postgresql/${PG_MAJOR}/lib/pg_documentdb.so /usr/lib/postgresql/${PG_MAJOR}/lib/
120+
COPY --from=builder /usr/share/postgresql/${PG_MAJOR}/extension/documentdb* /usr/share/postgresql/${PG_MAJOR}/extension/
31121

32122
# Custom postgresql.conf tuned for AI workloads
33123
COPY conf/postgresql.conf /etc/postgresql/postgresql.conf
34124

35-
# Init scripts: enable pgvector on startup
125+
# Init scripts: enable extensions on startup
36126
COPY docker-entrypoint-initdb.d/ /docker-entrypoint-initdb.d/
37127

38128
# Health check

conf/postgresql.conf

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ lc_numeric = 'en_US.utf8'
4545
lc_time = 'en_US.utf8'
4646

4747
# Extensions
48-
shared_preload_libraries = 'vector'
48+
shared_preload_libraries = 'vector,pg_cron,pg_documentdb_core,pg_documentdb'
49+
50+
# pg_cron - run jobs in any database (not just postgres)
51+
cron.database_name = 'postgres'
4952

5053
# pgvector specific - HNSW index build parallelism
5154
max_parallel_maintenance_workers = 4
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1-
-- Hanzo SQL: Enable pgvector extension
1+
-- Hanzo SQL: Enable extensions
22
CREATE EXTENSION IF NOT EXISTS vector;
3+
CREATE EXTENSION IF NOT EXISTS pg_cron;
4+
CREATE EXTENSION IF NOT EXISTS postgis;
5+
CREATE EXTENSION IF NOT EXISTS tsm_system_rows;
6+
CREATE EXTENSION IF NOT EXISTS documentdb_core;
7+
CREATE EXTENSION IF NOT EXISTS documentdb;

0 commit comments

Comments
 (0)