为什么需要
出海做产品,Postgres 是标配。但每次出问题都要手翻几十张 pg_stat 视图找根因——监控平台(pganalyze、PMM)部署麻烦、付费墙、独立开发者用不上。pgbot 一个静态二进制,跑完直接给 findings-first 报告:哪些是 CRITICAL、哪些是 WARNING、哪些 GOOD,每个发现都附带背景说明。
读权限基于 pg_monitor 角色 + 会话级 default_transaction_read_only + statement_timeout 三重防护,不是「加了 flag 就不写」而是「物理上不能写」。AI 层(pgbot ask / pgbot explain)只解释 finding 不生成 finding,OpenAI / Gemini 接入可选,本地调用零成本。
每次运行本地存 baseline,从第三次开始告诉你「这个 query 比上次慢了 3.2 倍」、「这个 index 没人用了」。CI 里加一行 pgbot inspect 就能把回归挡在 PR 阶段。
怎么用
一行安装:
curl -fsSL https://pgbot.dev/install | sh
pgbot inspect "postgres://pgbot_ro@host:5432/db"
或用环境变量:
export DATABASE_URL="postgres://pgbot_ro:…@host:5432/db?sslmode=require"
pgbot inspect
输出长这样:
connected · db.example.com · postgres 17.4 · read-only · 6h20m window
Database health: 82/100
CRITICAL
● transaction-id age 1.8B — 84% toward wraparound
WARNING
● orders queries 3.2× slower (8 → 26 ms mean)
● 3 unused indexes consume 18 GB
● connection usage reached 87%
GOOD
● cache hit ratio 99.4%
● replication healthy
● no deadlocks
子命令:indexes、queries、tables、vacuum、ask、explain。--json 输出 schema 化 JSON 喂给 Agent,pgbot mcp 暴露给 Claude Code。
使用案例
出海 SaaS 上线第二天,Stripe webhook 突然延迟,怀疑 Postgres 撑不住。连上 pgbot:
$ pgbot ask "what's wrong?"
Your database is mostly healthy.
1 critical issue: orders queries became 3.2× slower in the last 6 hours.
Likely cause: sequential scans increased after the orders table grew 18%.
Recommended: review an index on customer_id + created_at.
10 秒拿到根因建议,不用 ssh 进生产服务器翻视图。
另一个场景:CI 里加 pgbot inspect --json 抓回归。开发者改了一个 migration,CI 跑 schema diff + pgbot inspect,发现新加的 query 走全表扫描、cache hit ratio 掉了 5%,PR 直接挂红。
注意事项
- 完全开源(Apache-2.0),Postgres 14-18 都支持
- 单二进制,Linux / macOS / Windows 全平台
- 跟 pganalyze / PMM / pgwatch 互补:那些是持续监控平台,pgbot 是即时诊断
pg_stat_statements是 queries 命令的依赖,pgbot 会自动提示缺失时的安装步骤--json是稳定接口(v1.2.0),terminal 输出是 best-effort 不要解析