Provides AI-powered PostgreSQL performance tuning capabilities including query analysis, index recommendations with HypoPG support, execution plan analysis, and database health monitoring for connections, vacuum operations, replication lag, and buffer cache performance.
pgtuner_mcp
A Model Context Protocol (MCP) server that provides AI-powered PostgreSQL performance tuning capabilities. This server helps identify slow queries, recommend optimal indexes, analyze execution plans, and leverage HypoPG for hypothetical index testing.
Features
Query Analysis
Get top resource-consuming queries from
pg_stat_statementsAnalyze query execution plans with
EXPLAINandEXPLAIN ANALYZEIdentify slow queries and bottlenecks
Index Tuning
Smart index recommendations based on query workload
Hypothetical index testing with HypoPG extension
Index health analysis (duplicate, unused, bloated indexes)
Estimate index size before creation
Database Health
Connection utilization monitoring
Vacuum health and transaction ID wraparound checks
Replication lag monitoring
Buffer cache hit rate analysis
Sequence limit warnings
HypoPG Integration
When the HypoPG extension is available, the server can:
Create hypothetical indexes without actual disk usage
Test how PostgreSQL would use potential indexes
Compare query plans with and without proposed indexes
Hide existing indexes to test removal impact
Installation
Standard Installation (for MCP clients like Claude Desktop)
Manual Installation
Configuration
Environment Variables
DATABASE_URI: PostgreSQL connection string (required)Format:
postgresql://user:password@host:port/database
MCP Client Configuration
Add to your cline_mcp_settings.json:
Server Modes
1. Standard MCP Mode (Default)
2. HTTP SSE Mode (Legacy Web Applications)
3. Streamable HTTP Mode (Modern MCP Protocol - Recommended)
The streamable-http mode implements the modern MCP Streamable HTTP protocol with a single /mcp endpoint. It supports both stateful (session-based) and stateless modes.
Stateful vs Stateless:
Stateful (default): Maintains session state across requests using
mcp-session-idheader. Ideal for long-running interactions.Stateless: Creates a fresh transport for each request with no session tracking. Ideal for serverless deployments or simple request/response patterns.
Endpoint: http://{host}:{port}/mcp
Available Tools
Query Analysis Tools
get_top_queries- Get the slowest or most resource-intensive queriesParameters:
sort_by(total_time, mean_time, resources),limit
explain_query- Explain the execution plan for a SQL queryParameters:
sql,analyze(boolean),hypothetical_indexes(optional)
Index Tuning Tools
analyze_workload_indexes- Analyze frequently executed queries and recommend optimal indexesParameters:
max_index_size_mb,method(dta, greedy)
analyze_query_indexes- Analyze specific SQL queries and recommend indexesParameters:
queries(list),max_index_size_mb
get_index_recommendations- Get index recommendations for a single queryParameters:
query,max_recommendations
test_hypothetical_index- Test how a hypothetical index would affect query performanceParameters:
table,columns,query,using(btree, hash, etc.)
list_hypothetical_indexes- List all current hypothetical indexesreset_hypothetical_indexes- Remove all hypothetical indexes
Database Health Tools
analyze_db_health- Comprehensive database health analysisParameters:
health_type(index, connection, vacuum, sequence, replication, buffer, constraint, all)
get_index_health- Analyze index health (duplicate, unused, bloated)
Utility Tools
execute_sql- Execute a SQL query (respects access mode)Parameters:
sql
list_schemas- List all schemas in the databaseget_table_info- Get detailed information about a tableParameters:
schema,table
HypoPG Extension
Enable in Database
Example Usage
Find Slow Queries
Analyze and Optimize a Query
Database Health Check
Docker
Build
Run
Requirements
Python 3.10+
PostgreSQL 12+ (recommended: 14+)
pg_stat_statementsextension (for query analysis)hypopgextension (optional, for hypothetical index testing)
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.