Repository navigation
Build the usage insert once, not on every message (CHOO-3667) - #1262
Closed
abeldantas wants to merge 1 commit into
Closed
abeldantas wants to merge 1 commit into
abeldantas wants to merge 1 commit into
Conversation
Every message an agent sends records its usage, and UsageStore.record built its INSERT ... ON CONFLICT statement each time, which cost more than running it. It is built once and run with bind parameters, as the budget standings statement is.
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 05:07
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The review identifies no active issues, with the change focused on reusing a parameterized usage upsert while preserving the existing aggregation and conflict-update behavior.
No open issues 🎉
What changed in this PR
This pull request improves usage-recording performance by constructing the PostgreSQL upsert once and reusing it with bind parameters, while preserving hourly aggregation and conflict-update behavior.
Changes:
- Adds a reusable, parameterized usage upsert statement.
- Updates
UsageStore.recordto execute the shared statement. - Documents the optimization in the changelog.
| File | Summary |
|---|---|
core/switch_core/db/stores/usage_store.py |
Reuses the module-level usage upsert with runtime bind parameters. A regression test covering statement reuse would be beneficial. |
CHANGELOG.md |
Documents the usage-statement construction optimization. |
This pull request is ready to be approved.
Review effort: Lite
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 an agent sends records its usage, and
UsageStore.recordbuilt itsINSERT ... ON CONFLICTfor each one. In this PR we build it once and run it with bind parameters, as #1250 does for the budget standings.Locally (400 agents, 20 messages a second), this and #1263 take CPU per message from 13.2 to 12.7 ms.