---
title: "pg_stat_ch: a PostgreSQL extension that exports every metric to ClickHouse"
description: "pg_stat_ch is an open-source (Apache 2.0) PostgreSQL 16–18 extension that captures every statement execution, including failed queries and DDL, as a fixed-size ~4.6KB event with 45 fields and streams it to ClickHouse for APM-style analysis."
url: "https://swe.quest/articles/pg-stat-ch-a-postgresql-extension-that-exports-every-metric-to-clickhouse-44a394f2889686fc"
canonical: "https://clickhouse.com/blog/pg_stat_ch-postgres-extension-stats-to-clickhouse"
source: "ClickHouse"
published: "2026-02-13"
difficulty: "hard"
kind: "Deep dive"
reading_minutes: 16
tags: ["postgres","observability","performance","concurrency","analytics"]
---

# pg\_stat\_ch: a PostgreSQL extension that exports every metric to ClickHouse

A post on [ClickHouse](https://swe.quest/sources/clickhouse.md), published 2026-02-13, 16 min read, Deep dive. Original: [https://clickhouse.com/blog/pg\_stat\_ch-postgres-extension-stats-to-clickhouse](https://clickhouse.com/blog/pg_stat_ch-postgres-extension-stats-to-clickhouse)

## Summary

pg\_stat\_ch is an open-source (Apache 2.0) PostgreSQL 16–18 extension that captures every statement execution, including failed queries and DDL, as a fixed-size ~4.6KB event with 45 fields and streams it to ClickHouse for APM-style analysis. On the hot path each backend memcpys the event into a cache-line-padded shared-memory ring buffer. A background worker flushes batches once a second over ClickHouse's native protocol with LZ4 compression, and materialized views handle all aggregation on the ClickHouse side. Key design choices are fixed-size events for predictable memory (query text is truncated at 2KB), dropping events instead of applying back-pressure, and a try-lock path with per-backend batching that cuts lock acquisitions about 5x. On pgbench TPC-B with 32 clients, extension code costs about 2% CPU but throughput drops about 11% because of contention on the enqueue lock.

Written by swe.quest, not by the author.

**Difficulty:** Hard: written for specialists in the field

## Topics

- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [Observability](https://swe.quest/tags/observability.md) (Reliability & operations)
- [Performance engineering](https://swe.quest/tags/performance.md) (Reliability & operations)
- [Concurrency](https://swe.quest/tags/concurrency.md) (Compilers & runtimes)
- [Analytics & OLAP](https://swe.quest/tags/analytics.md) (Data & databases)

## Prerequisites

- PostgreSQL extension hooks ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))
- shared memory and atomics
- ClickHouse materialized views ([easy posts on Analytics & OLAP](https://swe.quest/tags/analytics.md?difficulty=easy))
- false sharing

## Keywords

pg\_stat\_ch, PostgreSQL, ClickHouse, pg\_stat\_statements, LZ4, clickhouse-cpp, pgbench

## Related posts

- [Introducing Postgres Query Insights in ClickHouse Cloud](https://swe.quest/articles/introducing-postgres-query-insights-in-clickhouse-cloud-a9697a827409b984.md): ClickHouse · 2026-05-07 · Medium · 7 min read · PostgreSQL, Observability, Performance engineering, Analytics & OLAP
  Postgres Query Insights, now in preview for ClickHouse Cloud Managed Postgres, ranks every normalized query pattern by impact and aggregates per-execution counters (CPU, cache vs disk reads, temp spills, unlaunched parallel workers, WAL) to explain why a pattern is slow.
- [Postgres week in the Netherlands: PGDay Lowlands & Percona Live 2026](https://swe.quest/articles/postgres-week-in-the-netherlands-pgday-lowlands-percona-live-2026-0eb593178690a919.md): ClickHouse · 2026-09-18 · Easy · 7 min read · PostgreSQL, Observability, Analytics & OLAP, Query engines
  Notes from PGDay Lowlands in Utrecht and Percona Live in Amsterdam, covering two ClickHouse-maintained Postgres extensions and several PostgreSQL 19 features. pg\_clickhouse is a foreign data wrapper that lets unmodified Postgres queries and ORMs run against ClickHouse via pushdown, with 15 of 22 TPC-H queries fully pushed down. pg\_stat\_ch streams per-query execution events through a shared-memory ring buffer to ClickHouse without back-pressure.
- [Introducing pg\_clickhouse: A Postgres extension for querying ClickHouse](https://swe.quest/articles/introducing-pg-clickhouse-a-postgres-extension-for-querying-clickhouse-98de49fb9ae8bb75.md): ClickHouse · 2025-12-10 · Medium · 11 min read · PostgreSQL, Query engines, Analytics & OLAP, SQL
  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.
- [PostgreSQL + ClickHouse as the Open Source unified data stack](https://swe.quest/articles/postgresql-clickhouse-as-the-open-source-unified-data-stack-2760f19ba52907df.md): ClickHouse · 2026-01-27 · Easy · 5 min read · PostgreSQL, Analytics & OLAP, Event-driven architecture, Replication
  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.
- [Postgres at Open House NYC](https://swe.quest/articles/postgres-at-open-house-nyc-5b2de1c38cc64f40.md): ClickHouse · 2026-10-13 · Easy · 6 min read · PostgreSQL, Replication, Event-driven architecture, Analytics & OLAP
  ClickHouse used its Open House NYC keynote to launch WalShadow, an open-source engine that replicates Postgres into ClickHouse from the physical WAL instead of logical replication.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
