Skip to content

perf(messagesearch): pg_trgm GIN 索引让消息搜索 ILIKE 分支可用索引 - #2178

Merged
DeliciousBuding merged 1 commit into
masterfrom
chore/cleanup-round-33
Sep 1, 2026
Merged

perf(messagesearch): pg_trgm GIN 索引让消息搜索 ILIKE 分支可用索引#2178
DeliciousBuding merged 1 commit into
masterfrom
chore/cleanup-round-33

Conversation

@DeliciousBuding

Copy link
Copy Markdown
Collaborator

背景

#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_trgm GIN trigram 部分索引(WHERE recalled = false,与 0042 对齐)。planner 随后对 OR 组做 BitmapOr,两个 GIN 索引同时生效。保留子串匹配语义(对 CJK 文本尤其重要——'simple' tsvector 对中文只会切出整段空白分隔 token,纯词搜索不够)。

守卫幂等:pg_available_extensions 无 pg_trgm 时整块跳过,搜索保持现状(扫描回退),不会部署失败。

实测证据(真实 PG16,50k 行 messages,needle 3 行)

计划 执行时间 buffers
修复前(仅 0042 GIN) Parallel Seq Scan 648.8 ms 1585
修复后(+0071 trgm) BitmapOr(tsvector GIN + trgm GIN) 0.248 ms 27

EXPLAIN ANALYZE 显示修复后两个 Bitmap Index Scan(idx_messages_content_text_tsvector + idx_messages_content_text_trgm)同时命中。

验收

  • 新增审计测试:静态形状断言(守卫/幂等/表达式逐字匹配/部分索引条件)+ 真实 PG 迁移全量 up 后断言扩展与索引存在、ILIKE 冒烟命中(本地 PG16 实跑通过;CI 由 backend-integration PG16-alpine 复跑)。
  • make test(edge+hub)全绿;go vet 通过。
  • verifiers:doc-ssot / ci-gates / i18n-deadkeys / openapi-contract / migration-idempotency + git diff --check 全绿。
  • 既有 message_search_test.go sqlmock SQL 形状断言不受影响(未改 Go 查询)。

未验证 / 备注

  • 生产 PG 需确认 pg_trgm(contrib)可用;不可用时迁移自动跳过,无回归。
  • 纯索引面改动,无 API/契约变化,无需重建前端。
  • 下一轮候选(探索 lane 已核实):typing 回调直查 DB(hub-server/internal/app/events.go:51,复用既有缓存解析器)。

@coderabbitai

coderabbitai Bot commented Sep 1, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Team

Run ID: 9d148719-86f4-4aa3-a6d2-8b24a4820815

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@DeliciousBuding
DeliciousBuding merged commit c85d16d into master Sep 1, 2026
39 checks passed
@DeliciousBuding
DeliciousBuding deleted the chore/cleanup-round-33 branch September 1, 2026 12:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant