From 9abea171ba06d728890e1d22cc7aea5e9a6fcae6 Mon Sep 17 00:00:00 2001 From: Michael Suchacz <203725896+ibetitsmike@users.noreply.github.com> Date: Fri, 9 Oct 2026 15:39:35 +0000 Subject: [PATCH] perf(coderd/database): index chat message pagination Add a partial (chat_id, id DESC) WHERE deleted = false index on chat_messages so chat history pages can read one chat's live messages in id order. Without it, the planner can choose a chat_messages_pkey scan filtered by chat_id and read every other chat's messages first. --- coderd/database/dump.sql | 2 ++ .../000614_chat_messages_pagination_index.down.sql | 1 + .../migrations/000614_chat_messages_pagination_index.up.sql | 5 +++++ 3 files changed, 8 insertions(+) create mode 100644 coderd/database/migrations/000614_chat_messages_pagination_index.down.sql create mode 100644 coderd/database/migrations/000614_chat_messages_pagination_index.up.sql diff --git a/coderd/database/dump.sql b/coderd/database/dump.sql index 5a1e08737b6..881efcbd4e9 100644 --- a/coderd/database/dump.sql +++ b/coderd/database/dump.sql @@ -5054,6 +5054,8 @@ CREATE INDEX idx_chat_messages_chat ON chat_messages USING btree (chat_id); CREATE INDEX idx_chat_messages_chat_created ON chat_messages USING btree (chat_id, created_at); +CREATE INDEX idx_chat_messages_chat_id ON chat_messages USING btree (chat_id, id DESC) WHERE (deleted = false); + CREATE INDEX idx_chat_messages_chat_role_id ON chat_messages USING btree (chat_id, role, id DESC) WHERE (deleted = false); CREATE INDEX idx_chat_messages_compressed_summary_boundary ON chat_messages USING btree (chat_id, id DESC) WHERE ((compressed = true) AND (deleted = false) AND (visibility = 'model'::chat_message_visibility)); diff --git a/coderd/database/migrations/000614_chat_messages_pagination_index.down.sql b/coderd/database/migrations/000614_chat_messages_pagination_index.down.sql new file mode 100644 index 00000000000..13353739e01 --- /dev/null +++ b/coderd/database/migrations/000614_chat_messages_pagination_index.down.sql @@ -0,0 +1 @@ +DROP INDEX IF EXISTS idx_chat_messages_chat_id; diff --git a/coderd/database/migrations/000614_chat_messages_pagination_index.up.sql b/coderd/database/migrations/000614_chat_messages_pagination_index.up.sql new file mode 100644 index 00000000000..59b086aeb49 --- /dev/null +++ b/coderd/database/migrations/000614_chat_messages_pagination_index.up.sql @@ -0,0 +1,5 @@ +-- Serves chat history pages, which read one chat's messages in id order. Without +-- it the planner can walk chat_messages_pkey through every other chat's rows. +CREATE INDEX idx_chat_messages_chat_id + ON chat_messages (chat_id, id DESC) + WHERE deleted = false;