This is a learning project implemented in a personal capacity to explore PostgreSQL query optimization and web application development. The tool transforms PostgreSQL query execution plans into interactive visualizations, helping developers understand query performance characteristics.
graph TB
subgraph "User Interface"
UI[React Frontend<br/>TypeScript + Vite]
Canvas[PlanCanvas<br/>React Flow Visualization]
Sidebar[PlanSidebar<br/>Node Details]
Advisor[IndexAdvisor<br/>Recommendations]
Compare[PlanCompare<br/>Side-by-side Analysis]
end
subgraph "Backend API"
API[FastAPI Server<br/>Python 3.11+]
Parse[Parse Endpoint<br/>/plans/parse]
CompareAPI[Compare Endpoint<br/>/plans/compare]
AdviseAPI[Advise Endpoint<br/>/advise]
end
subgraph "Core Processing"
Normalize[normalize.py<br/>Parse EXPLAIN JSON]
Metrics[metrics.py<br/>Critical Path & Heat Scores]
AdvisorCore[advisor.py<br/>Index Suggestions]
end
subgraph "Data Flow"
JSON[PostgreSQL EXPLAIN<br/>ANALYZE, BUFFERS, FORMAT JSON]
PlanDoc[PlanDoc<br/>Normalized Plan Structure]
Nodes[Nodes & Edges<br/>Graph Representation]
end
UI --> Canvas
UI --> Sidebar
UI --> Advisor
UI --> Compare
Canvas --> API
Sidebar --> API
Advisor --> AdviseAPI
Compare --> CompareAPI
API --> Parse
API --> CompareAPI
API --> AdviseAPI
Parse --> Normalize
Normalize --> Metrics
Metrics --> AdvisorCore
JSON --> Parse
Parse --> PlanDoc
PlanDoc --> Nodes
Nodes --> Canvas
style UI fill:#667eea,stroke:#333,stroke-width:2px,color:#fff
style API fill:#06b6d4,stroke:#333,stroke-width:2px,color:#fff
style Normalize fill:#10b981,stroke:#333,stroke-width:2px,color:#fff
style Metrics fill:#10b981,stroke:#333,stroke-width:2px,color:#fff
style AdvisorCore fill:#10b981,stroke:#333,stroke-width:2px,color:#fff
sequenceDiagram
participant User
participant Frontend
participant Backend
participant Normalizer
participant Metrics
participant Advisor
User->>Frontend: Upload EXPLAIN JSON
Frontend->>Backend: POST /plans/parse
Backend->>Normalizer: Parse JSON to PlanDoc
Normalizer->>Backend: Return nodes & edges
Backend->>Metrics: Compute critical path
Metrics->>Backend: Return path & heat scores
Backend->>Frontend: Return normalized plan
Frontend->>Frontend: Render graph visualization
User->>Frontend: Click node
Frontend->>Frontend: Display node details
User->>Frontend: Request index advice
Frontend->>Backend: POST /advise
Backend->>Advisor: Analyze plan for indexes
Advisor->>Backend: Return suggestions
Backend->>Frontend: Return index SQL
Frontend->>User: Display recommendations
User->>Frontend: Compare two plans
Frontend->>Backend: POST /plans/compare
Backend->>Normalizer: Parse both plans
Backend->>Metrics: Compute deltas
Backend->>Frontend: Return comparison
Frontend->>User: Side-by-side view
Visual SQL Plan Explorer is a learning project designed to transform PostgreSQL query execution plans into actionable performance insights. Built with FastAPI and React, the tool parses EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) output and provides interactive visualizations, automated bottleneck detection, and intelligent index recommendations.
The application implements graph-based algorithms to compute critical execution paths, heat scores for performance hotspots, and pattern-matching heuristics to identify missing indexes. The frontend leverages React Flow for interactive plan exploration, while the backend uses Pydantic models for type-safe plan normalization and metric computation.
This project serves as an educational exploration of database query optimization, web application architecture, and data visualization techniques.
- Interactive Plan Visualization - Drag, zoom, and explore query plans with React Flow graph visualization
- Critical Path Analysis - Automatic identification and highlighting of the slowest execution path through the plan tree
- Heatmap Visualization - Color-coded nodes showing performance hotspots based on execution time, buffer reads, and row estimation accuracy
- Performance Metrics Dashboard - Real-time display of total execution time, cost, row counts, and warning summaries
- Index Advisor - Intelligent suggestions for missing indexes with ready-to-use SQL statements
- Plan Comparison - Side-by-side comparison of query plans with performance deltas and diff annotations
- Demo Mode - Interactive demonstration with pre-loaded sample plans showcasing optimization scenarios
- Presentation Mode - Clean, enlarged interface optimized for demos and technical presentations
- Node Detail Inspector - Click any plan node to view detailed execution metrics, buffer statistics, and warnings
- Automatic Warning Detection - Identifies common performance issues including sequential scans, high buffer reads, and row estimation errors
- Responsive Design - Works seamlessly on desktop, tablet, and mobile devices
# Clone the repository
git clone https://github.com/your-username/visual-sql-plan-explorer.git
cd visual-sql-plan-explorer
# Start all services
make up
# Load demo data (optional)
make load-demo
# Open the application
open http://localhost:5173cd backend
pip install -e .
uvicorn app.main:app --reload --port 8000cd frontend
npm install
npm run dev# Start PostgreSQL
docker run -d \
--name postgres-demo \
-e POSTGRES_DB=plans \
-e POSTGRES_USER=dev \
-e POSTGRES_PASSWORD=dev \
-p 5432:5432 \
postgres:16
# Load sample data
psql -h localhost -U dev -d plans -f scripts/seed.sqlOption A: Upload JSON File
- Drag and drop your PostgreSQL
EXPLAINJSON output - Or click to select a file
Option B: Paste JSON
- Copy your
EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON)output - Paste directly into the text area
- Heatmap: Red nodes indicate performance bottlenecks
- Critical Path: Highlighted path shows the slowest execution route
- Node Details: Click any node to see detailed metrics in the sidebar
- Warnings: Automatic detection of common performance issues
- Click the "Index Advisor" section
- Review suggested indexes with SQL statements
- Copy SQL to clipboard with one click
- Apply suggestions to your database
- Switch to "Compare Plans" mode
- Upload two different query plans
- See side-by-side comparison with performance deltas
- Identify improvements or regressions
Try the interactive demo with pre-loaded sample plans:
- Click "Demo Mode" in the header
- Navigate through the 3-step demonstration:
- Step 1: Sequential scan problem
- Step 2: Missing index detection
- Step 3: Performance after optimization
The repository includes sample queries in scripts/demo_queries.sql:
-- Sequential scan with filter
EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON)
SELECT * FROM customers WHERE city = 'New York' AND status = 'active';
-- Join without proper indexes
EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON)
SELECT c.name, o.total_amount
FROM customers c
JOIN orders o ON c.id = o.customer_id
WHERE c.city = 'Los Angeles';- FastAPI - Modern, fast web framework
- Pydantic - Data validation and serialization
- Core Modules:
normalize.py- Parse PostgreSQL EXPLAIN JSONmetrics.py- Calculate critical path and heat scoresadvisor.py- Generate index suggestions
- React 18 - Modern React with hooks
- React Flow - Interactive graph visualization
- TanStack Table - Data grid components
- Vite - Fast build tool and dev server
PlanCanvas- Interactive plan visualizationPlanSidebar- Node details and metricsIndexAdvisor- Index recommendation enginePlanCompare- Side-by-side plan comparisonDemoScript- Interactive demonstration mode
Parse PostgreSQL EXPLAIN JSON into normalized format.
Request:
{
"rawExplainJson": { /* PostgreSQL EXPLAIN JSON */ }
}Response:
{
"id": "uuid",
"summary": {
"totalTimeMs": 1500.0,
"totalCost": 10000.0,
"totalRows": 50000,
"warnings": ["High shared read", "Sequential scan detected"]
},
"nodes": [ /* Plan nodes */ ],
"edges": [ /* Plan edges */ ],
"critical_path": ["node1", "node2"],
"warnings": []
}Compare two query plans and compute deltas.
Generate index suggestions for a query plan.
- Python 3.11+
- Node.js 18+
- PostgreSQL 16+
# Install pre-commit hooks
pre-commit install
# Backend development
cd backend
pip install -e ".[dev]"
pytest
# Frontend development
cd frontend
npm install
npm run lintmake up # Start all services
make down # Stop all services
make test # Run tests
make fmt # Format code
make lint # Lint code
make load-demo # Load demo data
make clean # Clean up containers and volumes- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Commit changes:
git commit -m 'Add amazing feature' - Push to branch:
git push origin feature/amazing-feature - Open a Pull Request
- Follow PEP 8 for Python code
- Use TypeScript for all frontend code
- Write tests for new features
- Update documentation for API changes
- Use conventional commit messages
This project is licensed under the MIT License - see the LICENSE file for details.
- PostgreSQL community for excellent documentation
- React Flow team for the amazing graph visualization library
- FastAPI team for the modern Python web framework
- Support for more database systems (MySQL, SQL Server)
- Query plan history and versioning
- Automated performance regression detection
- Integration with monitoring tools
- Advanced index recommendation algorithms
- Query plan sharing and collaboration features
Built for the PostgreSQL community