Quick Start (Docker)
From the repo root:pg_stat_ch schema preloaded. For endpoints and stack details, see docker/quickstart/README.md.
Manual Setup
For production deployments, apply the canonical schema directly (from the repo root):docker/init/00-schema.sql) is the single source of truth and includes:
- Full
events_rawtable with all columns documented (what metrics mean, when values are HIGH/LOW) - 4 materialized views for common analytics patterns
- Column comments explaining how to interpret each metric
- Canonical table/materialized-view DDL used by deployments
Schema Overview
events_raw Table
The primary table stores one row per query execution. Events are exported in batches by the pg_stat_ch background worker. Key column groups:
| Category | Columns | Notes |
|---|---|---|
| Identity & Timing | ts_start, duration_us, db, username, pid, query_id | Core fields for every event |
| Query | cmd_type, rows, query | Command classification and text |
| Shared Buffers | shared_blks_hit/read/dirtied/written | Cache hit ratio = hit / (hit + read) |
| Local Buffers | local_blks_hit/read/dirtied/written | Temp table I/O |
| Temp Files | temp_blks_read/written | Non-zero = work_mem pressure |
| I/O Timing | shared/local/temp_blk_read/write_time_us | Requires track_io_timing=on |
| WAL | wal_records, wal_fpi, wal_bytes | Write-ahead log metrics |
| CPU | cpu_user_time_us, cpu_sys_time_us | User vs kernel time |
| JIT (PG15+) | jit_functions, jit_*_time_us | JIT compilation overhead |
| Parallel (PG18+) | parallel_workers_planned/launched | Worker efficiency |
| Errors | err_sqlstate, err_elevel, err_message | Error tracking |
| Client | app, client_addr | Load attribution |
Materialized Views
The schema includes 4 materialized views. All aggregation happens in ClickHouse, not in PostgreSQL.1. events_recent_1h — Real-time Debugging
A copy of events_raw with a 1-hour TTL for fast access to recent events.
Use cases: Real-time dashboards, “what just happened?” debugging, sub-second refresh monitoring.
2. query_stats_5m — Query Performance Dashboard
Pre-aggregated query statistics in 5-minute buckets using ClickHouse AggregateFunction columns.
Use cases: QPS trends, latency percentiles (p95/p99), identifying slow queries, capacity planning.
Querying aggregate states: This MV uses -State / -Merge functions:
3. db_app_user_1m — Load by Application/User
1-minute buckets grouped by database, application, and user with error counts.
Use cases: Identifying load sources, per-tenant chargeback, spotting misbehaving applications.
4. errors_recent — Error Investigation
Recent errors with a 7-day TTL, filtered from events_raw where err_elevel > 0.
Use cases: Incident investigation, error rate monitoring, recurring error patterns.
Example Queries
Queries follow a typical workflow: find problems with MVs, then drill into raw events.Find Slowest Queries (MV)
Identify worst tail latency from the pre-aggregatedquery_stats_5m view. The -State/-Merge pattern is how ClickHouse finalizes pre-aggregated columns.
Latency Trend for a Specific Query
After finding a slowquery_id above, see how its latency changes over time. Impossible with pg_stat_statements since it only stores cumulative aggregates.
Cache Miss Outliers
Find individual executions that read the most from disk.Errors by SQLSTATE
Find which error types are most frequent. Filters onerr_elevel >= 21 (ERROR and above) to skip warnings.

