Skip to content

perf: store workspace agent session counts as JSONB - #28126

Draft
EhabY wants to merge 1 commit into
feat/agent-session-counts-protocolfrom
perf/workspace-agent-session-counts-jsonb
Draft

perf: store workspace agent session counts as JSONB#28126
EhabY wants to merge 1 commit into
feat/agent-session-counts-protocolfrom
perf/workspace-agent-session-counts-jsonb

Conversation

@EhabY

@EhabY EhabY commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Replaces the four session_count_* columns on workspace_agent_stats with a sparse session_counts JSONB object, so reporting a count for a new app needs no schema change.

  • Only positive counts are stored, so an idle agent writes {} rather than four zeroes.
  • The template insights covering index no longer carries four bigints in its INCLUDE list.
  • The batcher normalizes and caps outside its lock, still ingests the deprecated fields from 2.10 agents, and marshals one object per stats row.
  • Every reader and rollup moves to JSONB with output column names and API shapes unchanged.
  • GetWorkspaceAgentUsageStats becomes a single scan with a window function instead of three CTEs.
  • GetDeploymentWorkspaceAgentUsageStats selects the latest complete minute per agent and excludes the current partial one.
  • GetTemplateInsightsByTemplate deduplicates 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 EXCLUSIVE on workspace_agent_stats and 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 in template_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:

run the previous Coder version until template usage stats roll up, then retry the upgrade

Empty, idle-only, and expired-only databases upgrade regardless. Coderd logs failed to rollup data when 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, and queries.sql.go come from make gen. Worth a close read: the two migration files, queries/workspaceagentstats.sql, queries/insights.sql, workspacestats/batcher.go, and the guard test in migrations/migrate_test.go.

@EhabY
EhabY force-pushed the perf/workspace-agent-session-counts-jsonb branch from b654c59 to 102d860 Compare August 13, 2026 16:29
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.

1 participant