« All posts

pg_stat_ch Streams PostgreSQL Query Telemetry into ClickHouse

pg_stat_ch is an open-source PostgreSQL extension that streams raw per-query telemetry to ClickHouse, offloading analytics from the database engine.

pg_stat_ch is a PostgreSQL extension that captures per-query runtime telemetry and streams it to ClickHouse in real time. Unlike pg_stat_statements, which aggregates statistics inside PostgreSQL, it exports raw events and lets ClickHouse's analytical engine handle aggregation such as p50/p95/p99 percentiles, slowest queries, and error trends. The architecture relies on PostgreSQL hooks, a shared-memory MPSC ring buffer, and a background worker, avoiding synchronous network I/O on the query path and keeping p99 overhead around 5 microseconds per statement.

The extension captures detailed metrics including timing, row counts, buffer and WAL usage, CPU time, all DML/DDL/utility statement types, SQLSTATE error codes, JIT instrumentation on PG15+, and parallel worker stats on PG18+. When the queue overflows or ClickHouse becomes unavailable, events are dropped with counters rather than blocking PostgreSQL queries. PostgreSQL 16, 17, and 18 are fully supported.

For engineers, this offers low-overhead, granular, time-series observability into production query behavior. Leveraging ClickHouse's analytical capabilities enables queries like per-application slowest-query breakdowns and percentile analysis, delivering a richer and more flexible observability layer than the traditional pg_stat_statements approach.

» SourceHashnode #1