Introducing pg_clickhouse: A Postgres extension for querying ClickHouse
pg_clickhouse v0.1.0 is an Apache 2-licensed PostgreSQL foreign data wrapper, derived from clickhouse_fdw, that runs analytics queries on ClickHouse from Postgres so existing queries can work unmodified by changing search_path. It modernizes the code with PGXS builds, CI across PostgreSQL 13-18 and ClickHouse 22-25, TLS, and Bool/Decimal/JSON types. Its main advance is pushdown: ordered-set aggregates like percentile_cont() are rewritten to ClickHouse quantile() and FILTER clauses become -If combinators. SEMI JOIN pushdown plus planner cost tuning brought 21 of 22 TPC-H queries under 1s, with 12 fully pushed down.
Related posts
- Postgres FDW: Pushdown is a negotiation
ClickHouse hardMaster
pg_clickhouse, a Postgres foreign data wrapper for ClickHouse, decides clause by clause how much of a query to push down as ClickHouse SQL. One analytical query (join, GROUP BY, FILTER aggregate, percentile, window function, JSON predicate, LIMIT) runs in about 80 ms when fully pushed down, but takes minutes to half an hour when any single clause stays local, because grouped pushdown is all-or-nothing and millions of rows must stream back to Postgres. The post walks through the FDW planner callbacks (GetForeignPaths, GetForeignJoinPaths, GetForeignUpperPaths) and the deparser, then traces shipped steps such as cost-based join pushdown, emitting ALL INNER JOIN to keep Postgres join semantics, and mapping percentile_cont to quantile. It also explains why some translations were refused or revoked when they would have changed results.
- What's new in pg_clickhouse v0.10.0: Subqueries, TPC-H Speedups, C Driver, and Aggregates
ClickHouse hardMaster
pg_clickhouse v0.10.0 pushes correlated scalar subqueries, IN and NOT IN down to ClickHouse as remote subqueries and anti/semi-joins, raising full TPC-H pushdown from 12 to 16 of 22 queries. Q17 drops from 32.7 s to 37 ms, beating native PostgreSQL's 2.1 s. A NULL-semantics layer tracks how each expression's result is consumed and injects CASE guards only when operands may be NULL, so ClickHouse's two-valued IN matches PostgreSQL's three-valued logic; transform_null_in is pinned to 0 so a server profile can't break those guards. The release also switches from clickhouse-cpp to the plain-C clickhouse-c driver, which fixes setjmp/longjmp-vs-exception crashes and streams results, and gives each concurrent foreign scan its own connection. Statistical, ordered-set and partitionwise aggregates now push down, and builtin function pushdown is opt-in to preserve Postgres semantics.
- pg_clickhouse 0.11.1: Now with pgvector, UPDATE, DELETE pushdown
ClickHouse mediumAdept
pg_clickhouse 0.11.1 maps pgvector's vector type to ClickHouse Array(Float32/Float64) columns. It pushes pgvector distance operators down to ClickHouse functions such as L2Distance and cosineDistance, and reports in EXPLAIN whether a ClickHouse vector_similarity HNSW index applies. It also pushes UPDATE and DELETE down as ClickHouse lightweight updates and deletes on MergeTree tables, along with TRUNCATE; lightweight updates need block number/offset settings enabled and suit changes to under about 10% of a table. Other changes include regr_* aggregate pushdown, OrNull aggregates that match Postgres NULL semantics, JSONB subscript pushdown, session timezone alignment, and Decimal(76,38) for unconstrained numeric.
- ClickHouse and PostgreSQL - a Match Made in Data Heaven - part 1
ClickHouse mediumAdept
ClickHouse can query PostgreSQL data in place through the postgresql table function and the PostgreSQL table engine. Simple WHERE clauses are pushed down to Postgres, while aggregation, joins, sorting and LIMIT run in ClickHouse. On the 28M-row UK house price dataset, a low-selectivity filter makes Postgres choose a parallel sequential scan over the index, taking about 28s, and querying through ClickHouse gives similar times because Postgres still does the scan. Queries using ClickHouse-specific functions such as toYear are not pushed down and can run much slower. Rewriting joins with conditional aggregates like medianIf, or copying the data into local MergeTree tables, is what delivers the large speedups.
- PostgreSQL + ClickHouse as the Open Source unified data stack
ClickHouse easyNovice
An open source stack pairs PostgreSQL as the transactional system of record with ClickHouse for analytics, connected by PeerDB for change data capture and the pg_clickhouse extension, which transparently offloads analytical queries from Postgres to ClickHouse. Two patterns are described: CDC replication for operational analytics on application data, and split-writes for high-volume logs, metrics and events that never need to live in Postgres. Adopting it involves creating a ClickHouse database, replicating via PeerDB, configuring pg_clickhouse and pointing queries at its tables, so application changes stay minimal. A managed version in ClickHouse Cloud swaps PeerDB for ClickPipes and preinstalls the extension.