From 7e8fe78ecda15869ff9cbbf2901a7d79429a95a5 Mon Sep 17 00:00:00 2001 From: Muhsin Keloth Date: Tue, 28 Oct 2025 15:12:46 +0530 Subject: [PATCH] perf: Add database index on conversations identifier (#12715) **Problem** Slack webhook processing was failing with 500 errors due to database timeouts. The query `Conversation.where(identifier: params[:event][:thread_ts]).first` was performing full table scans and hitting PostgreSQL statement timeout. **Solution** Added database index on conversations.identifier and account_id. --- .../20251022152158_add_index_to_conversations_identifier.rb | 6 ++++++ db/schema.rb | 3 ++- 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20251022152158_add_index_to_conversations_identifier.rb diff --git a/db/migrate/20251022152158_add_index_to_conversations_identifier.rb b/db/migrate/20251022152158_add_index_to_conversations_identifier.rb new file mode 100644 index 000000000..e7d02b53d --- /dev/null +++ b/db/migrate/20251022152158_add_index_to_conversations_identifier.rb @@ -0,0 +1,6 @@ +class AddIndexToConversationsIdentifier < ActiveRecord::Migration[7.1] + disable_ddl_transaction! + def change + add_index :conversations, [:identifier, :account_id], name: 'index_conversations_on_identifier_and_account_id', algorithm: :concurrently + end +end diff --git a/db/schema.rb b/db/schema.rb index c0d539f6a..022a0101e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2025_10_03_091242) do +ActiveRecord::Schema[7.1].define(version: 2025_10_22_152158) do # These extensions should be enabled to support this database enable_extension "pg_stat_statements" enable_extension "pg_trgm" @@ -676,6 +676,7 @@ ActiveRecord::Schema[7.1].define(version: 2025_10_03_091242) do t.index ["contact_id"], name: "index_conversations_on_contact_id" t.index ["contact_inbox_id"], name: "index_conversations_on_contact_inbox_id" t.index ["first_reply_created_at"], name: "index_conversations_on_first_reply_created_at" + t.index ["identifier", "account_id"], name: "index_conversations_on_identifier_and_account_id" t.index ["inbox_id"], name: "index_conversations_on_inbox_id" t.index ["priority"], name: "index_conversations_on_priority" t.index ["status", "account_id"], name: "index_conversations_on_status_and_account_id"