Home / Companies / ClickHouse / Blog / Post Details
Content Deep Dive

pg_stat_ch: a PostgreSQL extension that exports every metric to ClickHouse

Blog post from ClickHouse

Post Details
Company
Date Published
Author
Why build pg_stat_ch? #
Word Count
3,658
Language
English
Hacker News Points
-
Summary

pg_stat_ch is a newly open-sourced PostgreSQL extension that records each query execution as a 4.6KB event and streams it into ClickHouse for detailed analysis, akin to application performance monitoring. It supports PostgreSQL versions 16 to 18 and is designed to provide deep insights into query behavior, including latency, top queries, and error tracking, which are not available out-of-the-box with PostgreSQL. The extension captures comprehensive query data, minimizes overhead on PostgreSQL, and prioritizes nonintrusive operation by avoiding back-pressure, ensuring that PostgreSQL performance remains unaffected even if ClickHouse slows down. The architecture uses fixed-size events to maintain predictable memory usage and employs efficient data handling methods, such as LZ4 compression, to minimize storage and network bandwidth. While pg_stat_ch is not yet in production, it seeks feedback from users operating at scale to refine its capabilities. It offers a complementary solution to existing tools, like pg_stat_statements and pg_stat_monitor, by providing unlimited retention and raw event granularity, allowing for sophisticated queries and analyses using ClickHouse's SQL capabilities. The extension is part of a broader strategy to integrate PostgreSQL with ClickHouse, forming a unified data stack for scalable applications, and future enhancements include query plan capture, sampling support, and pre-built dashboard templates.