Skip to content

Run the statements every message write costs on asyncpg directly (CHOO-3667) - #1276

Closed
abeldantas wants to merge 1 commit into
asyncpg-delivery-readsfrom
asyncpg-message-write
Closed

abeldantas wants to merge 1 commit into
asyncpg-delivery-readsfrom
asyncpg-message-write

Conversation

@abeldantas

Copy link
Copy Markdown
Collaborator

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:

CPU per message delivery p50 / p99
before 13.8, 13.6 ms 31 / 55 ms
this PR 13.1, 13.2 ms 31 / 54 ms

Stack (merge order)

  1. Make idle controller agents and startup cheap on the database as deployments grow (CHOO-3667) #1200 → Read a controller's agents' heads once per delivery, not three times (CHOO-3667) #1220 → Try a controller's delivery pass again when the pool has no connection, instead of closing its socket (CHOO-3667) #1221 → Take an agent's head from the notification of its new event (CHOO-3667) #1246 → Deliver an agent's event in three statements, not seven (CHOO-3667) #1251 → Store a message's own events together, in two statements (CHOO-3667) #1252 → Have a controller's stream wait on one bell, not one per agent (CHOO-3667) #1253 → Build an agent's event read once per shape, not on every read (CHOO-3667) #1254 → Run the two reads behind every delivery on asyncpg directly (CHOO-3667) #1268
  2. This PR ← you are here

…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.

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.

🟡 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 RawStatement and 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)
@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