用这个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、文件用途

文件
用途
db_config.env
定义 PostgreSQL 连接环境变量
run_postgres_check.sh
封装 30+ 个 SQL 查询,每个对应一个 skill,返回 JSON
postgres_agent.py
依次调用 skill,分析 JSON 输出,生成 Markdown 报告
SKILL.md
各 skill 的功能说明、用法、输出格式

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 分类)

  1. Core Health & Availability ✅/❌
Skill
Status
Finding
get_invalid_indexes
✅ OK
无无效索引
get_xid_wraparound_risk
✅ OK
XID年龄正常
get_blocking_locks
❌ ERROR
检测到5个阻塞锁
get_deadlock_detection
✅ OK
无死锁
get_critical_settings
✅ OK
关键参数正常

关键问题: truncate test 阻塞了所有对 test 表的查询,已持续1小时16分钟。

  1. Session & Connection Monitoring ✅/⚠️
Skill
Status
Finding
get_long_running_queries
⚠️ WARNING
5个长查询 (最长1小时16分)
get_idle_in_transaction_sessions
⚠️ WARNING
1个idle in transaction (1小时16分)
get_long_running_transactions
⚠️ WARNING
存在长时间运行事务
get_long_running_prepared_transactions
✅ OK
无预备事务
get_connection_usage
✅ OK
8/100 连接 (8%)
get_lock_waiters
⚠️ WARNING
20个锁等待者
get_wait_events
⚠️ WARNING
5个 relation 锁等待
  1. Performance & Activity ⚠️
Skill
Status
Finding
建议值
get_cache_hit_rate
⚠️ WARNING
54.8%
>99%
get_rollback_rate
✅ OK
正常
<5%
get_top_sql_by_time
ℹ️ INFO
pgbench更新语句耗时最长
-
get_table_hotspots
ℹ️ INFO
pgbench_accounts DML最多
-
get_bgwriter_stats
⚠️ WARNING
maxwritten_clean=13,844
<100
get_temp_file_usage
✅ OK
4个临时文件,191MB
-
get_io_statistics
ℹ️ INFO
总块读写正常
-
get_io_statistics_v2
ℹ️ INFO
client backend I/O详情
-
get_analyze_progress
✅ OK
无运行中的ANALYZE
-
get_create_index_progress
✅ OK
无运行中的CREATE INDEX
-
get_cluster_progress
✅ OK
无运行中的CLUSTER
-
get_wal_statistics
⚠️ WARNING
wal_buffers_full=113,935
<1000
get_checkpointer_stats
ℹ️ INFO
定时检查点=0, 请求检查点=0
-
get_slru_stats
ℹ️ INFO
transaction SLRU命中率97.85%
>90%
get_user_function_stats
ℹ️ INFO
track_functions未开启
-
  1. Replication & Archiving ✅
Skill
Status
Finding
get_replication_slots
✅ OK
无复制槽
get_replication_status
✅ OK
无活跃副本
get_logical_replication_status
✅ OK
无逻辑复制
get_wal_archiver_status
✅ OK
归档正常
  1. Maintenance & Storage ⚠️
Skill
Status
Finding
浪费空间
get_autovacuum_status
✅ OK
无运行中的autovacuum
-
get_table_bloat
❌ ERROR
pgbench_accounts 88.6% 膨胀
2.03 GB
get_index_bloat
❌ ERROR
pgbench_tellers_pkey 99.94% 膨胀
31.77 MB
get_top_objects_by_size
ℹ️ INFO
pgbench_a…
— 以上为试看内容(约 40%),开通会员后阅读全文 —