From c87c33f7dda82eb91ee8ba9504f749101bb367d6 Mon Sep 17 00:00:00 2001 From: Mykyta Protsenko Date: Tue, 25 Nov 2025 19:57:25 -0800 Subject: [PATCH] perf: add index to improve the GetWorkspaceAgentByInstanceID query performance (#20936) ## Context GetWorkspaceAgentByInstanceID has a suboptimal plan. Even though it is designed to fetch a small subset of records, there are no corresponding indexes and that query results in full table scan: Query: ``` SELECT id, auth_instance_id FROM workspace_agents where auth_instance_id='i-013c2b96b6441648a' and deleted=FALSE; ``` Plan: ``` ------------------------------------------------------------------------------------------------------------------ Seq Scan on workspace_agents (cost=0.00..222325.48 rows=2 width=36) (actual time=0.012..234.152 rows=4 loops=1) Filter: ((NOT deleted) AND ((auth_instance_id)::text = 'i-013c2b96b6441648a'::text)) Rows Removed by Filter: 302276 Planning Time: 0.173 ms Execution Time: 234.169 ms ``` After adding the index, the plan improves drastically. Updated plan: ``` Bitmap Heap Scan on workspace_agents (cost=4.44..12.32 rows=2 width=36) (actual time=0.019..0.019 rows=0 loops=1) Recheck Cond: (((auth_instance_id)::text = 'i-013c2b96b6441648a'::text) AND (NOT deleted)) -> Bitmap Index Scan on workspace_agents_auth_instance_id_deleted_idx (cost=0.00..4.44 rows=2 width=0) (actual time=0.013..0.014 rows=0 loops=1) Index Cond: (((auth_instance_id)::text = 'i-013c2b96b6441648a'::text) AND (deleted = false)) Planning Time: 0.388 ms Execution Time: 0.044 ms ``` ## Changes * add an index to optimize this query ## Testing * ran the queries manually against prod and test DBs * ran `./scripts/develop.sh`, connected to the local PostgreSQL instance, inspected the indexes to make sure new index is there: ``` Indexes: "workspace_agents_pkey" PRIMARY KEY, btree (id) // NEW INDEX CREATED SUCCESSFULLY [comment is mine] "workspace_agents_auth_instance_id_deleted_idx" btree (auth_instance_id, deleted) "workspace_agents_auth_token_idx" btree (auth_token) "workspace_agents_resource_id_idx" btree (resource_id) ``` --------- Signed-off-by: Danny Kopping Co-authored-by: Danny Kopping --- coderd/database/dump.sql | 2 ++ .../migrations/000401_add_workspace_agents_index.down.sql | 1 + .../migrations/000401_add_workspace_agents_index.up.sql | 1 + 3 files changed, 4 insertions(+) create mode 100644 coderd/database/migrations/000401_add_workspace_agents_index.down.sql create mode 100644 coderd/database/migrations/000401_add_workspace_agents_index.up.sql diff --git a/coderd/database/dump.sql b/coderd/database/dump.sql index 735fc5344a..7c06782459 100644 --- a/coderd/database/dump.sql +++ b/coderd/database/dump.sql @@ -3437,6 +3437,8 @@ CREATE INDEX workspace_agent_stats_template_id_created_at_user_id_idx ON workspa COMMENT ON INDEX workspace_agent_stats_template_id_created_at_user_id_idx IS 'Support index for template insights endpoint to build interval reports faster.'; +CREATE INDEX workspace_agents_auth_instance_id_deleted_idx ON workspace_agents USING btree (auth_instance_id, deleted); + CREATE INDEX workspace_agents_auth_token_idx ON workspace_agents USING btree (auth_token); CREATE INDEX workspace_agents_resource_id_idx ON workspace_agents USING btree (resource_id); diff --git a/coderd/database/migrations/000401_add_workspace_agents_index.down.sql b/coderd/database/migrations/000401_add_workspace_agents_index.down.sql new file mode 100644 index 0000000000..3b2a25345f --- /dev/null +++ b/coderd/database/migrations/000401_add_workspace_agents_index.down.sql @@ -0,0 +1 @@ +DROP INDEX IF EXISTS public.workspace_agents_auth_instance_id_deleted_idx; diff --git a/coderd/database/migrations/000401_add_workspace_agents_index.up.sql b/coderd/database/migrations/000401_add_workspace_agents_index.up.sql new file mode 100644 index 0000000000..db67cb400f --- /dev/null +++ b/coderd/database/migrations/000401_add_workspace_agents_index.up.sql @@ -0,0 +1 @@ +CREATE INDEX IF NOT EXISTS workspace_agents_auth_instance_id_deleted_idx ON public.workspace_agents (auth_instance_id, deleted);