Skip to content

perf: reuse Postgres pools for connect#141

Merged
joshcramer merged 4 commits into
mainfrom
perf/postgres-shared-connect
Jun 26, 2026
Merged

perf: reuse Postgres pools for connect#141
joshcramer merged 4 commits into
mainfrom
perf/postgres-shared-connect

Conversation

@larimonious

Copy link
Copy Markdown
Contributor

Summary

  • make std/db/postgres.connect(url) reuse a process-local shared pool keyed by connection string
  • keep each connect(url) call returning a fresh logical handle, with transaction state isolated per handle
  • change close(handle) to invalidate only that logical handle while keeping the shared URL pool cached
  • remove the now-stale postgres_connect_in_function lint and update docs/generated stdlib references

App compatibility scan

Scanned Larri-owned ntnt apps/repos under /home/larimonious/apps and /home/larimonious/repos for std/db/postgres imports and aliased calls.

Findings:

  • Most apps use module-scope connect() handles already: larri-net, ntnt-lang-org, TAP, Running Wild, Panamera Hub, site templates, etc.
  • Larri Dashboard has one request-local pg_connect/pg_close path in lib/dashboard_signals.tnt; this PR directly fixes that churn.
  • Benchmark/demo code intentionally does request-local connect → query → close; this now hits the shared pool fast path.
  • Only transaction usages found were ntnt examples / worktree examples; transaction state remains per logical handle.
  • No app relied on close() destroying the underlying URL pool.

Verification

  • cargo build --profile dev-release
  • cargo test -q — 1165 unit tests plus integration suites passing
  • ./target/dev-release/ntnt validate examples/ — all 57 examples valid, existing 6 warnings
  • ./target/dev-release/ntnt lint examples/ — existing warnings/suggestions only
  • Docker Postgres smoke test:
    • 50 repeated connect(url) calls stayed at activity_count=1
    • close(handle) invalidated the logical handle with Invalid or closed database connection
  • git diff --check

@greptile-apps

greptile-apps Bot commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR changes Postgres connections to reuse shared pools while keeping logical handles separate. The main changes are:

  • Shared Postgres pools keyed by normalized connection strings.
  • Per-handle tokens for close and transaction validation.
  • Updated close behavior that invalidates only the logical handle.
  • Removed the stale request-local Postgres connect lint.
  • Updated docs and tests for the new pooling model.

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 shared pool reuse, normalized pool keys, per-handle tokens, and handle validation for close and transaction operations.
src/main.rs Removes the lint that warned about calling Postgres connect inside functions.
tests/type_checker_tests.rs Removes tests for the retired Postgres connect lint.
docs/DEPLOYMENT_GUIDE.md Updates deployment guidance to describe shared Postgres pool reuse.
docs/RUNTIME_REFERENCE.md Clarifies the shared Postgres pool behavior for NTNT_DB_POOL_SIZE.
docs/STDLIB_REFERENCE.md Updates the Postgres stdlib reference for shared pools and logical handle close semantics.
docs/runtime.toml Updates runtime metadata for the revised Postgres pool behavior.

Reviews (4): Last reviewed commit: "fix: enforce Postgres handle tokens" | Re-trigger Greptile

Comment thread src/stdlib/postgres.rs Outdated
Comment thread src/stdlib/postgres.rs Outdated
Comment thread src/stdlib/postgres.rs Outdated
Comment thread src/stdlib/postgres.rs
Comment thread src/stdlib/postgres.rs Outdated
Comment thread src/stdlib/postgres.rs
Comment thread src/stdlib/postgres.rs Outdated
@joshcramer joshcramer self-assigned this Jun 26, 2026
@joshcramer joshcramer merged commit 22f35ed 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