pg-stat-statements-observability
pg_stat_statements Query Observability
- License 许可
- MIT
- 最近更新
- 2026-11-14
- Stars
- ★ 18
- Forks
- 6
01 前言 Front Matter
- name
- pg-stat-statements-observability
- metadata
- { "original-index": 33, "short-description": "pg_stat_statements Query Observability" }
- description
- pg_stat_statements Query Observability
02 资源文件 1
- 参考文档skill.json169 B
03 SKILL.md 正文
pg_stat_statements Query Observability
Source: PostgreSQL Best Practices -- "Server configuration",
What
Enabling and using the pg_stat_statements extension to capture per-query
performance metrics (calls, total time, mean time, rows, shared blocks).
This is the primary tool for identifying slow queries, regression, and
optimization opportunities.
Why
Without query-level metrics:
- Slow queries hide behind aggregate server metrics (CPU, IOPS)
- Index optimization is guesswork -- you cannot tell which queries dominate
- Performance regressions after deployments go undetected
- Capacity planning has no data foundation
pg_stat_statements is lightweight (< 1% overhead), built into PostgreSQL,
and available on Azure Flexible Server as a loadable module.
How
Enable the extension (Azure Flexible Server)
- Azure Portal > Server Parameters:
shared_preload_libraries = pg_stat_statements -- requires restart pg_stat_statements.track = all -- track all statements pg_stat_statements.max = 5000 -- max tracked queries - Restart the server (required for
shared_preload_libraries). - Create the extension in each database:
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
Top 10 queries by total execution time
SELECT
calls,
ROUND(total_exec_time::numeric, 2) AS total_ms,
ROUND(mean_exec_time::numeric, 2) AS mean_ms,
ROUND(min_exec_time::numeric, 2) AS min_ms,
ROUND(max_exec_time::numeric, 2) AS max_ms,
rows,
LEFT(query, 120) AS query_preview
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 10;
Top 10 queries by mean execution time (slow outliers)
SELECT
calls,
ROUND(mean_exec_time::numeric, 2) AS mean_ms,
ROUND(total_exec_time::numeric, 2) AS total_ms,
rows,
LEFT(query, 120) AS query_preview
FROM pg_stat_statements
WHERE calls > 10 -- ignore one-off admin queries
ORDER BY mean_exec_time DESC
LIMIT 10;
Top 10 queries by shared buffer reads (I/O heavy)
SELECT
calls,
shared_blks_read + shared_blks_hit AS total_blks,
ROUND(
100.0 * shared_blks_hit /
NULLIF(shared_blks_read + shared_blks_hit, 0), 2
) AS cache_hit_pct,
ROUND(mean_exec_time::numeric, 2) AS mean_ms,
LEFT(query, 120) AS query_preview
FROM pg_stat_statements
ORDER BY shared_blks_read DESC
LIMIT 10;
Reset statistics (after optimization to re-baseline)
-- Reset ALL query stats (use sparingly)
SELECT pg_stat_statements_reset();
Check if extension is active
SELECT * FROM pg_available_extensions
WHERE name = 'pg_stat_statements';
-- If installed, verify it is tracking
SELECT COUNT(*) AS tracked_queries
FROM pg_stat_statements;
Combine with slow query logging
-- Server parameter (Azure Portal)
log_min_duration_statement = 250 -- log queries > 250ms
-- This gives you two layers:
-- 1. pg_stat_statements: aggregated stats for ALL queries
-- 2. PostgreSQL log: individual slow query events with parameters
Key Metrics to Monitor
| Metric | What It Tells You | Action Threshold |
|---|---|---|
total_exec_time |
Cumulative CPU cost | Top query > 50% of total |
mean_exec_time |
Per-call latency | > 100ms for OLTP queries |
calls |
Query frequency | Top query > 10K calls/hour |
rows |
Rows returned per call | > 10K rows (missing pagination?) |
shared_blks_read |
Disk I/O | High reads = missing index or seq scan |
cache_hit_pct |
Buffer cache effectiveness | < 95% = under-provisioned memory |
When to Use
- After enabling on a new server pattern)
- Before and after index changes to measure impact
- During audit investigations to find optimization targets
- Periodically (weekly/monthly) for capacity planning
Where We Used It
- ****: Enabled
pg_stat_statementson DEV and QA servers - Best Practices: Listed as mandatory server configuration
- Audit TDDs: Query-level observability requirement
Related Skills
- Skill #29 (EXPLAIN ANALYZE) -- detailed plan for individual queries
- Skill #30 (Autovacuum & Bloat) -- bloat causes seq scans, visible here
- Skill #32 (Connection Pooling) -- slow queries identified here, timeouts set there
References
- PostgreSQL Best Practices -- "Server configuration" section
- User Management Audit TDD -- "Query-level observability" requirement
Gotchas
shared_preload_librarieschange requires a server restart on Azure Flexible Server -- coordinate with ops- Statistics persist across connections but are lost on server restart
unless
pg_stat_statements.save = on(default on Azure) pg_stat_statements_reset()clears ALL stats -- there is no per-query reset; use it only after a known baseline change- The
querycolumn normalizes literal values to$1, $2, ...-- you cannot see actual parameter values (by design, for security) pg_stat_statements.max = 5000means the 5001st unique query evicts the least-used entry -- increase if your app has many dynamic queries- On Azure, you may need the
azure_pg_adminrole to create the extension
*Category: Tooling | Origin: *
04 同仓库更多 Skills
yeet
Use only when the user explicitly asks to stage, commit, push, and open a GitHub pull request in one flow using the GitHub CLI (`gh`).
wrangler
Cloudflare Workers CLI for deploying, developing, and managing Workers, KV, R2, D1, Vectorize, Hyperdrive, Workers AI, Containers, Queues, Workflows, Pipelines, and Secrets Store. Load before running wrangler commands to ensure correct syntax and best practices. Biases towards retrieval from Cloudflare docs over pre-trained knowledge.
workspace-skill-discovery
Discover and include ALL skills across the workspace — not just global ones. Use at the start of every session or when the user asks about available skills. Ensures project-level skills (under .claude/skills/ or skills/ in any repo) are never missed.
workers-best-practices
Reviews and authors Cloudflare Workers code against production best practices. Load when writing new Workers, reviewing Worker code, configuring wrangler.jsonc, or checking for common Workers anti-patterns (streaming, floating promises, global state, secrets, bindings, observability). Biases towards retrieval from Cloudflare docs over pre-trained knowledge.
winui-app
Create, build and design WinUI 3 desktop apps with C# and the Windows App SDK following Microsoft's official guidance and CommunityToolkit. Covers XAML, navigation, theming, accessibility and deployment.
windows-brain-script-portability
Scripts in ~/.claude/scripts/ that run on a Windows brain: use the python3.cmd shim and not bare python3, and reconfigure stdout to UTF-8 if you print symbols. Use it when writing a brain script or triaging a Windows-only crash.