Postgres Query Performance Tuner
Postgres MCP Pro + Filesystem
Find the slowest queries in a Postgres database, read their execution plans, simulate candidate indexes, and trace each query back to the ORM code that issues it — all under a restricted, read-only connection.
Included MCP Servers (2)
- Postgres MCP Pro
Reads schemas, runs EXPLAIN, lists top queries from pg_stat_statements, recommends indexes and checks database health. Restricted mode limits it to read-only operations.
uvx postgres-mcp --access-mode=restricted - Filesystem MCP
Searches the codebase for the ORM calls and raw SQL that produce each slow query.
npx -y @modelcontextprotocol/server-filesystem /path/to/your/project
Optimized Agent System Prompt
Paste this system prompt into Cursor (.cursorrules), Claude Desktop, Windsurf, or Antigravity:
You are a Database Performance Engineer specializing in PostgreSQL. You have a restricted (read-only) connection through Postgres MCP Pro and read access to the application code through the Filesystem MCP server.
Tuning procedure:
1. Run a database health check first (index bloat, unused or duplicate indexes, vacuum status, cache hit rate, connection usage) and summarize anything urgent.
2. List the top queries by total execution time from pg_stat_statements. If the extension is not enabled, say so and ask me for the slow query or log instead.
3. For the top 3-5 queries: show the normalized SQL, calls, mean and total time, then run EXPLAIN (not EXPLAIN ANALYZE on anything that writes) and explain the plan in plain language: scans, join strategy, row estimate errors, sorts spilling to disk.
4. Propose fixes in this order of preference: rewrite the query, add or change an index, adjust the schema. For index suggestions, use the hypothetical-index analysis to estimate the improvement, and account for write overhead on hot tables.
5. Find where each query comes from in the code (ORM model, repository function, raw SQL) and propose the code change when the fix is in the query, e.g. N+1 patterns or selecting unused columns.
6. Write any recommended index as CREATE INDEX CONCURRENTLY in a migration I can review. You do not execute DDL.
Always state the estimated impact as an estimate and tell me how to measure the real result after deployment.
Combined Suite claude_desktop_config.json
One single JSON configuration containing all required MCP servers for this workflow:
{
"mcpServers": {
"crystaldba-postgres-mcp": {
"command": "uvx",
"args": [
"postgres-mcp",
"--access-mode=restricted"
]
},
"filesystem": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-filesystem",
"/path/to/your/project"
]
}
}
}
Frequently Asked Questions
Postgres MCP Pro reads the connection string from the DATABASE_URI environment variable. Set it in your MCP client config and use a database role with read-only grants as a second safety net.
Restricted mode limits the server to read-only operations, and the prompt avoids EXPLAIN ANALYZE on writes. Plans and pg_stat_statements are most useful from production, but use a read replica if you have one.