perf: store workspace agent session counts as JSONB - #28126
Draft
EhabY wants to merge 1 commit into
Draft
Conversation
This was referenced Aug 13, 2026
EhabY
force-pushed
the
perf/workspace-agent-session-counts-jsonb
branch
from
August 13, 2026 16:29
b654c59 to
102d860
Compare
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.
Replaces the four
session_count_*columns onworkspace_agent_statswith a sparsesession_countsJSONB object, so reporting a count for a new app needs no schema change.{}rather than four zeroes.INCLUDElist.GetWorkspaceAgentUsageStatsbecomes a single scan with a window function instead of three CTEs.GetDeploymentWorkspaceAgentUsageStatsselects the latest complete minute per agent and excludes the current partial one.GetTemplateInsightsByTemplatededuplicates by template, user, and minute before aggregating.Why this is one PR
The migration removes the legacy columns. Splitting this database cutover further would leave either old application code incompatible with the new schema or new queries referencing a column that does not exist.
Migration requirement for operators
The migration takes
ACCESS EXCLUSIVEonworkspace_agent_statsand converts only the retained window,MAX(template_usage_stats.start_time)minus one day, or a 180 day fallback when no rollup exists. Older usage already lives intemplate_usage_stats.Because that window comes from the rollup watermark, converting retained rows with session activity requires a watermark from within the last 24 hours. If it is missing or stale and such rows exist, the migration aborts before any schema change:
Empty, idle-only, and expired-only databases upgrade regardless. Coderd logs
failed to rollup datawhen the watermark stops advancing.Measured on 10M rows with a healthy watermark: about 4.44s on PG17 with tmpfs storage, 6.13s on durable storage.
The down migration restores the four known fields and discards any other key, which the guard test asserts.
Depends on #28125.
dump.sql,models.go,querier.go, andqueries.sql.gocome frommake gen. Worth a close read: the two migration files,queries/workspaceagentstats.sql,queries/insights.sql,workspacestats/batcher.go, and the guard test inmigrations/migrate_test.go.