fix: auto-detect PgBouncer transaction mode and disable prepared statements#284
Closed
garrytan wants to merge 1 commit into
Closed
fix: auto-detect PgBouncer transaction mode and disable prepared statements#284garrytan wants to merge 1 commit into
garrytan wants to merge 1 commit into
Conversation
…ements (#260) When the connection URL targets port 6543 (Supabase PgBouncer convention), gbrain now automatically sets prepare=false on the postgres.js connection. This prevents 'prepared statement does not exist' errors that silently drop pages during sync when multiple gbrain processes (autopilot, jobs worker, sync) compete for pooled connections. Precedence chain: 1. GBRAIN_PREPARE env var (explicit override) 2. ?prepare= query param in URL 3. Auto-detect: port 6543 → disable 4. Default: postgres.js default (enabled) Also adds: - Doctor check: warns if port 6543 detected with prepare still enabled - resolvePrepare() exported for testability - 11 unit tests covering all precedence paths Root cause: PgBouncer in transaction mode multiplexes clients across backend connections. Prepared statements are connection-scoped in Postgres, so a statement prepared on connection A fails when routed to connection B. Discovered during batch enrichment of 4,500+ brain pages where sync silently skipped pages with 'prepared statement ql7ganp7eyh25 does not exist'. Co-Authored-By: Wintermute <wintermute@openclaw.ai>
garrytan
added a commit
that referenced
this pull request
Apr 22, 2026
Without this, \`gbrain jobs work\` against a Supabase pooler URL hits "prepared statement does not exist" under load even after the module singleton was fixed in db.ts. Community PR #270 (@notjbg) caught this second path that #284 had missed. Reuses the shared helper, no regex duplication. Co-Authored-By: Jonah Berg <jonah.berg.g@gmail.com> Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
garrytan
added a commit
that referenced
this pull request
Apr 22, 2026
- test/resolve-prepare.test.ts: 11 cases covering env override, URL query param, port auto-detect, malformed URLs, postgres:// scheme, URL-encoded credentials. Uses bun:test — #284's original vitest file would never have run in this project. - test/postgres-engine.test.ts: new source-level grep case asserting the worker-pool connect() branch calls db.resolvePrepare(url) and includes a typeof prepare === 'boolean' check. Mirrors the existing SET LOCAL regression guard. If anyone rips out the wiring, the build fails before shipping starts dropping rows. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
13 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Auto-detect Supabase PgBouncer (port 6543) and disable prepared statements. See commit message for details. 11 tests. Doctor check included. Do NOT merge — Garry will review and fix.