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.
- Get top resource-consuming queries from
pg_stat_statements - Analyze query execution plans with
EXPLAINandEXPLAIN ANALYZE - Identify slow queries and bottlenecks
- 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
- Connection utilization monitoring
- Vacuum health and transaction ID wraparound checks
- Replication lag monitoring
- Buffer cache hit rate analysis
- Sequence limit warnings
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
pip install pgtuner_mcpgit clone https://github.com/example/pgtuner_mcp.git
cd pgtuner_mcp
pip install -e .DATABASE_URI: PostgreSQL connection string (required)- Format:
postgresql://user:password@host:port/database
- Format:
Add to your cline_mcp_settings.json:
{
"mcpServers": {
"pgtuner_mcp": {
"command": "python",
"args": ["-m", "pgtuner_mcp"],
"env": {
"DATABASE_URI": "postgresql://user:password@localhost:5432/mydb"
},
"disabled": false,
"autoApprove": []
}
}
}# Default mode (stdio)
python -m pgtuner_mcp
# Explicitly specify stdio mode
python -m pgtuner_mcp --mode stdio# Start SSE server on default host/port (0.0.0.0:8080)
python -m pgtuner_mcp --mode sse
# Specify custom host and port
python -m pgtuner_mcp --mode sse --host localhost --port 3000
# Enable debug mode
python -m pgtuner_mcp --mode sse --debugThe streamable-http mode implements the modern MCP Streamable HTTP protocol with a single /mcp endpoint. It supports both stateful (session-based) and stateless modes.
# Start Streamable HTTP server in stateful mode (default)
python -m pgtuner_mcp --mode streamable-http
# Start in stateless mode (fresh transport per request)
python -m pgtuner_mcp --mode streamable-http --stateless
# Specify custom host and port
python -m pgtuner_mcp --mode streamable-http --host localhost --port 8080
# Enable debug mode
python -m pgtuner_mcp --mode streamable-http --debugStateful 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
-
get_top_queries- Get the slowest or most resource-intensive queries- Parameters:
sort_by(total_time, mean_time, resources),limit
- Parameters:
-
explain_query- Explain the execution plan for a SQL query- Parameters:
sql,analyze(boolean),hypothetical_indexes(optional)
- Parameters:
-
analyze_workload_indexes- Analyze frequently executed queries and recommend optimal indexes- Parameters:
max_index_size_mb,method(dta, greedy)
- Parameters:
-
analyze_query_indexes- Analyze specific SQL queries and recommend indexes- Parameters:
queries(list),max_index_size_mb
- Parameters:
-
get_index_recommendations- Get index recommendations for a single query- Parameters:
query,max_recommendations
- Parameters:
-
test_hypothetical_index- Test how a hypothetical index would affect query performance- Parameters:
table,columns,query,using(btree, hash, etc.)
- Parameters:
-
list_hypothetical_indexes- List all current hypothetical indexes -
reset_hypothetical_indexes- Remove all hypothetical indexes
-
analyze_db_health- Comprehensive database health analysis- Parameters:
health_type(index, connection, vacuum, sequence, replication, buffer, constraint, all)
- Parameters:
-
get_index_health- Analyze index health (duplicate, unused, bloated)
-
execute_sql- Execute a SQL query (respects access mode)- Parameters:
sql
- Parameters:
-
list_schemas- List all schemas in the database -
get_table_info- Get detailed information about a table- Parameters:
schema,table
- Parameters:
CREATE EXTENSION hypopg;# Get top 10 resource-consuming queries
result = await get_top_queries(sort_by="resources", limit=10)# Get explain plan
plan = await explain_query(
sql="SELECT * FROM orders WHERE user_id = 123 AND status = 'pending'"
)
# Get index recommendations
recommendations = await analyze_query_indexes(
queries=["SELECT * FROM orders WHERE user_id = 123 AND status = 'pending'"]
)
# Test hypothetical index
test_result = await test_hypothetical_index(
table="orders",
columns=["user_id", "status"],
query="SELECT * FROM orders WHERE user_id = 123 AND status = 'pending'"
)# Run all health checks
health = await analyze_db_health(health_type="all")
# Check specific areas
index_health = await analyze_db_health(health_type="index")
vacuum_health = await analyze_db_health(health_type="vacuum")docker build -t pgtuner_mcp .# Streamable HTTP mode (recommended)
docker run -p 8080:8080 \
-e DATABASE_URI=postgresql://user:pass@host:5432/db \
pgtuner_mcp --mode streamable-http
# Streamable HTTP stateless mode
docker run -p 8080:8080 \
-e DATABASE_URI=postgresql://user:pass@host:5432/db \
pgtuner_mcp --mode streamable-http --stateless
# SSE mode (legacy)
docker run -p 8080:8080 \
-e DATABASE_URI=postgresql://user:pass@host:5432/db \
pgtuner_mcp --mode sse
# stdio mode (for MCP clients)
docker run \
-e DATABASE_URI=postgresql://user:pass@host:5432/db \
pgtuner_mcp- Python 3.10+
- PostgreSQL 12+ (recommended: 14+)
pg_stat_statementsextension (for query analysis)hypopgextension (optional, for hypothetical index testing)
Contributions are welcome! Please feel free to submit a Pull Request.