chore(db): PG 迁移卫生与热表索引覆盖审计(#2039) - #2051
Merged
Merged
Conversation
|
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: Pro Plus 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 |
Co-authored-by: Codex <codex@vectorcontrol.tech>
Co-authored-by: Codex <codex@vectorcontrol.tech>
DeliciousBuding
force-pushed
the
chore/migration-index-audit
branch
from
August 28, 2026 21:06
dec757c to
1bc7725
Compare
This was referenced Aug 30, 2026
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.
Closes #2039
Wave 5 Lane W5-C(数据库):hub-server PG 迁移卫生 + 热表索引覆盖审计。静态比对
hub-server/internal/repository/全部查询与hub-server/migrations/全部索引,处置 5 个确定性缺失 + 1 个确定性冗余(迁移 0068),并用真实 PG16 做了 up 路径与执行计划验证。一、迁移卫生审计(结论:健康,无需修复)
0001..0067连续无缺号,全部匹配^\d{4}_[a-z0-9_]+\.(up|down)\.sql$Get-ChildItem+ 正则全量校验)RunMigrations(file://migrations);健康检查VerifyMigrationshub-server/internal/repository/migrate.go:16,27;cmd/server-hub/main.go:44;internal/handler/health.go:139RunMigrationsFrom(file://../../migrations)hub-server/tests/integration/setup_test.go:117WORKDIR /app+COPY migrations ./migrations,与file://migrations相对路径一致hub-server/deployments/Dockerfileschema_migrations,每版本只执行一次)保证;0040 起文件级防御性IF NOT EXISTS/DO $$加固,早期初始迁移无防护但语义上只跑一次,符合 golang-migrate 契约0041_ensure_performance_indexes、0057_devices_primary_key为防护风格代表postgres:16-alpine+redis:7-alpine;AGENTHUB_PG_MIGRATION_TEST_ADMIN_URL驱动migration_audit_test.go建独立库跑真实 up 路径.github/workflows/checks.yml:713,756;hub-server/internal/repository/migration_audit_test.go:226二、索引覆盖矩阵(热表)
图例:✅=覆盖;❌=确定性缺失(0068 已修);🟡=存疑(建议真实负载观察,不臆造修复)
agent_team_events.go:78(MAX(seq) 追加)team_run_id =uq_agent_team_events_run_seq (team_run_id, seq)agent_team_events.go:104ListTeamEventsByRunteam_run_id =ORDERseq ASCagent_team_usage.go:73CountTeamRouteDecisionsteam_run_id = AND type =+ jsonb 表达式audit.go:57链尾查询created_at DESC, id DESCLIMIT 1idx_audit_events_created (created_at DESC)audit.go:92-107ListAuditEventsuser_id/event_type/severity/created_at单维 + keyset(维度, created_at DESC)复合prev_hash =session.go:69,84,99List/SearchSessionsworkspace_id =idx_sessions_workspace_idsm.pinned DESC, COALESCE(...)跨表表达式排序无法索引,LIMIT 500 可接受session_member.go全部session_id =前置(session_id, member_type, member_id)、(session_id, left_at)session.go:69,84,99、message.go:248、message_attachment.go:24member_id = AND left_at IS NULL(member_type, member_id)——member_id 非前缀session.go:70,85,100member_count 子查询GROUP BY session_idmessage.go:38-58列表/增量/去重session_id+seq_id范围/client_msg_idsession_seq、UNIQUEsession_client_msgmessage.go:202-263搜索session_id+ 全文/tsvectorrecalled=false)agent_team_runs.go:48,54session_id/team_idORDERcreated_at DESCsession_created、team_created(0062)agent_team_tasks.go:29、agent_team_assignments.go:19、agent_team_artifacts.go:22team_run_id =agent_team_teams.go:26,33ListTeamsByOwner/ReadableByUserowner_id =ORDERcreated_at DESCagent.go:226FindActivePendingTaskByAgentInstance(#1430 触发门禁)、agent.go:215CancelTasksByAgentInstanceagent_instance_id = AND status INORDERcreated_at DESCagent.go:209ScanExpiredTasks(查询体 :211)expire_at < AND status INstatus_expire (status, expire_at)agent.go:74ListCustomAgentsByOwner、agent_team_teams.go:36joinowner_user_id = AND deleted_at IS NULLfriendship.go:76ListPendingRequests(收到方向)friend_id = AND status =三、0068 处置内容与真实执行计划证据
hub-server/migrations/0068_index_coverage_fixes.up.sql(down 已验证可回滚):DROP idx_audit_events_prev_hash—— 与 0061 的idx_audit_events_prev_hash_unique同列同方法,唯一索引严格更强;与 0065 清理的三个冗余同型,保留只浪费 append 热表写放大。CREATE idx_session_members_member_active ON session_members(member_id) WHERE left_at IS NULLCREATE idx_agent_teams_owner_created ON agent_teams(owner_id, created_at DESC)CREATE idx_pending_agent_tasks_instance_created ON pending_agent_tasks(agent_instance_id, created_at DESC)CREATE idx_custom_agents_owner ON custom_agents(owner_user_id) WHERE deleted_at IS NULLCREATE idx_friendships_friend_status ON friendships(friend_id, status)EXPLAIN (ANALYZE, BUFFERS) 前后对比(真实
postgres:16-alpine16.15,播种 2 万行 session_members / 2 万行 pending_agent_tasks / 3 千行 agent_teams / 3 千行 custom_agents):(member_type, member_id),cost 422,136 buffersleft_at IS NULL进索引条件idx_agent_teams_owner_created,15 行精确四、验证记录(明卷)
cd hub-server && go test ./... -short -count=1全绿(40 包,含 repository/handler/service)go vet ./...干净(exit 0)TestMigration0068IndexCoverageFixes静态审计测试(migration_audit_test.go,沿用既有 readMigration/normalizeSQL 约定)通过AGENTHUB_PG_MIGRATION_TEST_ADMIN_URL驱动的TestMigration*全绿(含全 68 迁移从零应用到空库)go test -tags integration ./tests/integration/(PG16 + Redis7 真实容器)ok 48.677s—— 证明 0068 up 路径不破坏现有测试git diff --check干净证据边界
postgres:16-alpine(16.15)合成数据(规模 2×10⁴ 行级),非生产负载;🟡 存疑项明确不修,建议上线后用真实负载复测。C:\Program Files\GTK3-Runtime Win64\bin(PATH 第 31 位)的旧 MinGW DLL 抢占(msys64\ucrt64\bin 在第 61 位),导致 cgo 编译STATUS_ENTRYPOINT_NOT_FOUND。本 lane 会话内将C:\msys64\ucrt64\bin前置解决;建议后续把该 PATH 顺序修正为永久修复(影响本机所有 cgo 构建)。五、变更清单
hub-server/migrations/0068_index_coverage_fixes.up.sql(新增)hub-server/migrations/0068_index_coverage_fixes.down.sql(新增)hub-server/internal/repository/migration_audit_test.go(+25 行:0068 静态审计测试)