Skip to content

Commit cdec1a7

Browse files
committed
Pure C. Replace clickhouse-cpp with clickhouse-c
clickhouse-c is an experimental clickhouse library focused on minimalism, allowing for proper compatibility reusing postgres allocator
1 parent 664a327 commit cdec1a7

48 files changed

Lines changed: 3385 additions & 2507 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/bake.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ jobs:
2929
registry: ghcr.io
3030
username: ${{ github.actor }}
3131
password: ${{ secrets.GITHUB_TOKEN }}
32-
- name: Cache Vendor Build
33-
uses: actions/cache@v4
34-
with:
35-
path: vendor/_build
36-
key: ${{ runner.os }}-vendor-build
3732
- name: Set Bake Variables
3833
run: make bake-vars REGISTRY=ghcr.io/clickhouse PG_VERSIONS=${{ matrix.pgv }} >> $GITHUB_ENV
3934
- name: Build${{ startsWith(github.ref, 'refs/tags') && ' and Push' || '' }}

.github/workflows/clickhouse.yml

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
container: pgxn/pgxn-tools
2828
steps:
2929
- name: Start Postgres
30-
run: pg-start ${{ env.pg }} libcurl4-openssl-dev uuid-dev libre2-dev
30+
run: pg-start ${{ env.pg }} libcurl4-openssl-dev uuid-dev liblz4-dev libzstd-dev libre2-dev
3131
- name: Install Extensions
3232
run: pgxn install re2
3333
- name: Checkout the Repository
@@ -36,19 +36,5 @@ jobs:
3636
- name: Start ClickHouse
3737
env: { CH_RELEASE: "${{ matrix.ch }}" }
3838
run: .github/ubuntu/clickhouse.sh
39-
- name: Cache Dependencies
40-
uses: actions/cache@v5
41-
with:
42-
path: vendor/_build/*
43-
key: vendor-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('.git/modules/clickhouse-cpp/refs/heads/master') }}
44-
# Required to prevent clickhouse-cpp from rebuilding because it depends
45-
# on this file in the Makefile. https://github.com/actions/checkout/issues/968
46-
- name: Reset Vendor Timestamp
47-
run: cd vendor/clickhouse-cpp && touch -d $(git log -1 --format="@%ct" CMakeLists.txt) CMakeLists.txt
48-
- name: Test DSO
39+
- name: Test
4940
run: pg-build-test
50-
- name: Clean
51-
run: make clean NO_VENDOR_CLEAN=1
52-
- name: Test Static
53-
run: pg-build-test
54-
env: { CH_BUILD: static }

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
env: { pg: 18 }
1515
steps:
1616
- name: Install Postgres
17-
run: NO_CLUSTER=1 pg-start ${{ env.pg }} libcurl4-openssl-dev uuid-dev clang-tidy
17+
run: NO_CLUSTER=1 pg-start ${{ env.pg }} libcurl4-openssl-dev uuid-dev liblz4-dev libzstd-dev clang-tidy
1818
- name: Install clang-format # version in Ubuntu is too old
1919
run: curl -sLo /usr/local/bin/clang-format https://github.com/cpp-linter/clang-tools-static-binaries/releases/latest/download/clang-format-22_linux-amd64 && chmod +x /usr/local/bin/clang-format
2020
- name: Checkout the Repository

.github/workflows/postgres.yml

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,13 @@ jobs:
1919
container: pgxn/pgxn-tools
2020
steps:
2121
- name: Start Postgres ${{ matrix.pg }}
22-
run: pg-start ${{ matrix.pg }} libcurl4-openssl-dev uuid-dev libre2-dev
22+
run: pg-start ${{ matrix.pg }} libcurl4-openssl-dev uuid-dev liblz4-dev libzstd-dev libre2-dev
2323
- name: Install Extensions
2424
run: pgxn install re2
2525
- name: Checkout the Repository
2626
uses: actions/checkout@v6
2727
with: { submodules: true }
2828
- name: Start ClickHouse
2929
run: .github/ubuntu/clickhouse.sh
30-
- name: Cache Dependencies
31-
uses: actions/cache@v5
32-
with:
33-
path: vendor/_build/*
34-
key: vendor-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('.git/modules/clickhouse-cpp/refs/heads/master') }}
35-
# Required to prevent clickhouse-cpp from rebuilding because it depends
36-
# on this file in the Makefile. https://github.com/actions/checkout/issues/968
37-
- name: Reset Vendor Timestamp
38-
run: cd vendor/clickhouse-cpp && touch -d $(git log -1 --format="@%ct" CMakeLists.txt) CMakeLists.txt
39-
- name: Test DSO
30+
- name: Test
4031
run: pg-build-test
41-
- name: Clean
42-
run: make clean NO_VENDOR_CLEAN=1
43-
- name: Test Static
44-
run: pg-build-test
45-
env: { CH_BUILD: static }

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
- name: Check out the repo
1616
uses: actions/checkout@v6
1717
- name: Start Postgres
18-
run: pg-start 18 libcurl4-openssl-dev uuid-dev
18+
run: pg-start 18 libcurl4-openssl-dev uuid-dev liblz4-dev libzstd-dev
1919
- name: Start ClickHouse
2020
env: { CH_RELEASE: "${{ matrix.ch }}" }
2121
run: .github/ubuntu/clickhouse.sh

.gitmodules

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
[submodule "clickhouse-cpp"]
2-
path = vendor/clickhouse-cpp
3-
url = https://github.com/ClickHouse/clickhouse-cpp.git
1+
[submodule "vendor/clickhouse-c"]
2+
path = vendor/clickhouse-c
3+
url = https://github.com/ClickHouse/clickhouse-c

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ All notable changes to this project will be documented in this file. It uses the
1111

1212
### ⚡ Improvements
1313

14+
* Replaced the `clickhouse-cpp` driver with [ClickHouse/clickhouse-c],
15+
pulled in as a git submodule under `vendor/clickhouse-c`.
16+
1417
* Added multidimensional array support across SELECT and INSERT to both the
1518
binary and http drivers. Rectangular ClickHouse `Array(Array(...))` values
1619
now map to PostgreSQL multidimensional arrays, jagged arrays not supported,
@@ -21,6 +24,7 @@ All notable changes to this project will be documented in this file. It uses the
2124
[v0.3.1]: https://github.com/ClickHouse/pg_clickhouse/compare/v0.3.0...v0.3.1
2225
[#233]: https://github.com/ClickHouse/pg_clickhouse/pull/233
2326
"ClickHouse/pg_clickhouse#233 Support multidimensional arrays"
27+
[ClickHouse/clickhouse-c]: https://github.com/ClickHouse/clickhouse-c
2428

2529
## [v0.3.0] — 2026-05-11
2630

Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
1111
make \
1212
cmake \
1313
libssl-dev \
14+
liblz4-dev \
15+
libzstd-dev \
1416
libre2-dev \
1517
pgxnclient \
1618
unzip \
@@ -24,7 +26,7 @@ RUN cd /tmp && pgxn download re2 && unzip re2-*.zip && rm re2-*.zip && cd re2-*
2426
FROM postgres:$PG_MAJOR-trixie
2527

2628
# Install dependencies
27-
RUN apt-get update && apt-get install -y --no-install-recommends libcurl4t64 uuid libre2-11 ca-certificates \
29+
RUN apt-get update && apt-get install -y --no-install-recommends libcurl4t64 uuid libre2-11 liblz4-1 libzstd1 ca-certificates \
2830
&& apt-get clean \
2931
&& rm -rf /var/cache/apt/* /var/lib/apt/lists/*
3032

Makefile

Lines changed: 21 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -16,97 +16,38 @@ CURL_CONFIG ?= curl-config
1616
OS ?= $(shell uname -s | tr A-Z a-z)
1717
ARCH = $(shell uname -m)
1818

19-
# Collect all the C++ and C files to compile into MODULE_big.
20-
OBJS = $(subst .cpp,.o, $(wildcard src/*.cpp src/*/*.cpp)) \
21-
$(subst .c,.o, $(wildcard src/*.c src/*/*.c))
22-
23-
# Build static on Darwin by default.
24-
ifndef CH_BUILD
25-
# ifeq ($(OS),darwin)
26-
CH_BUILD = static
27-
# else
28-
# CH_BUILD = dynamic
29-
# endif
30-
endif
19+
# Collect all the C files to compile into MODULE_big.
20+
OBJS = $(subst .c,.o, $(wildcard src/*.c src/*/*.c))
3121

32-
# clickhouse-cpp source and build directories.
33-
CH_CPP_DIR = vendor/clickhouse-cpp
34-
CH_CPP_BUILD_DIR = vendor/_build/$(OS)-$(ARCH)-$(CH_BUILD)-$(shell git submodule status $(CH_CPP_DIR) | awk '{print substr($$1, 0, 7)}')
35-
36-
# List the clickhouse-cpp libraries we require.
37-
CH_CPP_LIB = $(CH_CPP_BUILD_DIR)/clickhouse/libclickhouse-cpp-lib$(DLSUFFIX)
38-
CH_CPP_FLAGS = -D CMAKE_BUILD_TYPE=Release -D WITH_OPENSSL=ON
39-
40-
# Are we statically compiling clickhouse-cpp into the extension or no?
41-
ifeq ($(CH_BUILD), static)
42-
# We'll need all the clickhouse-cpp static libraries.
43-
CH_CPP_LIB = $(CH_CPP_BUILD_DIR)/clickhouse/libclickhouse-cpp-lib.a
44-
SHLIB_LINK = $(CH_CPP_LIB) \
45-
$(CH_CPP_BUILD_DIR)/contrib/cityhash/cityhash/libcityhash.a \
46-
$(CH_CPP_BUILD_DIR)/contrib/absl/absl/libabsl_int128.a \
47-
$(CH_CPP_BUILD_DIR)/contrib/lz4/lz4/liblz4.a \
48-
$(CH_CPP_BUILD_DIR)/contrib/zstd/zstd/libzstdstatic.a
49-
else
50-
# Build and install the shared library.
51-
SHLIB_LINK = -L$(CH_CPP_BUILD_DIR)/clickhouse -lclickhouse-cpp-lib
52-
CH_CPP_FLAGS += -D BUILD_SHARED_LIBS=ON
53-
endif
22+
# clickhouse-c is a header-only single-header library. Override
23+
# CH_C_DIR to point elsewhere when developing against a local checkout.
24+
CH_C_DIR ?= vendor/clickhouse-c
5425

5526
# Add include directories.
56-
PG_CPPFLAGS = -I./src/include -I$(CH_CPP_DIR) -I$(CH_CPP_DIR)/contrib/absl
57-
58-
# Include other libraries compiled into clickhouse-cpp.
59-
PG_LDFLAGS = -lstdc++ -lssl -lcrypto $(shell $(CURL_CONFIG) --libs)
27+
PG_CPPFLAGS = -I./src/include -I$(CH_C_DIR)
6028

61-
# clickhouse-cpp requires C++ v17.
62-
PG_CXXFLAGS = -std=c++17
63-
64-
# Suppress annoying pre-c99 warning and include curl flags.
65-
PG_CFLAGS = -Wno-declaration-after-statement -Werror=type-limits $(shell $(CURL_CONFIG) --cflags)
29+
# Link OpenSSL (for TLS in the binary driver), curl (for the HTTP driver),
30+
# libuuid (for http_streaming.c's query-id generator), and lz4 / zstd
31+
# (for the binary driver's compressed-frame codecs).
32+
PG_LDFLAGS = -lssl -lcrypto -llz4 -lzstd $(shell $(CURL_CONFIG) --libs)
6633

67-
# We'll need libuuid except on darwin, where it's included in the OS.
34+
# libuuid is provided by the OS on darwin; explicit link elsewhere.
6835
ifneq ($(OS),darwin)
6936
PG_LDFLAGS += -luuid
7037
endif
7138

72-
# Clean up the clickhouse-cpp build directory and generated files.
39+
# Suppress annoying pre-c99 warning and include curl flags.
40+
PG_CFLAGS = -Wno-declaration-after-statement -Werror=type-limits $(shell $(CURL_CONFIG) --cflags)
41+
42+
# Clean up generated files.
7343
EXTRA_CLEAN = sql/$(EXTENSION)--$(EXTVERSION).sql src/include/version.h compile_commands.json test/schedule $(EXTENSION)-$(DISTVERSION).zip
74-
ifndef NO_VENDOR_CLEAN
75-
EXTRA_CLEAN += $(CH_CPP_BUILD_DIR)
76-
endif
7744

7845
# Import PGXS.
7946
PGXS := $(shell $(PG_CONFIG) --pgxs)
8047
include $(PGXS)
8148

82-
# We'll need the clickhouse-cpp library and rpath so it can be found.
83-
SHLIB_LINK += -Wl,-rpath,$(pkglibdir)/
84-
85-
# PostgreSQL 15 and earlier violate a C++ v17 storage specifier error.
86-
ifeq ($(shell test $(MAJORVERSION) -lt 16; echo $$?),0)
87-
PG_CXXFLAGS += -Wno-register
88-
endif
89-
90-
# Add the flags to the bitcode compiler variables.
91-
COMPILE.cc.bc += $(PG_CPPFLAGS)
92-
COMPILE.cxx.bc += $(PG_CXXFLAGS)
93-
94-
# shlib is the final output product: clickhouse-cpp and all .o dependencies.
95-
$(shlib): $(CH_CPP_LIB) $(OBJS)
96-
97-
# Clone clickhouse-cpp submodule.
98-
$(CH_CPP_DIR)/CMakeLists.txt:
99-
git submodule update --init
100-
101-
# Require the vendored clickhouse-cpp and the version header.
102-
$(OBJS): $(CH_CPP_LIB) src/include/version.h
103-
104-
# Build clickhouse-cpp.
105-
$(CH_CPP_LIB): export CXXFLAGS=-fPIC
106-
$(CH_CPP_LIB): export CFLAGS=-fPIC
107-
$(CH_CPP_LIB): $(CH_CPP_DIR)/CMakeLists.txt # Sync with "Reset Vendor Timestamp" steps in workflows.
108-
cmake -B $(CH_CPP_BUILD_DIR) -S $(CH_CPP_DIR) $(CH_CPP_FLAGS) -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
109-
cmake --build $(CH_CPP_BUILD_DIR) --parallel $$(nproc) --target all
49+
# Require the version header.
50+
$(OBJS): src/include/version.h
11051

11152
# Require the versioned C source and SQL script.
11253
all: sql/$(EXTENSION)--$(EXTVERSION).sql
@@ -119,17 +60,6 @@ sql/$(EXTENSION)--$(EXTVERSION).sql: sql/$(EXTENSION).sql
11960
src/include/version.h: src/include/version.h.in
12061
sed -e 's,__VERSION__,$(DISTVERSION),g' $< > $@
12162

122-
# Configure install/uninstall of the clickhouse-cpp library.
123-
ifneq ($(CH_BUILD), static)
124-
# Copy all dynamic files; use -a to preserve symlinks.
125-
install-ch-cpp: $(CH_CPP_LIB) $(shlib)
126-
cp -a $(CH_CPP_BUILD_DIR)/clickhouse/libclickhouse-cpp-lib*$(DLSUFFIX)* $(DESTDIR)$(pkglibdir)/
127-
uninstall-ch-cpp:
128-
rm -f $(DESTDIR)$(pkglibdir)/libclickhouse-cpp-lib*$(DLSUFFIX)*
129-
install: install-ch-cpp
130-
uninstall: uninstall-ch-cpp
131-
endif
132-
13363
# Build a PGXN distribution bundle.
13464
dist: $(EXTENSION)-$(DISTVERSION).zip
13565

@@ -187,7 +117,7 @@ bake-vars:
187117
@echo "revision=$(REVISION)"
188118
@echo "pg_versions=$(PG_VERSIONS)"
189119

190-
# Format the .c, .h, and .hh files according to the PostgreSQL indentation
120+
# Format the .c and .h files according to the PostgreSQL indentation
191121
# standard. Requires `pg_bsd_indent` to be in the path.
192122
indent: dev/indent.sh
193123
@$<
@@ -199,7 +129,7 @@ lint: .pre-commit-config.yaml
199129

200130
.PHONY: clang-tidy # Run clang-tidy static analysis (requires compile_commands.json)
201131
clang-tidy: compile_commands.json
202-
clang-tidy -p . $(wildcard src/*.c src/*.cpp)
132+
clang-tidy -p . $(wildcard src/*.c)
203133

204134
## .git/hooks/pre-commit: Install the pre-commit hook
205135
.git/hooks/pre-commit:
@@ -216,8 +146,8 @@ lsp: compile_commands.json
216146

217147
# Requires https://github.com/rizsotto/Bear.
218148
compile_commands.json:
219-
$(MAKE) clean -j $$(nproc) NO_VENDOR_CLEAN=$(NO_VENDOR_CLEAN)
220-
bear -- $(MAKE) all -j $$(nproc) NO_VENDOR_CLEAN=$(NO_VENDOR_CLEAN)
149+
$(MAKE) clean -j $$(nproc)
150+
bear -- $(MAKE) all -j $$(nproc)
221151

222152
# ClickHouse Docker Containers
223153
start-containers: dev/Makefile dev/docker-compose.yml

README.md

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ sudo apt install \
8282
uuid-dev \
8383
libssl-dev \
8484
make \
85-
cmake \
86-
g++
85+
gcc
8786
```
8887

8988
#### RedHat / CentOS / Yum
@@ -95,7 +94,6 @@ sudo yum install \
9594
libuuid-devel \
9695
openssl-libs \
9796
automake \
98-
cmake \
9997
gcc
10098
```
10199

@@ -121,18 +119,6 @@ make
121119
sudo make install
122120
```
123121

124-
<!-- XXX DSO currently disabled.
125-
By default `make` dynamically links the `clickhouse-cpp` library (except on
126-
macOS, where a dynamic `clickhouse-cpp` library is not yet supported). To
127-
statically compile the ClickHouse library into `pg_clickhouse`, pass
128-
`CH_BUILD=static`:
129-
130-
```sh
131-
make CH_BUILD=static
132-
sudo make install CH_BUILD=static
133-
```
134-
-->
135-
136122
If your host has several PostgreSQL installations, you might need to specify
137123
the appropriate version of `pg_config`:
138124

@@ -239,8 +225,8 @@ CREATE EXTENSION pg_clickhouse SCHEMA env;
239225
## Dependencies
240226

241227
The `pg_clickhouse` extension requires [PostgreSQL] 13 or higher, [libcurl],
242-
[libuuid]. Building the extension requires a C and C++ compiler, [libSSL], [GNU
243-
make], and [CMake].
228+
[libuuid]. Building the extension requires a C compiler, [libSSL], and [GNU
229+
make].
244230

245231
## Road Map
246232

@@ -295,7 +281,6 @@ adding DML features. Our road map:
295281
[libcurl]: https://curl.se/libcurl/ "libcurl — your network transfer library"
296282
[libuuid]: https://linux.die.net/man/3/libuuid "libuuid - DCE compatible Universally Unique Identifier library"
297283
[GNU make]: https://www.gnu.org/software/make "GNU Make"
298-
[CMake]: https://cmake.org/ "CMake: A Powerful Software Build System"
299284
[LibSSL]: https://openssl-library.org "OpenSSL Library"
300285
[TPC-H]: https://www.tpc.org/tpch/
301286
[re2]: https://github.com/ClickHouse/pg_re2 "pg_re2: ClickHouse-compatible regex functions using RE2"

0 commit comments

Comments
 (0)