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.4.2 |
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_clickhouse pg_stat_monitor pgmonitor pg_datasentinel duckdb_fdw pgsentinel pgnodemx pg_duckdb system_stats pg_profile |
Packages
| Linux / PG | PG19 | PG18 | PG17 | PG16 | PG15 |
|---|---|---|---|---|---|
| el8.x86_64 | N/A | PIGSTY 0.4.0 | PIGSTY 0.4.0 | PIGSTY 0.4.0 | N/A |
| el8.aarch64 | N/A | PIGSTY 0.4.0 | PIGSTY 0.4.0 | PIGSTY 0.4.0 | N/A |
| el9.x86_64 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | N/A |
| el9.aarch64 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | N/A |
| el10.x86_64 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | N/A |
| el10.aarch64 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | N/A |
| d12.x86_64 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | N/A |
| d12.aarch64 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | N/A |
| d13.x86_64 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | N/A |
| d13.aarch64 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | N/A |
| u22.x86_64 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | N/A |
| u22.aarch64 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | N/A |
| u24.x86_64 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | N/A |
| u24.aarch64 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | N/A |
| u26.x86_64 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | N/A |
| u26.aarch64 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | PIGSTY 0.4.2 | N/A |
Source
github.com/ClickHouse/pg_stat_ch
pg_stat_ch-0.4.2.tar.gz
Install
Make sure PGDG and PIGSTY repo available:
Install this extension with pig:
Config this extension to shared_preload_libraries:
Create this extension with:
Usage
Sources:
pg_stat_ch captures per-query PostgreSQL execution telemetry and exports raw events to ClickHouse through a shared-memory queue and background worker. The upstream project positions it as a raw-event alternative to pg_stat_statements, with aggregation and dashboards handled in ClickHouse instead of PostgreSQL.
Required Setup
pg_stat_ch must be preloaded and pointed at a ClickHouse database:
The README says PostgreSQL 16, 17, and 18 are fully supported; the distribution 0.4.1 keeps the SQL/control version at 0.4; the PGXN release changes exporter defaults and ClickHouse schema setup.
SQL API
pg_stat_ch_version()returns the extension version.pg_stat_ch_stats()exposes queue and exporter counters.pg_stat_ch_reset()clears queue counters.pg_stat_ch_flush()triggers an immediate export flush.
Important GUCs
pg_stat_ch.enabledtoggles collection.pg_stat_ch.queue_capacityandpg_stat_ch.string_area_sizesize the shared-memory buffers.pg_stat_ch.flush_interval_msandpg_stat_ch.batch_maxcontrol exporter cadence and batch size.pg_stat_ch.log_min_elevelcontrols which errors are captured.
What Gets Captured
- Query timing, row counts, buffer usage, WAL usage, and CPU time.
- DML, DDL, and utility statements.
- SQLSTATE and error levels.
- JIT metrics on PostgreSQL 15+.
- Parallel worker stats on PostgreSQL 18+.
- Application name, client IP, and query text up to the upstream truncation limit.
Release 0.3.6 replaces the normalize cache with a queryId-keyed LRU cache and replaces the OpenTelemetry SDK with a direct-protobuf exporter plus sampling support. Existing SQL usage remains centered on the event tables, summary views, and reset/version helper functions above.
Distribution 0.4.1
The 0.4.1 README raises the default pg_stat_ch.queue_capacity to 131072, sets flush_interval_ms to 500, and caps batch_max at the queue capacity. Manual ClickHouse setup now creates the pg_stat_ch database and applies the Goose migrations. Queries use the ts column for the recent-event window.
Caveats
- The design intentionally drops events on queue overflow instead of blocking the foreground query path.
- ClickHouse schema creation is a required part of setup; upstream quickstart scripts preload it automatically, but manual deployments must load the schema separately.
- Pigsty’s current RPM refresh for
pg_stat_chis PostgreSQL 16-18 on EL9/EL10 only; Debian/Ubuntu packages remain PostgreSQL 16-18 on the active targets.