用这个SKILL分分钟巡检一万个PG,DBA真的要下岗了
一些 PostgreSQL Agent Skill,健康巡检的、经营分析的。
参考了 deepwiki 以及之前写的 《PostgreSQL DBA最常用SQL》《PostgreSQL Freeze 风暴预测》 等文章。
SKILL 代码已放到 https://github.com/digoal/postgres_skill/ 拿走不谢, 以后可基于这个 skill 再进行迭代.
这个以经营分析为目标的 SKILL , 先获取并分析元数据, 采样数据, 生成经营分析相关的SQL和结果分析代码.
# 经营分析 SKILL 使用举例 :
以经营分析的视角, 重新分析这个 postgresql 数据库: host 127.0.0.1 port 1922 user digoal dbname postgres password 123456
以日常巡检的 Skill 为例,介绍如下。
1、PostgreSQL Daily Check 文件结构与用途
postgres-daily-check/
├── assets/
│ └── db_config.env # 数据库连接配置(PGHOST, PGPORT, PGUSER, PGDATABASE)
├── scripts/
│ ├── run_postgres_check.sh # 核心脚本:执行各 skill 的 SQL 查询
│ └── postgres_agent.py # Agent:调度执行、分析结果、生成报告
└── SKILL.md # Skill 定义文档
2、文件用途
3、Skill 用法
1. 独立执行单个 skill
./run_postgres_check.sh <skill_name> [参数]
# 示例
./run_postgres_check.sh get_long_running_queries 5 # 查询超过5分钟的长查询
./run_postgres_check.sh get_idle_in_transaction_sessions 2 # 查询超过2分钟的idle事务
./run_postgres_check.sh get_cache_hit_rate
./run_postgres_check.sh get_table_bloat
2. 完整巡检(推荐)
使用方式
cd postgres-daily-check
# 运行巡检
# 生成 daily_health_report.md
python3 scripts/postgres_agent.py
说明:postgres_agent.py 只使用 Python 标准库(json, subprocess, datetime, os),无需额外依赖。
3. 目前可用 Skill 列表如下
PostgreSQL 巡检报告总结 (按 SKILL.md 分类)
Core Health & Availability ✅/❌
关键问题: truncate test 阻塞了所有对 test 表的查询,已持续1小时16分钟。
Session & Connection Monitoring ✅/⚠️
Performance & Activity ⚠️
Replication & Archiving ✅
Maintenance & Storage ⚠️