Skip to content

perf(coderd/database): batch chat authz fetch in GetChatDiffStatusesByChatIDs - #31880

Merged
cstyan merged 2 commits into
mainfrom
callum/chats-batch-authz
Nov 12, 2026
Merged

cstyan merged 2 commits into
mainfrom
callum/chats-batch-authz

Conversation

@cstyan

@cstyan cstyan commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

GET /api/v2/chats authorizes diff status lookups through the function dbauthz.GetChatDiffStatusesByChatIDs, which currently calls GetChatByID once 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 of GetChatByID calls. 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 chats

This PR adds a GetChatsByIDs query 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 returns sql.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 GetChatByID calls the agents endpoint has a p99 latency of ~450ms. With the change to GetChatsByIDs the 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 GetChatByID vs new 1 x GetChatsByIDs, both followed by GetChatDiffStatusesByChatIDs):

Chats per request Old pg CPU New pg CPU Old wall New wall
10 2,149 us 342 us 3,044 us 544 us
25 5,180 us 409 us 7,366 us 638 us
50 10,341 us 1,245 us 14,595 us 1,560 us
100 20,471 us 1,203 us 29,062 us 1,636 us

Wall times were measured over localhost, so production savings should be larger since the old path paid network RTT per chat.

Decision log
  • Batch fetch vs skipping the check for this caller. listChats already filters via GetAuthorizedChats, so the per-chat check is redundant for that caller. Skipping it would rely on every caller pre-authorizing, and coderd/x/gitsync also calls this method, so the check stays in dbauthz and is batched instead.
  • perf(coderd): authorize chat calls from a cached chat RBAC object #30887 does not help here. It caches a single chat's RBAC object on the context keyed by chat ID, so a multi-chat list would still issue a GetChatByID for every chat after the first.
  • Plan shift at larger pages. At ~50 IDs the planner switches the chats_expanded root self-join to a hash join over a seq scan of chats (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.
  • Measurement method. Backend CPU read from /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%.

Generated by Coder Agents on behalf of @cstyan.

…yChatIDs

Replace the per-chat GetChatByID authorization loop with a single GetChatsByIDs query, so listing chats no longer issues one round trip per chat.
@cstyan
cstyan temporarily deployed to main November 9, 2026 05:25 — with GitHub Actions Inactive
@cstyan
cstyan marked this pull request as ready for review November 9, 2026 05:29
@cstyan
cstyan temporarily deployed to main November 9, 2026 05:29 — with GitHub Actions Inactive
@cstyan
cstyan requested a review from mafredri November 9, 2026 17:06
@mafredri

Copy link
Copy Markdown
Member

@codex security review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
🔒 Security Review ✅ Completed 2026-11-12T15:59:48.356322Z 1272ccc Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review

Security review completed. No security issues were found in this pull request.

Reviewed commit: 1272ccc679

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

Comment thread coderd/database/dbauthz/dbauthz_test.go Outdated
require.NoError(t, rec.AllAsserted(), "should only be 1 rbac call")
}

func TestGetChatDiffStatusesByChatIDsBatchAuthz(t *testing.T) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come there's a completely new test rather than updating existing ones? Didn't we have tests for GetChatDiffStatusesByChatIDs prior?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This duplicates GetChatsByIDsForRunnerSync, sans the sort. Not blocking this PR, but noting this needs a cleanup.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll open a 2nd PR to fix this 👍

@cstyan
cstyan temporarily deployed to main November 12, 2026 19:55 — with GitHub Actions Inactive
@cstyan
cstyan merged commit c303114 into main Nov 12, 2026
29 checks passed
@cstyan
cstyan deleted the callum/chats-batch-authz branch November 12, 2026 22:14
@cstyan
cstyan temporarily deployed to docs-preview November 12, 2026 22:14 — with GitHub Actions Inactive
@cstyan
cstyan temporarily deployed to main November 12, 2026 22:14 — with GitHub Actions Inactive
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 12, 2026

This branch was previously deployed

2 inactive deployments
docs-preview — 1e593e0b Deployed Nov 12, 2026 by cstyan via cleanup #13
main — 1e593e0b Deployed Nov 12, 2026 by cstyan via cla #255331
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants