pgtriage
Read-only PostgreSQL performance auditing via MCP. Collects metrics from PostgreSQL system views, applies deterministic checks for index, query, vacuum, configuration, and connection issues, and returns structured findings for MCP clients.
README
pgtriage
MCP server for PostgreSQL performance auditing. Connect it to Claude Code (or any MCP client) and say "audit my database" to get actionable performance findings with exact fixes.
Not related to the pgAudit logging extension. pgtriage does performance triage, not compliance logging.
<!-- mcp-name: io.github.pgtriage/pgtriage -->
Why I built this
Built after diagnosing implicit type casts and missing indexes on multi-million-row tables in production fintech systems. The fixes were simple (one CREATE INDEX CONCURRENTLY statement each), but finding them required reading query plans most engineers never look at. pgtriage automates that diagnostic process and lets any AI client explain the results.
How it works
Any MCP Client (Claude Code / Cursor / Windsurf / VS Code)
| MCP (stdio)
v
pgtriage (data collection + pattern detection)
| psycopg3 (read-only)
v
PostgreSQL database
pgtriage connects to your PostgreSQL database and exposes performance auditing tools via the Model Context Protocol. It collects metrics from PostgreSQL system views, runs deterministic pattern detection, and returns structured findings. The MCP client provides the AI layer, interpreting results and explaining fixes in plain English.
No API keys required. No AI costs. No vendor lock-in. The intelligence comes from your MCP client.
Example output
{
"severity": "high",
"category": "connection_pressure",
"detail": "Connection utilization at 104% (104/100). Approaching max_connections limit.",
"suggested_fix": "Consider using a connection pooler (PgBouncer) or increasing max_connections if RAM allows.",
"evidence": {
"total_connections": 104,
"max_connections": 100,
"utilization_pct": 104.0
}
}
{
"severity": "medium",
"category": "duplicate_index",
"table": "account",
"detail": "Duplicate indexes on 'account': 'account_title_reverse_index' (16 kB) and 'account_group_reverse_index' (16 kB). Same column definition. One can be dropped.",
"suggested_fix": "DROP INDEX CONCURRENTLY account_group_reverse_index;"
}
From a real audit: 118 tables scanned, 88 findings, prioritized by severity.
What it finds
- Sequential scans on large tables with missing index suggestions
- Dead tuple buildup and autovacuum health issues
- Unused and duplicate indexes wasting disk and slowing writes
- N+1 query patterns from pg_stat_statements analysis
- Stale table statistics causing bad query plans
- TOAST table bloat from large JSONB/TEXT columns
- Configuration issues (shared_buffers, work_mem, autovacuum tuning)
- Connection pressure approaching max_connections
- Long-running queries holding locks
Quick start
Install
pip install pgtriage
Configure Claude Code
Add to your MCP settings (.claude/settings.json or project settings):
{
"mcpServers": {
"pgtriage": {
"command": "python",
"args": ["-m", "pgtriage"],
"env": {
"PGTRIAGE_CONNECTION_STRING": "postgres://user:pass@localhost:5432/dbname"
}
}
}
}
Recommended: Use a dedicated read-only database role:
CREATE ROLE pgtriage_reader LOGIN PASSWORD 'secure_password';
GRANT pg_read_all_stats TO pgtriage_reader;
GRANT USAGE ON SCHEMA public TO pgtriage_reader;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO pgtriage_reader;
Use
> audit my database
> check table health for the users table
> are there any unused indexes?
> review my PostgreSQL configuration
> find slow queries
Tools
full_audit
Run a comprehensive performance audit covering table health, slow queries, index health, and configuration. Returns all findings sorted by severity.
check_table_health
Analyze dead tuples, autovacuum stats, sequential scan ratios, and TOAST bloat. Optionally filter to a specific table.
analyze_slow_queries
Pull the slowest queries from pg_stat_statements, run EXPLAIN ANALYZE on each, and detect patterns like sequential scans, stale statistics, and N+1 queries.
check_index_health
Find unused indexes (zero scans), duplicate indexes (same column definition), and tables that likely need indexes based on scan patterns.
check_config
Review PostgreSQL settings (shared_buffers, work_mem, autovacuum_vacuum_scale_factor, random_page_cost, etc.) and flag suboptimal values. Checks connection utilization and long-running queries.
Resources
| Resource | Description |
|---|---|
pgtriage://status |
Connection status, PostgreSQL version, loaded extensions |
pgtriage://tables |
All tables with sizes and approximate row counts |
Requirements
- Python 3.11+
- PostgreSQL 12+
pg_stat_statementsextension (recommended for slow query analysis, not required for other tools)- Database user with read access to
pg_stat_*views
Safety
pgtriage is designed for read-only production use and does not issue write SQL. Three independent layers protect database state:
- Session-level read-only:
SET default_transaction_read_only = trueon every connection. PostgreSQL rejects any write attempt at the server level. - Query validation: EXPLAIN ANALYZE only runs on SELECT statements. INSERT, UPDATE, DELETE, DROP, SELECT INTO, SELECT FOR UPDATE, and stacked queries are all rejected before execution.
- Transaction rollback: Every EXPLAIN ANALYZE runs inside an explicit BEGIN/ROLLBACK block with a 10-second
statement_timeout. Transactional database changes are rolled back and long-running queries are canceled. Rollback cannot undo external side effects triggered by database extensions or functions, so the dedicated read-only role and session-level read-only enforcement remain essential.
Additionally:
- Connection strings are never exposed in tool outputs
- Suggested fixes are advisory and are never executed by pgtriage; findings default to
safe_to_apply: false - All database access is single-connection, no pooling
Development
git clone https://github.com/pgtriage/pgtriage.git
cd pgtriage
python3 -m venv .venv
source .venv/bin/activate
pip install -e ".[dev]"
pytest
License
MIT
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。