PostgreSQL query optimization with correctness validation via metamorphic testing.
An execution environment for SQL agents - like the terminal is for coding agents.
- Validates query correctness using TLP and NoREC metamorphic testing
- Detects performance bottlenecks via EXPLAIN plan analysis
- Suggests optimizations (indexes, query rewrites)
- Runs autonomous optimization loops with safety controls
LLM-generated SQL often has semantic errors that return wrong results silently. This tool catches those bugs.
git clone https://github.com/yudduy/sql_exenv.git
cd sql_exenv
pip install -r requirements.txtRequires Python 3.10+.
Start PostgreSQL with sample data:
docker-compose up -d
export DB_CONNECTION='postgresql://postgres:postgres@localhost:5432/demo'
export ANTHROPIC_API_KEY='your-key'Run CLI:
python cli.py # chat mode
python cli.py --query "SELECT * FROM users WHERE email='user5000@example.com'"
python cli.py --query "..." --validate-only # skip optimization
python cli.py --query "..." --no-validation # skip validationRun autonomous agent:
python run_agent.pyfrom src.agent import SQLOptimizationAgent
agent = SQLOptimizationAgent()
result = await agent.optimize_query(
sql="SELECT * FROM users WHERE email='user@example.com'",
db_connection="postgresql://postgres:postgres@localhost:5432/demo",
)
print(result['success'], result['final_query'])Analyzer: Parses EXPLAIN JSON, identifies sequential scans, high-cost operations, estimate errors.
Semanticizer: Translates analysis to natural language via Claude. Suggests CREATE INDEX or query rewrites.
Agent: ReAct-style loop using Claude Sonnet. Iteratively improves queries until optimized or max iterations.
Safety: Two-phase EXPLAIN (estimate cost first, run ANALYZE only if safe). Statement timeouts. HypoPG for virtual index testing.
pytest
pytest --cov=src --cov-report=htmlexport ANTHROPIC_API_KEY="your-key"
export DB_CONNECTION="postgresql://localhost:5432/mydb"Or use a .env file.
MIT
