Skip to content

Conversation

@mafredri
Copy link
Member

@mafredri mafredri commented Dec 4, 2025

The queries GetWorkspaceAppStatusesByAppIDs and GetLatestWorkspaceAppStatusByAppID both filter by app_id but there was no index to support this, causing sequential scans.

Each query took ~30ms on average with 80 requests/second to the cluster, resulting in ~5.2 query-seconds every second.

Use a composite index on (app_id, created_at DESC) to support both queries efficiently, and add ORDER BY to GetWorkspaceAppStatusesByAppIDs for consistent ordering (newest first).

NOTE: This migration creates an index on workspace_app_statuses. For deployments with heavy task usage, this may take a moment to complete. The concern is minor since tasks are a new feature and most tables will be small.

Refs https://www.notion.so/coderhq/2k-Task-Status-Updates-retest-2bfd579be59280ca8f86eb376d05e0fa

@@ -0,0 +1 @@
CREATE INDEX workspace_app_statuses_app_id_idx ON workspace_app_statuses (app_id);
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we're often looking for the latest status, it would help to make this (app_id, created_at)

Copy link
Member Author

Choose a reason for hiding this comment

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

In the case of GetWorkspaceAppStatusesByAppIDs we're only filtering on appID and not even sorting, this may make PostgreSQL ditch the index.

Sorting might make sense for that query, but will of course add its own overhead. WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

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

I added the sorting to GetWorkspaceAppStatusesByAppIDs as well. Given that the index is already sorted, the overhead should be minimal.

Copy link
Contributor

Choose a reason for hiding this comment

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

Field order matters. PG should be able to use a (app_id, ...) index for WHERE app_id = clauses no matter what else is in the index because the tuples are sorted left to right.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes true, but the query optimizer can also opt not to use it due to higher specificity/perceived cost.

@spikecurtis
Copy link
Contributor

If there are any heavy users of task statuses with a large table this could be a potentially expensive migration. We need some way to call it out in the release notes, at minimum. cc @bpmct @david-fraley

The queries GetWorkspaceAppStatusesByAppIDs and
GetLatestWorkspaceAppStatusByAppID both filter by app_id but there
was no index to support this, causing sequential scans.

Use a composite index on (app_id, created_at DESC) to support both
queries efficiently, and add ORDER BY to GetWorkspaceAppStatusesByAppIDs
for consistent ordering (newest first).

Refs https://www.notion.so/coderhq/2k-Task-Status-Updates-retest-2bfd579be59280ca8f86eb376d05e0fa
@mafredri mafredri force-pushed the mafredri/add-workspace-app-statuses-app-id-index branch from 836370c to 821a4f4 Compare December 4, 2025 12:37
@mafredri
Copy link
Member Author

mafredri commented Dec 4, 2025

If there are any heavy users of task statuses with a large table this could be a potentially expensive migration. We need some way to call it out in the release notes, at minimum. cc @bpmct @david-fraley

Added a note to the description 👍🏻

@mafredri mafredri changed the title fix(coderd/database): add index on workspace_app_statuses.app_id perf(coderd/database): add index on workspace_app_statuses.app_id Dec 4, 2025
@mafredri mafredri merged commit cfdd4a9 into main Dec 4, 2025
38 checks passed
@mafredri mafredri deleted the mafredri/add-workspace-app-statuses-app-id-index branch December 4, 2025 15:56
@github-actions github-actions bot locked and limited conversation to collaborators Dec 4, 2025
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.

3 participants