AI Database Diagnostics
An n8n diagnostics agent that loops through planning, MCP tool calls, and retries, then returns a finding an operator can act on.
PostgreSQL · AI · Diagnostics · n8n · MCP · Automation
Problem
Monitoring fires alerts; people still dig through sessions, locks, and query lists by hand. Context is scattered, response time suffers, and the same playbooks get re-discovered under pressure.
What it does
An AI diagnostics agent sits behind chat or alert hooks. n8n owns the looping. A planner model decides whether live data is required, then calls MCP tools to run read-only checks—active sessions, wait events, blocking locks, long-running statements, cache and I/O stats. Those queries can run one at a time or in parallel. A synthesizer turn turns the results into a short, actionable answer.
MCP tools
The agent does not invent a private SQL socket. It calls a small MCP-style tool surface:
- Execute query — read-only SELECT against the selected instance
- Process list — a sibling workflow that analyzes running sessions when the question is about who is on the box
Tools return columns, rows, and errors. The model never writes. Hostnames, API keys, and internal proxy URLs stay out of the story.
Agent loops in n8n
Two sanitized n8n workflows drive this: a database diagnostics agent and a process-list analysis agent. Both share the same loop shape.
- Think loop — reason first; reply with no SQL when the answer is already known
- SQL loop — emit one
<execute_sql>block or several, then run them through MCP - Error loop — if a probe fails (missing relation, bad column, syntax), ask the model for a corrected query and retry
- Feedback loop — keep session history so follow-up questions stay in context
- Synthesize — one finding back to the operator
The built-in n8n AI Agent node was skipped so the proxy, retries, and parallel probes stay explicit.
How a turn runs
- Intake — operator question or monitoring signal, plus session id
- Plan — model chooses no SQL, one query, or several probes
- Collect — MCP execute-query, in parallel when the planner asked for more than one
- Recover — retry only when the error looks fixable
- Synthesize — combined evidence into likely cause and next checks
Outcome
Faster triage with shared language: what is blocked, who is waiting, which statements dominate—and what to look at next—without pasting hostnames or internal service URLs into the product story.