{
 "meta": {
  "title": "星图 App 接口超时排障复盘",
  "template": "R02-tech",
  "expires_at": null
 },
 "sections": [
  {
   "id": "s1-现象",
   "heading": "现象",
   "text": "10-12 21:40 起，`/api/orders` p99 从 200ms 飙到 **8.2s**，错误率 6%。"
  },
  {
   "id": "s2-定位",
   "heading": "定位",
   "text": "连接池指标显示 20/20 占满，等待队列持续增长。抓慢查询：\nSELECT * FROM orders\nWHERE user_id = $1 AND status = 'paid'\nORDER BY created_at DESC LIMIT 20;\n-- Seq Scan on orders  (cost=0.00..48211.00)"
  },
  {
   "id": "s3-修复",
   "heading": "修复",
   "text": "加复合索引，池上限提到 50，并给下游调用加 2s 超时。\nCREATE INDEX CONCURRENTLY idx_orders_user_status_created\n  ON orders (user_id, status, created_at DESC);"
  },
  {
   "id": "s4-教训",
   "heading": "教训",
   "text": "- 新查询上线前必须过 `EXPLAIN ANALYZE`\n- 连接池占用率 > 80% 要告警"
  },
  {
   "id": "s5-命令速查",
   "heading": "命令速查",
   "text": "psql -c \"SELECT * FROM pg_stat_activity WHERE state='active'\" 看正在跑的查询\nEXPLAIN (ANALYZE, BUFFERS) <query> 看真实执行计划"
  },
  {
   "id": "s6-参考链接",
   "heading": "参考链接",
   "text": "PostgreSQL 索引文档"
  }
 ],
 "data": {
  "author": "张三",
  "date": "2026-10-13",
  "title": "星图 App 接口超时排障复盘",
  "subtitle": "一次连接池耗尽引发的 P1",
  "summary": "根因是数据库连接池上限 20 被慢查询占满；加索引 + 池扩容 + 超时熔断后 p99 从 8.2s 降到 180ms。",
  "tags": [
   "排障",
   "postgres",
   "性能"
  ],
  "stack": [
   "Python 3.12",
   "FastAPI",
   "PostgreSQL 16",
   "Redis",
   "Cloudflare"
  ],
  "sections": [
   {
    "heading": "现象",
    "body": "10-12 21:40 起，`/api/orders` p99 从 200ms 飙到 **8.2s**，错误率 6%。"
   },
   {
    "heading": "定位",
    "body": "连接池指标显示 20/20 占满，等待队列持续增长。抓慢查询：",
    "code": {
     "lang": "sql",
     "content": "SELECT * FROM orders\nWHERE user_id = $1 AND status = 'paid'\nORDER BY created_at DESC LIMIT 20;\n-- Seq Scan on orders  (cost=0.00..48211.00)"
    }
   },
   {
    "heading": "修复",
    "body": "加复合索引，池上限提到 50，并给下游调用加 2s 超时。",
    "code": {
     "lang": "sql",
     "content": "CREATE INDEX CONCURRENTLY idx_orders_user_status_created\n  ON orders (user_id, status, created_at DESC);"
    },
    "callout": {
     "kind": "warn",
     "text": "生产建索引一定用 `CONCURRENTLY`，否则会锁表。"
    }
   },
   {
    "heading": "教训",
    "body": "- 新查询上线前必须过 `EXPLAIN ANALYZE`\n- 连接池占用率 > 80% 要告警"
   }
  ],
  "commands": [
   {
    "cmd": "psql -c \"SELECT * FROM pg_stat_activity WHERE state='active'\"",
    "desc": "看正在跑的查询"
   },
   {
    "cmd": "EXPLAIN (ANALYZE, BUFFERS) <query>",
    "desc": "看真实执行计划"
   }
  ],
  "links": [
   {
    "label": "PostgreSQL 索引文档",
    "url": "https://www.postgresql.org/docs/current/indexes.html"
   }
  ]
 }
}