pg_microbench
pg_microbench · Perf stat-like hardware performance counters for PostgreSQL planner, executor, utility, and SPI query phases using Linux perf_event_open
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 100439 | pg_microbench | pg_microbench | 1.0 | STAT | MIT | C |
Usage
Sources:
pg_microbench instruments a Linux PostgreSQL backend with perf_event_open counters. It can measure planner, executor, utility, or explicit global SPI scopes and prints hardware-counter results as NOTICE messages for development experiments.
Core Workflow
The operating system must permit the PostgreSQL account to open the required performance events. Prefer a narrowly scoped capability or security policy on an isolated host rather than weakening the host-wide perf policy.
pg_microbench.enable opens or closes the per-backend perf file descriptors. pg_microbench.log controls notice output, and pg_microbench.scopes accepts a comma-separated subset of global, planner, executor, and utility. Reported fields include wall time, cycles, instructions, cache references and misses, L1 data and instruction events, branches, and branch misses.
Benchmark Boundaries
The counters are session-local and the output is not persisted. Unsupported or restricted hardware events may be unavailable, and measurements vary with the CPU, kernel, PostgreSQL build, cache state, scheduler, and concurrent workload. Warm up the query, repeat trials, record the environment, and compare distributions rather than treating one notice as a guarantee.
pg_microbench_run(text) executes its SQL string synchronously with the caller’s privileges and can contain writes or expensive work. Restrict it when only benchmark administrators should initiate such workload. Enabling hook scopes also adds measurement overhead to matching queries in that session. The pinned source explicitly targets Linux and PostgreSQL development master as of January 2026, so build against and test the exact server source; no broad released-major compatibility is promised. No preload is required because installing the extension loads the library in the session.