Skip to content

Run the two reads behind every delivery on asyncpg directly (CHOO-3667) - #1268

Closed
abeldantas wants to merge 1 commit into
read-shapes-oncefrom
asyncpg-delivery-reads
Closed

abeldantas wants to merge 1 commit into
read-shapes-oncefrom
asyncpg-delivery-reads

Conversation

@abeldantas

Copy link
Copy Markdown
Collaborator

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 same db.query.duration timing. 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:

before this PR
pair 1 13.6 ms 13.0 ms
pair 2 13.2 ms 12.4 ms
pair 3 14.7 ms 14.2 ms

CPU per message, both pods together.

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
  2. This PR ← you are here

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.

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

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 RawStatement for 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

@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