Skip to content

Store: one connection pool per process for the Postgres backend - #227

Merged
sathish-mg merged 1 commit into
mainfrom
feat/pg-connection-pool
Sep 11, 2026
Merged

sathish-mg merged 1 commit into
mainfrom
feat/pg-connection-pool

Conversation

@gokul08-mg

Copy link
Copy Markdown
Contributor

Closes #181, the third and final increment. #195 made the bootstrap lock
transaction-scoped; #205 schema-qualified every statement so nothing rode
the session's search_path. With that done, nothing about a connection
belonged to a memory any more — but each handle still owned one (plus a
second for telemetry), so a worker holding a thousand memories held a
thousand or two thousand connections against a max_connections sized in the
low hundreds. The documented remedy was a host-side LRU of handles, which is
exactly what Areev Cloud's spec asks to remove.

What changes

  • One pool per DSN per process (PgPool), keyed by the DSN with the
    store's own schema/provision/pool parameters stripped. A handle
    borrows a connection per statement (with_conn), or pins one for a
    transaction (begin → commit/rollback, Drop rolls back a forgotten
    one). An idle handle holds nothing.
  • ?pool=P / $AREEV_PG_POOL, default 8, the DSN winning; callers past
    the cap wait in FIFO order rather than fail. The first open sizes the
    pool; a later open naming another size is reported in open_warnings().
  • Per-connection state. Prepared statements (keyed by translated SQL,
    cleared past 512) and the ANN GUCs live on PooledConn. The handle keeps
    only what it wants: txn_prelude applies it with SET LOCAL after
    BEGIN, align_session reconciles it onto the borrowed connection outside
    one. The bootstrap's search_path is SET LOCAL, so no pooled session
    keeps a schema.
  • Robustness. A dead connection is discarded, never returned; a read
    outside a transaction replays once, a write never. Concurrent
    first-openers converge on the CREATE SCHEMA race (Postgres documents IF
    NOT EXISTS DDL as racy; the shared runtime widened the window at a small
    pool).
  • The telemetry sidecar and read_blob_offline's Postgres door go through
    the same pool. The pool's runtime is a small multi-thread runtime (a
    current-thread one would serialise its block_on callers into a
    process-wide lock); it stays private to the store behind the sync Db seam.

Proof (local Postgres 16 + PgBouncer 1.25.2)

  • Full conformance at the default pool, and six clean runs at ?pool=2
    under parallel load; chaos, stale and pool binaries at ?pool=2.
  • Behind PgBouncer in transaction mode, the suite at both sizes (the one
    skip is a role-auth test needing password forwarding the throwaway
    trust-mode PgBouncer lacks; it passes on direct Postgres).
  • tests/pg_pool.rs: N handles ≤ P connections, counted in
    pg_stat_activity by application_name = areev.
  • memories_in_one_process_share_nothing: two memories served in turn by
    one pool share nothing across grains, recall, text, vectors, meta, blobs.
  • cargo test --workspace 2,732 passed; clippy clean on postgres and
    postgres-tls; default build/clippy clean; repo-stats within tolerance.
  • CI gains a step running the suite through ?pool=2 against the postgres
    service.

Docs moved with the code: docs/deployment-profile.md (connections and
pooler sections rewritten, the LRU advice withdrawn), docs/docker.md,
docs/cookbook.md, the store CLAUDE.md, an ARCHITECTURE.md §10
decision, and the changelog.

… one

DSN — one server, one role, one TLS setting — borrows from a process-wide
pool: a connection per statement, or one pinned for the length of a
transaction and returned on commit or rollback. So N memories in a process
hold at most P connections between them, an idle memory holds none, and the
telemetry sidecar and the broker's blob door ride the same pool rather than
dialling their own.
@gokul08-mg
gokul08-mg requested a review from a team as a code owner September 10, 2026 13:22
@sathish-mg
sathish-mg merged commit 4109c6d into main Sep 11, 2026
22 checks passed
@sathish-mg
sathish-mg deleted the feat/pg-connection-pool branch September 11, 2026 04:40
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.

One connection pool per process across schemas, search_path per transaction

2 participants