Commit 59058a1
authored
Add in-process pg_bench benchmarking for pgrx extensions (pgcentralfoundation#2263)
Introduce a new `#[pg_bench]` benchmarking workflow for pgrx extensions,
backed by a new optional pgrx-bench crate and a new cargo pgrx bench
subcommand.
This adds Criterion-driven, in-process benchmarks that run inside a
Postgres backend, using the same managed cluster lifecycle as cargo pgrx
run rather than the ephemeral pg_test harness. Benchmarks are installed
into a managed database named $extname_benches, and each cargo pgrx
bench invocation is recorded as a persistent benchmark group with
environment metadata, git metadata, pg_settings snapshots, and
normalized benchmark results.
Highlights
- add #[pg_bench] proc macro in pgrx-macros
- require benchmarks to live in #[cfg(feature = "pg_bench")]
#[pg_schema] mod benches
- add per-benchmark setup support via #[pg_bench(setup = some_path)]
- add transaction modes:
- shared
- subtransaction_per_batch
- subtransaction_per_iteration
- add new pgrx-bench runtime crate exposing:
- Bencher
- BatchSize
- black_box
- add cargo pgrx bench command
- build/install bench-enabled extensions through the normal install
path, including control files and generated SQL scripts
- refresh the extension in $extname_benches with DROP EXTENSION / CREATE
EXTENSION while preserving runner-owned benchmark history
- store benchmark history in persistent pgrx_bench schema tables and
views
- stream per-benchmark results as each benchmark finishes
- emit Criterion-like CLI output for time/change/slope/mean/median
- persist raw Criterion artifacts and replay them as baselines for later
comparisons
- switch comparison reporting to Criterion-driven change data instead of
host-side approximations
- warn, but do not fail, when pg_bench is explicitly enabled for cargo
pgrx run/install/package
Result storage
Benchmark history is stored outside extension ownership in the
pgrx_bench schema so it survives extension refreshes. Each run group
captures:
- benchmark group name and comparison group
- extension/package/profile metadata
- full command line
- host and toolchain metadata
- git metadata
- pg_settings snapshot
- benchmark case/run/estimate/sample/throughput rows
- raw Criterion JSON artifacts for later comparison replay
Comparison behavior
Comparisons now use persisted Criterion artifacts from a prior benchmark
group. The backend reconstructs Criterion’s base/ layout, reruns the
benchmark, reads Criterion’s change/estimates.json, and mirrors
Criterion 0.5.1’s mixed-bootstrap T-test path for p-value reporting.
This keeps change intervals and status messages aligned with Criterion’s
own model.
Examples, docs, and CI
- add new pgrx-examples/benching example crate
- add example README showing #[pg_bench], setup functions, and
transaction handling
- add PG_BENCH-SPEC.md design/spec document
- expand pgrx-bench README with feature overview, workflow guidance, and
black_box discussion
- update cargo-pgrx README to document cargo pgrx bench
- add CI smoke coverage for the new benching example
- update publish.sh for the new pgrx-bench crate
Tests
- add unit tests in pgrx-bench for:
- Criterion new-vs-base directory selection
- baseline artifact materialization
- artifact collection
- Criterion-style summary classification
- deterministic p-value behavior
- comparison parsing from persisted baseline artifacts
This establishes pg_bench as the in-process benchmarking counterpart to
pg_test for pgrx extensions, with persistent historical storage and
Criterion-compatible comparison reporting.
---
NOTE: 100% AI, but this is a super cool feature. Something I've always
wanted to do but just never had the time. Only took a few hours with
Codex.1 parent f62e785 commit 59058a1
File tree
29 files changed
+7767
-9
lines changed- .github/workflows
- cargo-pgrx
- src
- command
- templates
- pgrx-bench
- src
- pgrx-examples
- benching
- src
- bin
- pgrx-macros/src
29 files changed
+7767
-9
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
279 | 279 | | |
280 | 280 | | |
281 | 281 | | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
282 | 290 | | |
283 | 291 | | |
284 | 292 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | | - | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
| |||
27 | 28 | | |
28 | 29 | | |
29 | 30 | | |
| 31 | + | |
30 | 32 | | |
31 | 33 | | |
32 | 34 | | |
| |||
68 | 70 | | |
69 | 71 | | |
70 | 72 | | |
| 73 | + | |
71 | 74 | | |
72 | 75 | | |
73 | 76 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
| 56 | + | |
56 | 57 | | |
57 | 58 | | |
58 | 59 | | |
59 | 60 | | |
60 | 61 | | |
61 | 62 | | |
| 63 | + | |
| 64 | + | |
62 | 65 | | |
63 | 66 | | |
64 | 67 | | |
| |||
0 commit comments