perf(messagesearch): pg_trgm GIN 索引让消息搜索 ILIKE 分支可用索引 - #2178
Merged
Conversation
Co-authored-by: Cursor <cursor@vectorcontrol.tech>
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Team Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
#2154 perf lane:消息搜索在 PostgreSQL 上生成
to_tsvector(...) @@ plainto_tsquery(...) OR content->>'text' ILIKE '%q%'。0042 建的 GIN tsvector 索引只服务第一个分支;前导通配符 ILIKE 无索引可用 → 整个 OR 组退化为行级过滤,GIN 索引建了也用不上(#2154 原话:OR ILIKE导致 GIN 索引失效)。SearchAllMessages(无 session 过滤)受害更重。修复(零 Go 代码改动)
新迁移 0071:
pg_trgm扩展 +idx_messages_content_text_trgmGIN trigram 部分索引(WHERE recalled = false,与 0042 对齐)。planner 随后对 OR 组做 BitmapOr,两个 GIN 索引同时生效。保留子串匹配语义(对 CJK 文本尤其重要——'simple' tsvector 对中文只会切出整段空白分隔 token,纯词搜索不够)。守卫幂等:
pg_available_extensions无 pg_trgm 时整块跳过,搜索保持现状(扫描回退),不会部署失败。实测证据(真实 PG16,50k 行 messages,needle 3 行)
EXPLAIN ANALYZE显示修复后两个 Bitmap Index Scan(idx_messages_content_text_tsvector+idx_messages_content_text_trgm)同时命中。验收
make test(edge+hub)全绿;go vet通过。git diff --check全绿。message_search_test.gosqlmock SQL 形状断言不受影响(未改 Go 查询)。未验证 / 备注
pg_trgm(contrib)可用;不可用时迁移自动跳过,无回归。