pg_stat_ch
CLAUDE.md129 starsChanged 8 months agoApache-2.0View on GitHub
pg_stat_ch is a PostgreSQL 16+ extension written in C++ that captures query execution telemetry via server hooks and exports it to ClickHouse. The architecture is: hooks (foreground) → shared-memory queue → bgworker exporter → ClickHouse events_raw All aggregation (p50/p95/p99, top queries, errors) happens in ClickHouse via materialized views, not in the extension. Most third-party dependencies (OpenTelemetry, Arrow, OpenSSL, lz4, zstd) are managed via vcpkg (manifest mode). The vcpkg submodule lives at third_party/vcpkg; the manifest is vcpkg.json. The ClickHouse client is…
In this repository5 other files it gives its agents.
- cpp-header-templateSkill
- cpp-naming-checkSkill
- cpp-reviewSkill
- create-new-releaseSkill
- google-cpp-styleSkill
The file
We split this CLAUDE.md into the rules it gives an agent
5 rules. About 0 are common, 1 is distinctive, and 4 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
4
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 10
All aggregation (p50/p95/p99, top queries, errors) happens in ClickHouse via materialized views, not in the extension.
- Very specific: line 81
Use extern "C" blocks for PostgreSQL C ABI compatibility
- Distinctive: line 83
No STL in shared memory; use Postgres allocators and shmem APIs
- Very specific: line 108
Use #if PG_VERSION_NUM >= XXXXX for version-specific code:
- Very specific: line 125
Only bump default_version when SQL interface changes (new functions, types, etc.)
≈ “Common” is an estimate: at least 1 in 5 of the 29,248 CLAUDE.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Project Overview
pg_stat_ch is a PostgreSQL 16+ extension written in C++ that captures query execution telemetry via server hooks and exports it to ClickHouse. The architecture is:
hooks (foreground) → shared-memory queue → bgworker exporter → ClickHouse events_raw
All aggregation (p50/p95/p99, top queries, errors) happens in ClickHouse via materialized views, not in the extension.
Dependencies
Most third-party dependencies (OpenTelemetry, Arrow, OpenSSL, lz4, zstd) are managed via vcpkg (manifest mode). The vcpkg submodule lives at third_party/vcpkg; the manifest is vcpkg.json.
The ClickHouse client is clickhouse-c (https://github.com/ClickHouse/clickhouse-c), a header-only C library vendored as the third_party/clickhouse-c submodule. Its sole CHC_IMPLEMENTATION unit is src/export/clickhouse_c_impl.c; the C++ exporter includes the headers for declarations only.
First-time setup:
git submodule update --init # clone vcpkg + clickhouse-c
third_party/vcpkg/bootstrap-vcpkg.sh -disableMetricsmise.toml sets VCPKG_ROOT automatically when using mise run tasks.
Build Commands
mise run build # Debug build (current pg_config)
mise run build:release # Release build
mise run build:16 # Build for PostgreSQL 16
mise run build:17 # Build for PostgreSQL 17
mise run build:18 # Build for PostgreSQL 18
mise run build:19 # Build for PostgreSQL 19
mise run build:all # Build for all PG versions
mise run install # Install the extension
mise run clean # Clean build artifactsDevelopment Commands
mise run format # Format code with clang-format
mise run lint # Run clang-tidy linting
mise run compdb # Copy compile_commands.json to root (for IDE)
mise run configure # Configure with CMake (debug)Testing
# Via mise (uses PG 18)
mise run test:all # Run all tests
mise run test:regress # SQL regression tests
mise run test:tap # TAP tests (stress, concurrent, overflow)
mise run test:isolation # Isolation tests (race conditions)
# Via script (specify PG version)
./scripts/run-tests.sh 18 all
./scripts/run-tests.sh 17 regressTest types:
regress- SQL regression tests intest/regression/tap- Perl TAP tests int/(stress, concurrent sessions, overflow)isolation- Race condition tests inspecs/
Note: TAP tests require PostgreSQL built with --enable-tap-tests. Mise-installed versions don't include the Perl test modules; the script skips TAP tests gracefully when unavailable.
# we have a local build of PostgreSQL with tap tests enabled
# so we can run the tap tests against it
./scripts/run-tests.sh ../postgres/install_tap tapCode Style
- C++17 with Google style guide (
.clang-format) - Column limit: 100, 2-space indent
postgres.hmust be included first in any source file- Use
extern "C"blocks for PostgreSQL C ABI compatibility - Naming: CamelCase for classes/functions, lower_case for variables, kCamelCase for constants
- No STL in shared memory; use Postgres allocators and shmem APIs
Architecture
Source files:
src/pg_stat_ch.cc- Main entry point with_PG_init()and SQL functionsinclude/pg_stat_ch/pg_stat_ch.h- Public header with version macro and declarationssql/pg_stat_ch--0.1.sql- SQL function definitions
Build system:
- CMake with presets (default=debug, release, release-arm64)
- vcpkg manifest mode (
vcpkg.json) with custom triplets intriplets/ cmake/FindPostgreSQLServer.cmake- Finds PostgreSQL via pg_configcmake/CompilerWarnings.cmake- Strict warning flagscmake/GitVersion.cmake- Version extraction from git
Key PostgreSQL hooks to implement (see pg_stat_ch.md for details):
shmem_request_hook/shmem_startup_hook- Shared memory setuppost_parse_analyze_hook- Capture queryId earlyExecutorStart/Run/Finish/End_hook- Track executionProcessUtility_hook- Handle DDL/utility statementsemit_log_hook- Capture errors
Version Compatibility
Use #if PG_VERSION_NUM >= XXXXX for version-specific code:
- PG 19+: Query instrumentation changes, const JumbleState, updated shared memory APIs, core ComputeConstantLengths
- PG 18+:
execute_onceremoved from ExecutorRun - PG 17+: Unified nesting_level, separate block timing
- PG 15+: JIT instrumentation, temp_blk timing
Versioning
Two independent versions exist:
| Version | Location | Purpose |
Git tag (e.g., v0.1.5) | Release workflow | Build/release artifacts |
default_version | pg_stat_ch.control | PostgreSQL extension schema |
- Release workflow uses
git describe --tagsfor artifact naming - Extension version in
.controlmust match SQL filename (e.g.,0.1→pg_stat_ch--0.1.sql) - Only bump
default_versionwhen SQL interface changes (new functions, types, etc.) - Schema changes require a migration script:
pg_stat_ch--OLD--NEW.sql
Reference Projects
These projects are available in the workspace as references:
../pg_stat_monitor- Primary reference for PostgreSQL hook patterns, shared memory management, and query statistics collection. Our hook implementations are based on patterns from this project.~/s/pg_clickhouse- Sibling PG extension that embeds clickhouse-c; reference for the connection setup,CHC_IMPLEMENTATIONTU, and INSERT packet loop (src/binary/connection.c,insert.c)../pg_duckdb- Another PG extension in C++; reference for C++/PostgreSQL integration patterns../postgres- PostgreSQL source code for understanding internal APIs
Useful Skills
/cpp-review- Review C++ code against Google Style Guide/cpp-naming-check- Check naming conventions
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

