From 98104d10fe18b27b8df5b41da2fd891a4e77ff60 Mon Sep 17 00:00:00 2001 From: Michael Suchacz <203725896+ibetitsmike@users.noreply.github.com> Date: Thu, 22 Oct 2026 09:42:22 +0200 Subject: [PATCH] chore(coderd/database): drop the chat debug stale-sweep indexes (#31009) Drops `idx_chat_debug_runs_stale` and `idx_chat_debug_steps_stale`, the partial `updated_at` indexes that migration 000468 added for the `FinalizeStaleChatDebugRows` query. #30995 deleted that query, so nothing reads these indexes, but every debug run and step write still updates them. **Why it is safe:** no remaining query can use them. A partial index on `updated_at WHERE finished_at IS NULL` only serves queries that filter on `finished_at IS NULL`, and the one query that still does, the `locked_run` CTE in `InsertChatDebugStep`, finds the run by primary key. `DeleteOldChatDebugRuns` filters on `updated_at` alone and uses `idx_chat_debug_runs_updated_at` from migration 000487, which stays. The down migration recreates both indexes as 000468 defined them. **Review focus:** the migration number. 000632 follows `main`'s newest migration, 000631 from #31059. If `main` gains another migration first, this one moves to the next free number. Validation: - `make gen` changes only `dump.sql`, which loses the two `CREATE INDEX` lines. The renumber to 000632 leaves it unchanged. - `go test -count=1 ./coderd/database/migrations/` passes on this branch, where `main`'s 000631 and this 000632 both apply. The run includes `TestMigrate/UpDownUp` (every migration up, down and up again) and `TestMigrationChain`. - The pre-commit hook passed. ## Stack This stack deletes the stale debug-run sweep, then simplifies the chatdebug debug-run, stream wrapper and step numbering code, drops the indexes the sweep used, and last deletes a runner debug field that lost its only reader with the heartbeats. The debug-run and stream wrapper refactors each sit on a test-only layer that pins the behavior they keep. 1. #30988 Delete the chat debug run and step heartbeats 2. #30989 Delete `FinalizeStale` and the stale threshold 3. #30995 Delete the `FinalizeStaleChatDebugRows` query 4. #30933 Pin the debug runs that turns, titles and status labels record 5. #30934 Create and finalize debug runs through one helper 6. #30980 Table the stream wrapper tests and cover their status rules 7. #30981 Share one finalize-once lifecycle between the stream wrappers 8. #31007 Number chat debug steps in the database 9. #31008 Delete the process-global step counters 10. This PR: Drop the chat debug stale-sweep indexes 11. #31010 Drop the unused `runnerCtx` from the runner debug turn > Xum, an AI agent, opened this PR for @ibetitsmike and wrote this description. --- coderd/database/dump.sql | 4 ---- .../migrations/000632_drop_chat_debug_stale_indexes.down.sql | 2 ++ .../migrations/000632_drop_chat_debug_stale_indexes.up.sql | 4 ++++ 3 files changed, 6 insertions(+), 4 deletions(-) create mode 100644 coderd/database/migrations/000632_drop_chat_debug_stale_indexes.down.sql create mode 100644 coderd/database/migrations/000632_drop_chat_debug_stale_indexes.up.sql diff --git a/coderd/database/dump.sql b/coderd/database/dump.sql index 7411f7c2abe..1f5afd43d2c 100644 --- a/coderd/database/dump.sql +++ b/coderd/database/dump.sql @@ -5101,8 +5101,6 @@ CREATE INDEX idx_chat_debug_runs_chat_started ON chat_debug_runs USING btree (ch CREATE UNIQUE INDEX idx_chat_debug_runs_id_chat ON chat_debug_runs USING btree (id, chat_id); -CREATE INDEX idx_chat_debug_runs_stale ON chat_debug_runs USING btree (updated_at) WHERE (finished_at IS NULL); - CREATE INDEX idx_chat_debug_runs_updated_at ON chat_debug_runs USING btree (updated_at); CREATE INDEX idx_chat_debug_steps_chat_assistant_msg ON chat_debug_steps USING btree (chat_id, assistant_message_id) WHERE (assistant_message_id IS NOT NULL); @@ -5111,8 +5109,6 @@ CREATE INDEX idx_chat_debug_steps_chat_tip ON chat_debug_steps USING btree (chat CREATE UNIQUE INDEX idx_chat_debug_steps_run_step ON chat_debug_steps USING btree (run_id, step_number); -CREATE INDEX idx_chat_debug_steps_stale ON chat_debug_steps USING btree (updated_at) WHERE (finished_at IS NULL); - CREATE INDEX idx_chat_diff_statuses_pr_title_fts ON chat_diff_statuses USING gin (to_tsvector('simple'::regconfig, pull_request_title)); CREATE INDEX idx_chat_diff_statuses_stale_at ON chat_diff_statuses USING btree (stale_at); diff --git a/coderd/database/migrations/000632_drop_chat_debug_stale_indexes.down.sql b/coderd/database/migrations/000632_drop_chat_debug_stale_indexes.down.sql new file mode 100644 index 00000000000..c3c15f56b9b --- /dev/null +++ b/coderd/database/migrations/000632_drop_chat_debug_stale_indexes.down.sql @@ -0,0 +1,2 @@ +CREATE INDEX idx_chat_debug_runs_stale ON chat_debug_runs(updated_at) WHERE finished_at IS NULL; +CREATE INDEX idx_chat_debug_steps_stale ON chat_debug_steps(updated_at) WHERE finished_at IS NULL; diff --git a/coderd/database/migrations/000632_drop_chat_debug_stale_indexes.up.sql b/coderd/database/migrations/000632_drop_chat_debug_stale_indexes.up.sql new file mode 100644 index 00000000000..20287d5ed2c --- /dev/null +++ b/coderd/database/migrations/000632_drop_chat_debug_stale_indexes.up.sql @@ -0,0 +1,4 @@ +-- These partial indexes served only the FinalizeStaleChatDebugRows query, +-- which was deleted with the stale debug-run sweep. +DROP INDEX idx_chat_debug_runs_stale; +DROP INDEX idx_chat_debug_steps_stale;