Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/bake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
steps:
- name: Checkout the Repository
uses: actions/checkout@v6
with: { submodules: true }
with: { submodules: recursive }
- name: Set up Depot CLI
uses: depot/setup-action@v1
- name: Log in to GHCR
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/clickhouse.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
run: pgxn install re2
- name: Checkout the Repository
uses: actions/checkout@v6
with: { submodules: true }
with: { submodules: recursive }
- name: Start ClickHouse
env: { CH_RELEASE: "${{ matrix.ch }}" }
run: .github/ubuntu/clickhouse.sh
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
run: NO_CLUSTER=1 pg-start ${{ env.pg }} libcurl4-openssl-dev uuid-dev liblz4-dev libzstd-dev clang-tidy bear shellcheck
- name: Checkout the Repository
uses: actions/checkout@v6
with: { submodules: true }
with: { submodules: recursive }
- name: Install pre-commit
run: make debian-install-lint
- name: Add Postgres to Path
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/postgres.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
run: pgxn install re2
- name: Checkout the Repository
uses: actions/checkout@v6
with: { submodules: true }
with: { submodules: recursive }
- name: Start ClickHouse
run: .github/ubuntu/clickhouse.sh
- name: Test
Expand Down
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "vendor/clickhouse-c"]
path = vendor/clickhouse-c
url = https://github.com/ClickHouse/clickhouse-c
[submodule "vendor/pg-clickhouse-c"]
path = vendor/pg-clickhouse-c
url = https://github.com/ClickHouse/pg-clickhouse-c
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ All notable changes to this project will be documented in this file. It uses the
rather than understating foreign scan cost ([#310]).
* Added mapping to push down the re2 v0.4 `@~` operator to ClickHouse as
the `match()` function ([#318]).
* Binary driver errors now name the column and type involved, for example
`cannot encode integer into ClickHouse Array(Array(Int32)) (column "c2")`
in place of `unexpected PG/CH type pair for column 0` ([#326]).
* Coerce array elements in binary driver. `Array(Int32)` to `bigint[]`, or
`quantilesExactLow()` results into `double precision[]`, no longer fails
with `could not cast value from integer[] to bigint[]` ([#326]).

### 🐞 Bug Fixes

Expand Down Expand Up @@ -160,6 +166,8 @@ All notable changes to this project will be documented in this file. It uses the
"ClickHouse/pg_clickhouse#317 Push down the array IN family unconditionally"
[#319]: https://github.com/ClickHouse/pg_clickhouse/pull/319
"ClickHouse/pg_clickhouse#319 Fix foreign scan RTE selection"
[#326]: https://github.com/ClickHouse/pg_clickhouse/pull/326
"ClickHouse/pg_clickhouse#326 pg-clickhouse-c"

## [v0.3.2] — 2026-06-16

Expand Down
27 changes: 17 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@ ARCH = $(shell uname -m)
# Collect all the C files to compile into MODULE_big.
OBJS = $(subst .c,.o, $(wildcard src/*.c src/*/*.c))

# clickhouse-c is a header-only single-header library. Override
# CH_C_DIR to point elsewhere when developing against a local checkout.
CH_C_DIR ?= vendor/clickhouse-c
# clickhouse-c and pg-clickhouse-c are header-only. pg-clickhouse-c vendors the
# clickhouse-c commit its own signatures compile against, so take that pin
# rather than a second checkout. Override CH_C_DIR / PGCH_DIR to point
# elsewhere when developing against a local checkout.
PGCH_DIR = vendor/pg-clickhouse-c
CH_C_DIR = $(PGCH_DIR)/clickhouse-c

# Own headers via -I; vendored and PG server headers via -isystem so their
# own warnings (eg -Wsign-compare in PG array.h macros) don't trip -Werror.
PG_CPPFLAGS = -I./src/include -isystem $(CH_C_DIR) -isystem $(shell $(PG_CONFIG) --includedir-server)
# PGCH_MSG_PREFIX prefixes every message pg-clickhouse-c raises; it expands
# inside the ereport macros, so every TU must see the same definition.
PG_CPPFLAGS = -I./src/include -isystem $(CH_C_DIR) -isystem $(PGCH_DIR) -isystem $(shell $(PG_CONFIG) --includedir-server) -DPGCH_MSG_PREFIX='"pg_clickhouse: "'

# Link OpenSSL (for TLS in the binary driver), curl (for the HTTP driver),
# libuuid (for http_streaming.c's query-id generator), and lz4 / zstd
Expand All @@ -50,13 +55,15 @@ EXTRA_CLEAN = sql/$(EXTENSION)--$(EXTVERSION).sql src/include/version.h compile_
PGXS := $(shell $(PG_CONFIG) --pgxs)
include $(PGXS)

# Clone clickhouse-c submodule.
$(CH_C_DIR)/clickhouse.h: .gitmodules
git submodule update --init
# Clone vendored submodules, clickhouse-c nested inside pg-clickhouse-c. Spelled
# out rather than via CH_C_DIR / PGCH_DIR: an override points at a checkout git
# knows nothing about.
vendor/pg-clickhouse-c/clickhouse-c/clickhouse.h vendor/pg-clickhouse-c/pg-clickhouse.h: .gitmodules
git submodule update --init --recursive

# Require clickhouse-c and the version header. The bitcode twins compile the
# same sources, so they need the same generated header.
$(OBJS) $(OBJS:.o=.bc): $(CH_C_DIR)/clickhouse.h src/include/version.h
# Require both vendored libraries and the version header. The bitcode twins
# compile the same sources, so they need the same generated header.
$(OBJS) $(OBJS:.o=.bc): $(CH_C_DIR)/clickhouse.h $(PGCH_DIR)/pg-clickhouse.h src/include/version.h

# Require the versioned SQL script.
all: sql/$(EXTENSION)--$(EXTVERSION).sql
Expand Down
70 changes: 9 additions & 61 deletions src/binary/binary.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
/*
* binary.c
*
* Core glue for the binary driver. Owns the chc_alloc thunks (palloc on
* CurrentMemoryContext, MCXT_ALLOC_HUGE so block buffers can escape the 1GB
* cap), error mapping into ereport, and a couple of small type helpers shared
* across the subdir.
*
* This also holds the CHC_IMPLEMENTATION define for the clickhouse-c
* header-only library; the other .c files in src/binary include the
* same headers without that define and link against the bodies emitted
* here.
* Core glue for the binary driver. Holds the CHC_IMPLEMENTATION and
* PGCH_IMPLEMENTATION defines for the header-only clickhouse-c and
* pg-clickhouse-c libraries; the other .c files in src/binary include the
* same headers without those defines and link against the bodies emitted
* here. pgch_in_alloc needs both in the same TU, so they stay together.
*
* API lives in src/include/binary.h. Driver internals shared between
* connection.c / select.c / insert.c live in binary_internal.h
Expand All @@ -28,50 +24,11 @@
#include "clickhouse-posix-io.h"
#include "clickhouse.h"

#include "binary_internal.h"

static void*
pg_chc_palloc(void* ud pg_attribute_unused(), size_t n) {
return MemoryContextAllocExtended(CurrentMemoryContext, n, MCXT_ALLOC_HUGE);
}

static void*
pg_chc_repalloc(
void* ud pg_attribute_unused(),
void* p,
size_t old_bytes pg_attribute_unused(),
size_t new_bytes
) {
if (!p) {
return MemoryContextAllocExtended(
CurrentMemoryContext, new_bytes, MCXT_ALLOC_HUGE
);
}
return repalloc_huge(p, new_bytes);
}
#define PGCH_IMPLEMENTATION
#include "pg-clickhouse-decode.h"
#include "pg-clickhouse-encode.h"

static void
pg_chc_pfree(
void* ud pg_attribute_unused(),
void* p,
size_t bytes pg_attribute_unused()
) {
if (p) {
pfree(p);
}
}

const chc_alloc pg_chc_alloc = {
.ud = NULL,
.alloc = pg_chc_palloc,
.realloc = pg_chc_repalloc,
.free = pg_chc_pfree,
};

/* power-of-10 lookup; CH bounds DateTime64 / Decimal scale to [0, 9] */
const int64_t pow10i[10] = {
1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000,
};
#include "binary_internal.h"

/*
* output_format_native_write_json_as_string exists on the server from
Expand All @@ -93,12 +50,3 @@ server_supports_json_as_string(const chc_client* c) {
}
return false;
}

void
raise_chc(const chc_err* err, int sqlstate, const char* prefix) {
const char* m = (err && err->msg[0]) ? err->msg : "unknown error";

ereport(
ERROR, errcode(sqlstate), errmsg("pg_clickhouse: %s%s", prefix ? prefix : "", m)
);
}
Loading
Loading