Repository navigation
Run the two reads behind every delivery on asyncpg directly (CHOO-3667) - #1268
Closed
abeldantas wants to merge 1 commit into
Closed
abeldantas wants to merge 1 commit into
abeldantas wants to merge 1 commit into
Conversation
Under load SQLAlchemy is about half of switch-core's CPU, most of it per statement rather than per row. A stream reads an agent's events and counts its unread messages for every event it delivers. `db/raw.py` runs a statement SQLAlchemy compiled once on the session's own asyncpg connection, in its transaction and under its tenant, with the same type conversions and query timing; those two reads now use it.
abeldantas
requested review from
agent-runner353,
amaudruz,
christian-mcdermott,
lbangalosbt,
massimo-sandboxaq,
niall-sandboxaq,
petr-sandbox,
raullopez-sandbox and
simonflk-sb
as code owners
November 1, 2026 14:31
Contributor
There was a problem hiding this comment.
🔵 Needs a closer look
The low-level asyncpg delivery path changes transaction, cancellation, pooling, codec, and connection-error behavior in ways that require representative runtime validation beyond static review.
No open issues 🎉
What changed in this PR
This pull request reduces per-delivery overhead by compiling SQLAlchemy statements once and executing the agent-event and unread-count reads directly through asyncpg while preserving tenant scoping, type conversion, transaction behavior, and query timing.
Changes:
- Added
RawStatementfor direct asyncpg execution with SQLAlchemy parameter and result handling. - Migrated event delivery and unread-count reads to the raw execution path.
- Added tenant-drift validation, query timing support, and coverage for transactions, RLS, conversions, and unsupported statements.
- Updated observability helpers, delivery tests, and changelog documentation.
| File | Summary |
|---|---|
core/tests/switch_core/observability/test_query_timing.py |
Updates operation-label tests for the renamed query helper. |
core/tests/switch_core/management/test_stream.py |
Verifies delivery query timing and expected statement counts. |
core/tests/switch_core/db/test_raw.py |
Covers raw execution, transactions, RLS, conversions, timing, tenant validation, and compilation constraints. |
core/switch_core/observability/query.py |
Exposes the query operation-label helper for raw query metrics. |
core/switch_core/db/tenant_session.py |
Exposes tenant-drift validation for direct database reads. |
core/switch_core/db/raw.py |
Implements direct asyncpg execution with parameter/result conversion, tenant validation, and timing. Two moderate findings remain: direct-driver connection failures may bypass transient-failure classification, and literal_execute/other postcompile binds are not currently rejected or rendered. |
core/switch_core/bridges/agent/protocol/event_buffer.py |
Routes event and unread-count delivery reads through cached raw statements. |
CHANGELOG.md |
Documents the asyncpg-backed delivery-read optimization. |
Review effort: Lite
This was referenced Nov 1, 2026
Collaborator
Author
|
Combined into #1307 with the other scaling PRs. |
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.
Under load, SQLAlchemy is about half of switch-core's CPU, and most of that is spent per statement (the execution context, its events, the cursor adapter, building rows) rather than in Postgres. A controller's stream runs two reads for every event it delivers: the agent's events, and its unread count.
In this PR we add
db/raw.py: a statement is still written in SQLAlchemy and compiled by it once, then run by asyncpg on the session's own connection, in its transaction and under its tenant, with SQLAlchemy's type conversions and the samedb.query.durationtiming. The two delivery reads use it.Local A/B on two pods, 400 agents behind 20 controllers at 20 msg/s, rooms of 4. Each pair ran back to back, and the pairs up to an hour apart, so compare within a row:
CPU per message, both pods together.
Stack (merge order)