Skip to content

Survive a multiplexing connection pooler - #86

Merged
pulkitxm merged 2 commits into
mainfrom
fix/supabase-connection-pooling
Jul 30, 2026
Merged

Survive a multiplexing connection pooler#86
pulkitxm merged 2 commits into
mainfrom
fix/supabase-connection-pooling

Conversation

@pulkitxm

Copy link
Copy Markdown
Member

Memoise the database pool in production and disable named prepared statements behind a transaction pooler.

Serverless instances each opened their own pool of ten connections while
Supabase allows fifteen in session mode, so page loads failed with
EMAXCONNSESSION once a couple of instances were warm. Two causes.

The pool was only memoised on globalThis outside production, so every
bundle that evaluated this module built another pool. Route handlers and
the server renderer are separate bundles, so one instance held two pools.
Memoise unconditionally.

A transaction pooler multiplexes many clients onto one backend
connection, and Bun names prepared statements after the query text, so
concurrent clients collide on the name. Detect that pooler by its port
and let the driver send unnamed statements instead.
@vercel

vercel Bot commented Jul 30, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
orbit Ready Ready Preview Jul 30, 2026 10:44am

Request Review

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pulkitxm has reached the 50-credit limit for trial accounts. To continue receiving code reviews, upgrade your plan.

@coderabbitai

coderabbitai Bot commented Jul 30, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

You’ve reached a temporary PR review limit under our Fair Usage Limits Policy.

Your recent review volume is higher than typical usage, so adaptive limits are currently applied.

Next review available in: 10 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 8a42afaa-7d51-45b3-905a-c7634a00248e

📥 Commits

Reviewing files that changed from the base of the PR and between 28bb89e and d2d83fb.

📒 Files selected for processing (2)
  • packages/db/src/client.test.ts
  • packages/db/src/client.ts
📝 Walkthrough

Walkthrough

Changes

Database pooler support

Layer / File(s) Summary
Transaction pooler detection and coverage
packages/db/src/client.ts, packages/db/src/client.test.ts
Adds multiplexesConnections to detect port 6543, with tests covering valid, invalid, query-bearing, and misleading URLs.
Pool configuration and global caching
packages/db/src/client.ts
Configures SQL statement preparation from the detected pooler type and assigns the pool to the global cache unconditionally.

Estimated code review effort: 2 (Simple) | ~10 minutes

Sequence Diagram(s)

sequenceDiagram
  participant DATABASE_URL
  participant multiplexesConnections
  participant pool
  DATABASE_URL->>multiplexesConnections: provide connection URL
  multiplexesConnections-->>pool: return transaction-pooler status
  pool->>pool: set prepare option
  pool->>pool: assign globalForDb.orbitPool
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title is clearly related to supporting multiplexing poolers and matches the main change.
Description check ✅ Passed The description matches the PR goal of memoizing the pool and disabling named prepared statements behind a transaction pooler.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/supabase-connection-pooling

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
packages/db/src/client.test.ts (1)

6-40: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Cover pool configuration, not only detection.

These tests verify multiplexesConnections, but not the changed integration: port 6543 must create a pool with prepare: false, other URLs must keep preparation enabled, and the global cache must not reuse incompatible configuration. Extract the pool factory/options if needed to test these contracts without opening a database connection.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/db/src/client.test.ts` around lines 6 - 40, Extend the tests beyond
multiplexesConnections to cover the pool-creation integration: verify port 6543
produces pool options with prepare disabled, while other connection URLs retain
preparation enabled. Also verify the global pool cache does not reuse a pool
created with incompatible preparation settings; extract the pool factory or
options builder if necessary so these contracts can be tested without opening a
database connection.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/db/src/client.ts`:
- Around line 34-42: Update the global pool lookup and assignment around the
orbitPool initialization so caching occurs only in production, preventing
development and test imports from reusing stale connection configuration.
Preserve creating a fresh SQL pool with the current connectionString,
poolMax.data, and multiplexesConnections(connectionString) settings when the
production guard is not active.

---

Nitpick comments:
In `@packages/db/src/client.test.ts`:
- Around line 6-40: Extend the tests beyond multiplexesConnections to cover the
pool-creation integration: verify port 6543 produces pool options with prepare
disabled, while other connection URLs retain preparation enabled. Also verify
the global pool cache does not reuse a pool created with incompatible
preparation settings; extract the pool factory or options builder if necessary
so these contracts can be tested without opening a database connection.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: e285005b-9629-4dad-8429-0bf40888e861

📥 Commits

Reviewing files that changed from the base of the PR and between 13515f3 and 28bb89e.

📒 Files selected for processing (2)
  • packages/db/src/client.test.ts
  • packages/db/src/client.ts

Comment thread packages/db/src/client.ts Outdated
Memoising unconditionally meant a later import could be handed a pool
built from a different url, size, or prepare mode. Key the entry so a pool
is only reused when every option matches, which keeps one pool per
configuration in a next dev process and across the separate route and
renderer bundles in production.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pulkitxm has reached the 50-credit limit for trial accounts. To continue receiving code reviews, upgrade your plan.

@pulkitxm
pulkitxm merged commit a57e3a8 into main Jul 30, 2026
8 checks passed
@pulkitxm
pulkitxm deleted the fix/supabase-connection-pooling branch July 30, 2026 10:50
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.

1 participant