Repository navigation
perf(coderd/database): batch chat authz fetch in GetChatDiffStatusesByChatIDs - #31880
Conversation
…yChatIDs Replace the per-chat GetChatByID authorization loop with a single GetChatsByIDs query, so listing chats no longer issues one round trip per chat.
|
@codex security review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
🛡️ Codex Security ReviewSecurity review completed. No security issues were found in this pull request. Reviewed commit: Only the user who started this review can view the report in Codex. ℹ️ About Codex security reviews in GitHubThis is an experimental Codex feature. Security reviews are triggered when:
Once complete, Codex will leave suggestions, or a comment if no findings are found. |
| require.NoError(t, rec.AllAsserted(), "should only be 1 rbac call") | ||
| } | ||
|
|
||
| func TestGetChatDiffStatusesByChatIDsBatchAuthz(t *testing.T) { |
There was a problem hiding this comment.
How come there's a completely new test rather than updating existing ones? Didn't we have tests for GetChatDiffStatusesByChatIDs prior?
There was a problem hiding this comment.
there is one test case in here that required a new test setup as opposed to the single setup in the existing test to test auth for a user who could access one of the requested chat IDs but not another, however there was a test case in here that could go in the existing test
| chats.id ASC | ||
| LIMIT @limit_count::int; | ||
|
|
||
| -- name: GetChatsByIDs :many |
There was a problem hiding this comment.
Note: This duplicates GetChatsByIDsForRunnerSync, sans the sort. Not blocking this PR, but noting this needs a cleanup.
There was a problem hiding this comment.
I'll open a 2nd PR to fix this 👍
GET /api/v2/chatsauthorizes diff status lookups through the functiondbauthz.GetChatDiffStatusesByChatIDs, which currently callsGetChatByIDonce per chat on the page (plus embedded children). For the agents web UI page (page size 50) that is 50+ extra round trips per list request, and it was the dominant source ofGetChatByIDcalls. In addition to the DB load this can cause, just the network round trips alone cause the agents page to load slowly for users with many chatsThis PR adds a
GetChatsByIDsquery and uses it to fetch all parent chats in one statement, then runs the same per-chat RBAC permissions check against the result. Error behavior is unchanged; any unreadable chat returns not authorized, and any missing ID returnssql.ErrNoRows. The system-restricted fast path is untouched.I ran this branch in a scaletest environment where I'd given a single user 600 agents chats. For the individual
GetChatByIDcalls the agents endpoint has a p99 latency of ~450ms. With the change toGetChatsByIDsthe p99 becomes ~54ms.Additionally an agent in a workspace measured this change against Postgres 13 with 5,000 chats on a single backend, Postgres backend CPU per list request (old N x
GetChatByIDvs new 1 xGetChatsByIDs, both followed byGetChatDiffStatusesByChatIDs):Wall times were measured over localhost, so production savings should be larger since the old path paid network RTT per chat.
Decision log
listChatsalready filters viaGetAuthorizedChats, so the per-chat check is redundant for that caller. Skipping it would rely on every caller pre-authorizing, andcoderd/x/gitsyncalso calls this method, so the check stays in dbauthz and is batched instead.GetChatByIDfor every chat after the first.chats_expandedroot self-join to a hash join over a seq scan ofchats(0.09 ms to 1.4 ms execution in the benchmark). This is a pre-existing property of the view, still roughly 8x cheaper than the old path, and may differ on production-sized tables./proc/<pid>/schedstat(cross-checked with utime+stime from/proc/<pid>/stat), single connection, 3 rounds per case after warm-up; rounds agreed within ~3%.