Skip to content

perf: cache Postgres prepared statements#139

Merged
joshcramer merged 2 commits into
mainfrom
perf/db-request-hunt
Jun 26, 2026
Merged

perf: cache Postgres prepared statements#139
joshcramer merged 2 commits into
mainfrom
perf/db-request-hunt

Conversation

@larimonious

Copy link
Copy Markdown
Contributor

Summary

  • cache Postgres prepared statements via deadpool-postgres prepare_cached() for query, query_one, and execute
  • preserve existing Result-returning error semantics for query/prepare failures
  • keep transaction and pooled-connection paths covered by the same helpers

Benchmark

Local Postgres 16 container, wrk -t4 -c32 -d4s, same temporary ntnt app/fixtures, branch vs origin/main.

Pooled query routes improved roughly 2.0–2.36x; JSON/no-DB routes stayed flat, and per-request connect(...) routes stayed bottlenecked as expected.

Representative results:

  • NTNT_WORKERS=1, /db/pooled-single: 7,548.7 → 15,625.0 rps (2.07x), latency 4.19ms → 2.01ms
  • NTNT_WORKERS=1, /db/pooled-twenty: 428.4 → 1,011.1 rps (2.36x), latency 73.95ms → 31.52ms
  • NTNT_WORKERS=8, /db/pooled-single: 32,003.7 → 62,197.8 rps (1.94x), latency 0.99ms → 0.51ms
  • NTNT_WORKERS=8, /db/pooled-twenty: 2,092.2 → 4,735.9 rps (2.26x), latency 15.26ms → 6.75ms

Verification

  • cargo fmt
  • cargo build --profile dev-release
  • cargo test --lib -q
  • cargo test --test language_features_tests --test type_checker_tests --test cli_tests
  • ./target/dev-release/ntnt docs --generate
  • cargo test -q
  • git diff --check
  • DB benchmark against origin/main and this branch

@greptile-apps

greptile-apps Bot commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR caches prepared statements for pooled Postgres calls. The main changes are:

  • Adds cached helpers for query, query_one, and execute.
  • Keeps explicit transaction calls on the direct query path.
  • Detects stale cached-plan errors, evicts the cached statement, and retries directly.
  • Adds a regression test for result-shape changes after temp table recreation.

Confidence Score: 5/5

This looks safe to merge.

  • No blocking issues found in the changed code.

Important Files Changed

Filename Overview
src/stdlib/postgres.rs Adds cached prepared-statement helpers, transaction-safe direct paths, stale-plan eviction, and regression coverage.

Reviews (2): Last reviewed commit: "fix: recover stale Postgres cached plans" | Re-trigger Greptile

Comment thread src/stdlib/postgres.rs
@joshcramer joshcramer merged commit 0526ee8 into main Jun 26, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants