File contents db-tweak — PostgreSQL 调优
self-contained skill。覆盖 PG 慢查询优化、索引、DDL 安全、字段/表删除的退场流水线。
🛑 MANDATORY WORKFLOW — check all before declaring done
Phase 0: Baseline (证据先行 — 铁律 1)
Phase 1: 9 铁律预检(铁律 1-9 全过一遍)
Phase 2: 8 模式 sweep
Phase 3: Fix
Phase 4: Verify
Phase 5: Drop retirement (铁律 9)
Phase 6: Document + Share
包含
路径
内容
references/db-tuning.md
9 铁律 + 7 phase + 8 模式 + 工具栈 + 监控指标
scripts/plan-delete.sh
DROP 前 RENAME → PLAN_DELETE_ 流水线
scripts/audit-plan-delete.sh
列所有 pending + DAYS_LEFT + STATUS
scripts/config_drift.py
live config (systemd / crontab / .env) vs git 检测
使用
./scripts/plan-delete.sh --column public.users.legacy_field
./scripts/plan-delete.sh --table public.old_logs
./scripts/plan-delete.sh --index public.idx_unused
./scripts/audit-plan-delete.sh
python scripts/config_drift.py --all --repo /path/to/project
DB 专属铁律
#
铁律
1
证据先行
2
DDL CONCURRENTLY
3
删除三核对
4
名字正则陷阱
5
statement_timeout 30s
6
VACUUM FULL owner 批
7
锁查双时钟
8
DDL 不裸跑
9
删除前先 rename
14 硬约束(跨子工作流通用)
零新增依赖 (YAGNI) : 用自带工具优先,不为假设场景加 pgcli / HypoPG 之外的依赖。
commit 颗粒度 : 1 个 migration = 1 commit。
默认回滚 = RENAME 窗口 (PLAN_DELETE_)。绝对禁止 git reset --hard 。
死代码证明 : 字段/索引删除前必查 pg_depend + 应用代码 grep + ORM migration。
TDD : schema 变更用 Structural(build + test)。
commit 前全量测试全绿 : migration dry-run + 应用回归。
prod 锁定 : prod DDL 走 migration tool + owner 审。不裸 psql -c 。
宁缺勿伪 : 验证靠 EXPLAIN + pg_stat_statements。
DB 删除必走退场流水线 : PLAN_DELETE_ + 7 天 + owner 审。
批量任务先测最小 : 大批 migration 先 sample 1-10 表。
daemon 改动 4 步独立 : 触发器/PL 改后 4 步独立验证。
buffer 所有权 : COPY FROM 客户端 buffer 保留副本。
hash 化产物整目录同步 : 索引 rebuild 多 backend 节点完成才切流量。
部署验证实际生效 : migration apply 后查 pg_indexes / pg_attribute 确认新 schema。
关联
/repo-medic — meta 入口
/py-improve — 慢查询的 Python 调用方代码审查
/doc-reorg — schema 变更后 env.md / deploy.md 同步
/config-base — bootstrap PG 客户端工具(psql / pgcli / migration tool)
仓库
github.com/liyong-labs/repo-medic — Apache-2.0。
1 --- 2 name: db-tweak-2 3 description: db-tweak — PostgreSQL 调优 4 --- 5 6 # db-tweak — PostgreSQL 调优 7 8 self-contained skill。覆盖 PG 慢查询优化、索引、DDL 安全、字段/表删除的退场流水线。 9 10 ## 🛑 MANDATORY WORKFLOW — check all before declaring done 11 12 ### Phase 0: Baseline (证据先行 — 铁律 1) 13 14 - [ ] **Read** `references/db-tuning.md` in full (9 铁律 + 7 phase + 8 模式) 15 - [ ] **抓 top 20 慢查询**: `SELECT * FROM pg_stat_statements ORDER BY mean_exec_time DESC LIMIT 20;` 16 - [ ] **表大小 + 索引 bloat 快照**: 用 `pgstattuple` 量化,存 baseline.json 17 - [ ] **EXPLAIN ANALYZE** 当前慢查询(带 BUFFERS 选项)保存前后对比基线 18 - [ ] 🛑 **GATE**: baseline 文档落盘 (`baseline-<date>.json`) 才能进 Phase 1 19 20 ### Phase 1: 9 铁律预检(铁律 1-9 全过一遍) 21 22 - [ ] **铁律 1**: 动任何东西之前,每条慢查询先跑 `EXPLAIN (ANALYZE, BUFFERS)` 23 - [ ] **铁律 2**: 即将跑的 DDL 是否用 CONCURRENTLY? 24 - [ ] **铁律 3**: DROP 候选引用 / 备份 / RENAME 窗口 3 项都核了? 25 - [ ] **铁律 4**: 标识符全小写下划线?无 PG 保留字? 26 - [ ] **铁律 5**: 所有 session 设 `SET statement_timeout = '30s'`? 27 - [ ] **铁律 6**: 即将 VACUUM FULL?owner 批了? 28 - [ ] **铁律 7**: 当前锁等待 > 5s?查 `pg_stat_activity`? 29 - [ ] **铁律 8**: DDL 走 migration tool(不裸 `psql -c`)? 30 - [ ] **铁律 9**: DROP 走 `plan-delete.sh` 而不是直接 DROP? 31 - [ ] 🛑 **GATE**: 9 条任一未达 = 进 Phase 2 前修正 32 33 ### Phase 2: 8 模式 sweep 34 35 - [ ] **TOAST 进 WHERE**: EXPLAIN 看外联节点 36 - [ ] **TOAST 全表窗口**: 大表 seq_scan >> idx_scan 37 - [ ] **ORDER BY 无索引**: EXPLAIN 中 Sort 节点 38 - [ ] **OFFSET 深分页**: 查 `LIMIT N OFFSET > 10000` 39 - [ ] **统计过期**: `pg_stat_user_tables.n_mod_since_analyze` 大值 40 - [ ] **隐式 cast**: EXPLAIN 中 Cast 节点 41 - [ ] **SELECT ***: 应用代码 grep `SELECT \*` 找候选 42 - [ ] **btree bloat**: `pgstattuple` 量化 43 - [ ] 输出 `sweep-report.md` 列命中项 + 修复建议 44 45 ### Phase 3: Fix 46 47 - [ ] **新索引**: `CREATE INDEX CONCURRENTLY idx_xxx ON tbl (col) WHERE ...` (铁律 2) 48 - [ ] **改 schema 类型**: 走 migration tool,prod 走 maintenance window (铁律 7) 49 - [ ] **关掉 SELECT ***: 应用代码改列名 50 - [ ] **OFFSET → keyset**: WHERE id > last_id LIMIT N 51 - [ ] **REINDEX CONCURRENTLY** (PG 12+) for btree bloat > 30% 52 - [ ] **ANALYZE** for 统计过期 53 - [ ] 🛑 **GATE**: 每个 fix 单独 commit(硬约束 2),commit 后 `EXPLAIN ANALYZE` 复跑确认改进 54 55 ### Phase 4: Verify 56 57 - [ ] **回归测试**: 应用层 pytest/e2e 全绿 58 - [ ] **EXPLAIN 对比**: Phase 0 baseline vs 现在 — p95 latency 应下降 59 - [ ] **buffer hit rate**: Phase 0 vs 现在 — 应 ≥ 99% 60 - [ ] **replication lag**: `pg_stat_replication.replay_lag < 1s` (铁律 7) 61 - [ ] **无新增 lock**: 监控 5 分钟无 `wait_event_type = 'Lock'` 62 - [ ] 🛑 **GATE**: 全部绿 = 可以汇报改进幅度。任何一项退化 = 立即回滚 migration 63 64 ### Phase 5: Drop retirement (铁律 9) 65 66 - [ ] 不适用场景跳过 67 - [ ] **RENAME**: `./scripts/plan-delete.sh --column public.users.legacy_field` 68 - [ ] **7 天观察期**: 监控应用无 `column not found` 错误 69 - [ ] **audit 检查**: `./scripts/audit-plan-delete.sh` 列 DAYS_LEFT 70 - [ ] **owner 显式授权** DROP 71 - [ ] **真 DROP**: 在 owner 授权窗口执行 72 - [ ] 🛑 **GATE**: 7 天观察 + owner 授权两个条件都满足才能 DROP 73 74 ### Phase 6: Document + Share 75 76 - [ ] **写 work-note**: `docs/work-note/<date>-db-tune.md` (Phase 0 baseline + Phase 4 verify 对比) 77 - [ ] **KB 同步**: 推到所配 KB 端点的 public-knowledge 集合(未配 KB 系统则跳过) 78 - [ ] **更新 schema 文档**: 字段/表变更 → env.md / deploy.md 79 80 --- 81 82 ## 包含 83 84 | 路径 | 内容 | 85 |---|---| 86 | `references/db-tuning.md` | 9 铁律 + 7 phase + 8 模式 + 工具栈 + 监控指标 | 87 | `scripts/plan-delete.sh` | DROP 前 RENAME → PLAN_DELETE_ 流水线 | 88 | `scripts/audit-plan-delete.sh` | 列所有 pending + DAYS_LEFT + STATUS | 89 | `scripts/config_drift.py` | live config (systemd / crontab / .env) vs git 检测 | 90 91 ## 使用 92 93 ```bash 94 ./scripts/plan-delete.sh --column public.users.legacy_field 95 ./scripts/plan-delete.sh --table public.old_logs 96 ./scripts/plan-delete.sh --index public.idx_unused 97 ./scripts/audit-plan-delete.sh 98 python scripts/config_drift.py --all --repo /path/to/project 99 ``` 100 101 ## DB 专属铁律 102 103 | # | 铁律 | 104 |---|---| 105 | 1 | 证据先行 | 106 | 2 | DDL CONCURRENTLY | 107 | 3 | 删除三核对 | 108 | 4 | 名字正则陷阱 | 109 | 5 | statement_timeout 30s | 110 | 6 | VACUUM FULL owner 批 | 111 | 7 | 锁查双时钟 | 112 | 8 | DDL 不裸跑 | 113 | 9 | 删除前先 rename | 114 115 ## 14 硬约束(跨子工作流通用) 116 117 1. **零新增依赖 (YAGNI)**: 用自带工具优先,不为假设场景加 `pgcli` / HypoPG 之外的依赖。 118 2. **commit 颗粒度**: 1 个 migration = 1 commit。 119 3. **默认回滚 = RENAME 窗口**(PLAN_DELETE_)。**绝对禁止 `git reset --hard`**。 120 4. **死代码证明**: 字段/索引删除前必查 `pg_depend` + 应用代码 grep + ORM migration。 121 5. **TDD**: schema 变更用 Structural(build + test)。 122 6. **commit 前全量测试全绿**: migration dry-run + 应用回归。 123 7. **prod 锁定**: prod DDL 走 migration tool + owner 审。**不裸 `psql -c`**。 124 8. **宁缺勿伪**: 验证靠 EXPLAIN + pg_stat_statements。 125 9. **DB 删除必走退场流水线**: PLAN_DELETE_ + 7 天 + owner 审。 126 10. **批量任务先测最小**: 大批 migration 先 sample 1-10 表。 127 11. **daemon 改动 4 步独立**: 触发器/PL 改后 4 步独立验证。 128 12. **buffer 所有权**: COPY FROM 客户端 buffer 保留副本。 129 13. **hash 化产物整目录同步**: 索引 rebuild 多 backend 节点完成才切流量。 130 14. **部署验证实际生效**: migration apply 后查 `pg_indexes` / `pg_attribute` 确认新 schema。 131 132 ## 关联 133 134 - `/repo-medic` — meta 入口 135 - `/py-improve` — 慢查询的 Python 调用方代码审查 136 - `/doc-reorg` — schema 变更后 env.md / deploy.md 同步 137 - `/config-base` — bootstrap PG 客户端工具(psql / pgcli / migration tool) 138 139 ## 仓库 140 141 github.com/liyong-labs/repo-medic — Apache-2.0。
liyong-labs/repo-medic/tree/main/docs/zh/db-tweak commit 0ecd368cf1
Frequently asked questions How do I install the DB Tweak skill? Run npx skillmds@latest add liyong-labs/db-tweak-2 in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
What does the DB Tweak skill do? db-tweak — PostgreSQL 调优 It is listed under Coding & Dev Tools on SkillMD.
Is DB Tweak safe to use? This skill has not completed SkillMD's automated safety review yet. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
Which AI agents work with DB Tweak? This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Is DB Tweak free to use? Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
Who published DB Tweak? liyong-labs (@liyong-labs) published this skill. Their other Agent Skills are listed on their SkillMD profile.