Skip to content

Make every transaction's tenant binding and pool checkout cheaper on the server (CHOO-3667) - #1224

Closed
abeldantas wants to merge 1 commit into
mainfrom
cheaper-pool-checkout
Closed

abeldantas wants to merge 1 commit into
mainfrom
cheaper-pool-checkout

Conversation

@abeldantas

Copy link
Copy Markdown
Collaborator

The same profile put the tenant binding at the start of every transaction at about 2% of a pod's CPU, and the pool's hold timer at about 0.5%.

In this PR we:

  • send the tenant binding to the driver as it is, rather than building and compiling a text() statement for every transaction; the same statement in the same transaction
  • keep each function's label in the hold timer, stop walking the stack once it has the frames it shows, and write the stack out only for a hold long enough to log

…the server (CHOO-3667)

A CPU profile under message load put the tenant binding that starts each
transaction at about 2% of a pod's CPU and the pool's hold timer at about
0.5%. The binding built and compiled a text() statement every time; it
now goes to the driver as it is, the same statement in the same
transaction. The hold timer formatted a stack of strings on every
checkout; it now keeps each function's label, stops walking once it has
the frames it shows, and writes the stack out only for a long hold.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Approval is deferred because the security-critical tenant hook now emits asyncpg-specific $1 SQL on every transaction, requiring a real Postgres/asyncpg integration run plus statement-count and hold-timer validation.

No open issues 🎉

What changed in this PR

This pull request reduces server overhead from per-transaction tenant binding and connection-pool hold tracking while preserving tenant isolation and long-hold diagnostics.

Changes:

  • Uses a reusable driver-level statement for tenant binding.
  • Caches pool borrower labels and limits stack traversal.
  • Defers stack formatting until long-hold warnings are emitted.
  • Updates the related test fixture and changelog.
File Description
core/​tests/​switch_core/​observability/​test_pool_holds.py Updates _Hold construction for deferred stack data.
core/​switch_core/​observability/​pool.py Optimizes caller detection, frame walking, label caching, and deferred stack formatting.
core/​switch_core/​db/​tenant_session.py Uses a constant driver-level SQL statement for transaction tenant binding.
CHANGELOG.md Documents the transaction and pool-checkout performance improvements.

Review effort: Lite

@abeldantas

Copy link
Copy Markdown
Collaborator Author

Combined into #1307 with the other scaling PRs.

@abeldantas abeldantas closed this Nov 2, 2026
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