← All use cases Database Operations

Profile queries and optimize databases interactively

Your agent connects to PostgreSQL, Oracle, or MySQL, runs EXPLAIN plans, inspects indexes, and tunes queries — all inside psql, sqlplus, or mysql CLI. It reads the output, reasons about the plan, and applies fixes.

Illustrative workflow and output. Verify application compatibility and results in your own test environment.

database operations
$ claude "find slow queries on production"
 
> shell_exec("psql -U dba production")
 
> shell_exec("SELECT query, mean_exec_time FROM pg_stat_statements ORDER BY mean_exec_time DESC LIMIT 5;")
 
query | mean_exec_time
───────────────────────┼────────────────
SELECT * FROM orders | 2847.32 ms
WHERE status = $1 |
 
> shell_exec("CREATE INDEX CONCURRENTLY idx_orders_status ON orders(status, created_at);")
CREATE INDEX
 
Index created. Re-running query...
Execution Time: 1.2 ms (was 2847 ms)

The problem

Database optimization is iterative. You run a query, read the EXPLAIN plan, hypothesize an index, create it, re-run, compare. Each cycle requires understanding the database's interactive CLI — its prompts, output formatting, pager behavior, and multi-line input handling.

Some database workflows depend on a persistent client connection or interactive inspection. If your existing API or script handles them reliably, keep it. Evaluate a terminal session when retaining connection state or navigating client output is part of the task.

How agend solves it

Your agent gets a real Linux environment with database clients pre-installed. It opens psql, mysql, or sqlplus as an interactive session and operates it natively:

  • Interactive CLI sessions — the agent stays inside psql across multiple queries, maintaining connection state and transaction context
  • Read complex output — EXPLAIN plans, table schemas, index stats rendered exactly as the CLI formats them
  • Iterate on fixes — create an index, re-run the query, compare execution times, adjust if needed
  • Multi-database workflows — connect to staging, test a migration, then apply to production
  • Background monitoring — run pg_stat_activity in a loop, watch for lock contention, kill blocking queries

What you can automate

  • Slow query identification and index optimization
  • EXPLAIN plan analysis and query rewriting
  • Schema migration testing across environments
  • Lock detection and resolution
  • Replication lag monitoring and troubleshooting
  • Backup verification and restore testing
  • pg_stat_statements analysis and reporting

Key MCP tools used

  • shell_exec — launch database CLIs and run queries
  • shell_provide_input — send follow-up commands in interactive sessions
  • shell_raw_stdout — read query output and EXPLAIN plans
  • shell_task_output — monitor long-running operations

Supported databases

  • PostgreSQL (psql)
  • MySQL / MariaDB (mysql)
  • Oracle (sqlplus)
  • SQL Server (sqlcmd)
  • Redis (redis-cli)
  • MongoDB (mongosh)

Give your agent DBA-level database access.

Start Free Trial