pg_stat_ch

pg_stat_ch

pg_stat_ch : Export PostgreSQL query telemetry to ClickHouse

Overview

ID Extension Package Version Category License Language
6020
pg_stat_ch
pg_stat_ch
0.3.3
STAT
Apache-2.0
C++
Attribute Has Binary Has Library Need Load Has DDL Relocatable Trusted
--sLd--
No
Yes
Yes
Yes
no
no
Relationships
See Also
pg_tracing
pg_stat_monitor
pg_stat_kcache
powa

Requires shared_preload_libraries = pg_stat_ch; README recommends track_io_timing = on.

Packages

Type Repo Version PG Major Compatibility Package Pattern Dependencies
EXT
PIGSTY
0.3.3
18
17
16
15
14
pg_stat_ch -
RPM
PIGSTY
0.3.3
18
17
16
15
14
pg_stat_ch_$v -
DEB
PIGSTY
0.3.3
18
17
16
15
14
postgresql-$v-pg-stat-ch -
Linux / PG PG18 PG17 PG16 PG15 PG14
el8.x86_64
MISS
MISS
MISS
MISS
MISS
el8.aarch64
MISS
MISS
MISS
MISS
MISS
el9.x86_64
PIGSTY 0.3.3
PIGSTY 0.3.3
PIGSTY 0.3.3
MISS
MISS
el9.aarch64
PIGSTY 0.3.3
PIGSTY 0.3.3
PIGSTY 0.3.3
MISS
MISS
el10.x86_64
PIGSTY 0.3.3
PIGSTY 0.3.3
PIGSTY 0.3.3
MISS
MISS
el10.aarch64
PIGSTY 0.3.3
PIGSTY 0.3.3
PIGSTY 0.3.3
MISS
MISS
d12.x86_64
PIGSTY 0.3.3
PIGSTY 0.3.3
PIGSTY 0.3.3
MISS
MISS
d12.aarch64
PIGSTY 0.3.3
PIGSTY 0.3.3
PIGSTY 0.3.3
MISS
MISS
d13.x86_64
PIGSTY 0.3.3
PIGSTY 0.3.3
PIGSTY 0.3.3
MISS
MISS
d13.aarch64
PIGSTY 0.3.3
PIGSTY 0.3.3
PIGSTY 0.3.3
MISS
MISS
u22.x86_64
PIGSTY 0.3.3
PIGSTY 0.3.3
PIGSTY 0.3.3
MISS
MISS
u22.aarch64
PIGSTY 0.3.3
PIGSTY 0.3.3
PIGSTY 0.3.3
MISS
MISS
u24.x86_64
PIGSTY 0.3.3
PIGSTY 0.3.3
PIGSTY 0.3.3
MISS
MISS
u24.aarch64
PIGSTY 0.3.3
PIGSTY 0.3.3
PIGSTY 0.3.3
MISS
MISS
Package Version OS ORG SIZE File URL
pg_stat_ch_18 0.3.3 el9.x86_64 pigsty 1.1 MiB pg_stat_ch_18-0.3.3-1PIGSTY.el9.x86_64.rpm
pg_stat_ch_18 0.3.3 el9.aarch64 pigsty 1.1 MiB pg_stat_ch_18-0.3.3-1PIGSTY.el9.aarch64.rpm
pg_stat_ch_18 0.3.3 el10.x86_64 pigsty 1.0 MiB pg_stat_ch_18-0.3.3-1PIGSTY.el10.x86_64.rpm
pg_stat_ch_18 0.3.3 el10.aarch64 pigsty 994.3 KiB pg_stat_ch_18-0.3.3-1PIGSTY.el10.aarch64.rpm
postgresql-18-pg-stat-ch 0.3.3 d12.x86_64 pigsty 962.0 KiB postgresql-18-pg-stat-ch_0.3.3-1PIGSTY~bookworm_amd64.deb
postgresql-18-pg-stat-ch 0.3.3 d12.aarch64 pigsty 865.7 KiB postgresql-18-pg-stat-ch_0.3.3-1PIGSTY~bookworm_arm64.deb
postgresql-18-pg-stat-ch 0.3.3 d13.x86_64 pigsty 987.7 KiB postgresql-18-pg-stat-ch_0.3.3-1PIGSTY~trixie_amd64.deb
postgresql-18-pg-stat-ch 0.3.3 d13.aarch64 pigsty 885.3 KiB postgresql-18-pg-stat-ch_0.3.3-1PIGSTY~trixie_arm64.deb
postgresql-18-pg-stat-ch 0.3.3 u22.x86_64 pigsty 5.6 MiB postgresql-18-pg-stat-ch_0.3.3-1PIGSTY~jammy_amd64.deb
postgresql-18-pg-stat-ch 0.3.3 u22.aarch64 pigsty 5.3 MiB postgresql-18-pg-stat-ch_0.3.3-1PIGSTY~jammy_arm64.deb
postgresql-18-pg-stat-ch 0.3.3 u24.x86_64 pigsty 922.1 KiB postgresql-18-pg-stat-ch_0.3.3-1PIGSTY~noble_amd64.deb
postgresql-18-pg-stat-ch 0.3.3 u24.aarch64 pigsty 880.8 KiB postgresql-18-pg-stat-ch_0.3.3-1PIGSTY~noble_arm64.deb
Package Version OS ORG SIZE File URL
pg_stat_ch_17 0.3.3 el9.x86_64 pigsty 1.1 MiB pg_stat_ch_17-0.3.3-1PIGSTY.el9.x86_64.rpm
pg_stat_ch_17 0.3.3 el9.aarch64 pigsty 1.1 MiB pg_stat_ch_17-0.3.3-1PIGSTY.el9.aarch64.rpm
pg_stat_ch_17 0.3.3 el10.x86_64 pigsty 1.0 MiB pg_stat_ch_17-0.3.3-1PIGSTY.el10.x86_64.rpm
pg_stat_ch_17 0.3.3 el10.aarch64 pigsty 991.6 KiB pg_stat_ch_17-0.3.3-1PIGSTY.el10.aarch64.rpm
postgresql-17-pg-stat-ch 0.3.3 d12.x86_64 pigsty 961.6 KiB postgresql-17-pg-stat-ch_0.3.3-1PIGSTY~bookworm_amd64.deb
postgresql-17-pg-stat-ch 0.3.3 d12.aarch64 pigsty 865.8 KiB postgresql-17-pg-stat-ch_0.3.3-1PIGSTY~bookworm_arm64.deb
postgresql-17-pg-stat-ch 0.3.3 d13.x86_64 pigsty 988.7 KiB postgresql-17-pg-stat-ch_0.3.3-1PIGSTY~trixie_amd64.deb
postgresql-17-pg-stat-ch 0.3.3 d13.aarch64 pigsty 884.0 KiB postgresql-17-pg-stat-ch_0.3.3-1PIGSTY~trixie_arm64.deb
postgresql-17-pg-stat-ch 0.3.3 u22.x86_64 pigsty 5.6 MiB postgresql-17-pg-stat-ch_0.3.3-1PIGSTY~jammy_amd64.deb
postgresql-17-pg-stat-ch 0.3.3 u22.aarch64 pigsty 5.3 MiB postgresql-17-pg-stat-ch_0.3.3-1PIGSTY~jammy_arm64.deb
postgresql-17-pg-stat-ch 0.3.3 u24.x86_64 pigsty 922.5 KiB postgresql-17-pg-stat-ch_0.3.3-1PIGSTY~noble_amd64.deb
postgresql-17-pg-stat-ch 0.3.3 u24.aarch64 pigsty 879.8 KiB postgresql-17-pg-stat-ch_0.3.3-1PIGSTY~noble_arm64.deb
Package Version OS ORG SIZE File URL
pg_stat_ch_16 0.3.3 el9.x86_64 pigsty 1.1 MiB pg_stat_ch_16-0.3.3-1PIGSTY.el9.x86_64.rpm
pg_stat_ch_16 0.3.3 el9.aarch64 pigsty 1.1 MiB pg_stat_ch_16-0.3.3-1PIGSTY.el9.aarch64.rpm
pg_stat_ch_16 0.3.3 el10.x86_64 pigsty 1.0 MiB pg_stat_ch_16-0.3.3-1PIGSTY.el10.x86_64.rpm
pg_stat_ch_16 0.3.3 el10.aarch64 pigsty 994.6 KiB pg_stat_ch_16-0.3.3-1PIGSTY.el10.aarch64.rpm
postgresql-16-pg-stat-ch 0.3.3 d12.x86_64 pigsty 961.9 KiB postgresql-16-pg-stat-ch_0.3.3-1PIGSTY~bookworm_amd64.deb
postgresql-16-pg-stat-ch 0.3.3 d12.aarch64 pigsty 865.5 KiB postgresql-16-pg-stat-ch_0.3.3-1PIGSTY~bookworm_arm64.deb
postgresql-16-pg-stat-ch 0.3.3 d13.x86_64 pigsty 989.7 KiB postgresql-16-pg-stat-ch_0.3.3-1PIGSTY~trixie_amd64.deb
postgresql-16-pg-stat-ch 0.3.3 d13.aarch64 pigsty 884.8 KiB postgresql-16-pg-stat-ch_0.3.3-1PIGSTY~trixie_arm64.deb
postgresql-16-pg-stat-ch 0.3.3 u22.x86_64 pigsty 5.6 MiB postgresql-16-pg-stat-ch_0.3.3-1PIGSTY~jammy_amd64.deb
postgresql-16-pg-stat-ch 0.3.3 u22.aarch64 pigsty 5.3 MiB postgresql-16-pg-stat-ch_0.3.3-1PIGSTY~jammy_arm64.deb
postgresql-16-pg-stat-ch 0.3.3 u24.x86_64 pigsty 921.8 KiB postgresql-16-pg-stat-ch_0.3.3-1PIGSTY~noble_amd64.deb
postgresql-16-pg-stat-ch 0.3.3 u24.aarch64 pigsty 879.5 KiB postgresql-16-pg-stat-ch_0.3.3-1PIGSTY~noble_arm64.deb

Source

pig build pkg pg_stat_ch;		# build rpm/deb

Install

Make sure PGDG and PIGSTY repo available:

pig repo add pgsql -u   # add both repo and update cache

Install this extension with pig:

pig install pg_stat_ch;		# install via package name, for the active PG version

pig install pg_stat_ch -v 18;   # install for PG 18
pig install pg_stat_ch -v 17;   # install for PG 17
pig install pg_stat_ch -v 16;   # install for PG 16

Config this extension to shared_preload_libraries:

shared_preload_libraries = 'pg_stat_ch';

Create this extension with:

CREATE EXTENSION pg_stat_ch;

Usage

Syntax:

CREATE EXTENSION pg_stat_ch;
SELECT pg_stat_ch_version();
SELECT * FROM pg_stat_ch_stats();

Sources: README, Blog post

pg_stat_ch captures per-query execution telemetry in PostgreSQL and exports raw events to ClickHouse in real time. The upstream project contrasts this with pg_stat_statements: instead of aggregating inside PostgreSQL, it sends raw events to ClickHouse for downstream analysis.

Architecture

The README describes a single pipeline:

PostgreSQL hooks -> shared memory queue -> background worker -> ClickHouse

Design goals called out upstream include:

  • no network I/O on the query path
  • bounded memory via a fixed-size ring buffer
  • raw event export instead of local aggregation
  • graceful degradation when the queue overflows or ClickHouse is unavailable

Setup

The extension must be preloaded and configured with ClickHouse connection settings:

shared_preload_libraries = 'pg_stat_ch'
track_io_timing = on

pg_stat_ch.clickhouse_host = 'localhost'
pg_stat_ch.clickhouse_port = 9000
pg_stat_ch.clickhouse_database = 'pg_stat_ch'
pg_stat_ch.clickhouse_use_tls = on
pg_stat_ch.clickhouse_skip_tls_verify = off

After PostgreSQL restart and ClickHouse schema setup:

CREATE EXTENSION pg_stat_ch;

SQL API

The README documents these SQL functions:

  • pg_stat_ch_version()
  • pg_stat_ch_stats()
  • pg_stat_ch_reset()

pg_stat_ch_stats() exposes queue and exporter counters so you can verify that events are being captured and flushed.

What Gets Captured

The current README lists support for:

  • query timing and row counts
  • buffer usage and WAL usage
  • CPU time
  • DML, DDL, and utility statements
  • SQLSTATE and error levels
  • JIT instrumentation on PostgreSQL 15+
  • parallel worker statistics on PostgreSQL 18+
  • client context such as application name and client IP

The project currently states full support for PostgreSQL 16, 17, and 18.

Last updated on