Repository navigation
Run the statements every message write costs on asyncpg directly (CHOO-3667) - #1276
Closed
abeldantas wants to merge 1 commit into
Closed
abeldantas wants to merge 1 commit into
abeldantas wants to merge 1 commit into
Conversation
…O-3667) Every message takes its room's lock, reads the room's head, stores its agents' own events in two statements, and reads the room's agent members again under the lock. Through SQLAlchemy, the work around those five statements cost more than Postgres took to run them; they now run on asyncpg directly (`db/raw.py`), as the delivery reads do. The test helper that counts a request's statements counts these too, so the statement-count guards keep working.
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 16:34
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
A moderate active issue remains because failed asyncpg statements are omitted from StatementScope, causing request guards to undercount failed or retried work.
1 open issue
What changed in this PR
This PR extends the existing RawStatement asyncpg path to cover the database work performed for every message write, reducing SQLAlchemy execution overhead while preserving transaction, tenant, and statement-count behavior.
Changes:
- Runs room locking, room-head reads, agent-member reads, and batched own-event storage through asyncpg.
- Adds SQL visibility to
RawStatementand updates statement-count instrumentation and failure tests. - Documents the performance optimization in the changelog.
| File | Description |
|---|---|
| core/tests/switch_core/statement_counts.py | Updated as part of this pull request. |
| core/tests/switch_core/delivery/test_router.py | Updated as part of this pull request. |
| core/switch_core/delivery/router.py | Updated as part of this pull request. |
| core/switch_core/db/stores/message_store.py | Updated as part of this pull request. |
| core/switch_core/db/raw.py | Updated as part of this pull request. |
| core/switch_core/bridges/agent/protocol/event_buffer.py | Updated as part of this pull request. |
| CHANGELOG.md | Updated as part of this pull request. |
Review effort: Lite
Comment on lines
+65
to
+69
| rows = await run(statement, session, parameters) | ||
| scope = _current.get() | ||
| if scope is not None: | ||
| scope.statements.append(statement.sql or "") | ||
| scope.rows += len(rows) |
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.

Every message takes its room's lock, reads the room's head, stores its agents' own events (two statements), and reads the room's agent members again under the lock. Through SQLAlchemy, the work around those five statements cost more than Postgres took to run them.
In this PR they run on asyncpg directly, through the
RawStatement#1268 added. The test helper that counts a request's statements now counts these too, so the statement-count guards keep working as before.Local A/B on two pods, 400 agents behind 20 controllers at 20 msg/s, rooms of 4, 150 s windows, two rounds interleaved:
Stack (merge order)