Postgres MCP Pro Plus
# Postgres MCP Pro Plus
<p align="center">
<strong>Advanced PostgreSQL Database Analysis & Optimization Suite</strong><br>
<em>Extended version based on <a href="https://github.com/crystaldba/postgres-mcp">crystaldba/postgres-mcp</a></em>
</p>
## π Key Features
- **π Comprehensive Database Analysis**: Deep insights into schema structure, relationships, and performance
- **β‘ AI-Powered Optimization**: Intelligent index recommendations using Database Tuning Advisor (DTA) and LLM methods
- **π©Ί Advanced Health Monitoring**: Multi-dimensional health checks with predictive analytics
- **π Lock & Blocking Analysis**: Real-time detection and resolution of query blocking and deadlocks
- **π§Ή Smart Maintenance**: Automated vacuum analysis with bloat detection and maintenance scheduling
- **π Performance Intelligence**: Query performance analysis with resource usage optimization
- **π Security Assessment**: Comprehensive security analysis and recommendations
- **π³ Docker Ready**: Containerized deployment with Docker Compose support
## π Available Tools
### Core Database Operations
| Tool Name | Description |
| -------------------- | ------------------------------------------------------------------------ |
| `list_schemas` | List all schemas with ownership and type classification |
| `list_objects` | Browse database objects (tables, views, sequences, extensions) by schema |
| `get_object_details` | Detailed object analysis including columns, constraints, and indexes |
| `execute_sql` | Execute SQL with safety controls (restricted/unrestricted modes) |
### Performance & Optimization
| Tool Name | Description |
| -------------------------- | -------------------------------------------------------------------------- |
| `explain_query` | Advanced execution plan analysis with HypoPG hypothetical index simulation |
| `get_top_queries` | Identify slow and resource-intensive queries with performance metrics |
| `analyze_workload_indexes` | AI-powered index recommendations from workload analysis (DTA/LLM) |
| `analyze_query_indexes` | Targeted index optimization for specific query sets (up to 10 queries) |
### Health & Monitoring
| Tool Name | Description |
| ----------------------------- | ------------------------------------------------------------------------------------------------------------ |
| `analyze_db_health` | Comprehensive health checks: indexes, connections, vacuum, sequences, replication, buffer cache, constraints |
| `get_blocking_queries` | Advanced blocking analysis with lock hierarchy visualization and resolution recommendations |
| `analyze_vacuum_requirements` | Comprehensive vacuum analysis with bloat detection and maintenance recommendations |
### Advanced Analysis
| Tool Name | Description |
| ------------------------------ | ------------------------------------------------------------------------------------------ |
| `get_database_overview` | Enterprise-grade database assessment with performance, security, and relationship analysis |
| `analyze_schema_relationships` | Schema dependency mapping with visual relationship analysis and coupling metrics |
## π§ Tool Details & Capabilities
### π Database Overview Analysis
**Enterprise-grade comprehensive database assessment**
The `get_database_overview` tool provides multi-dimensional analysis:
- **π Schema Analysis**: Complete structure with table relationships and dependency mapping
- **β‘ Performance Metrics**: Query performance, index efficiency, and resource utilization patterns
- **π Security Analysis**: User permissions, role assignments, and security configuration assessment
- **πΎ Storage Analysis**: Table sizes, index bloat detection, and disk usage optimization
- **π©Ί Health Indicators**: Connection health, vacuum statistics, and system performance metrics
**Configuration Options:**
- `max_tables` (default: 500): Maximum tables to analyze per schema for performance control
- `sampling_mode` (default: true): Statistical sampling for large datasets to optimize execution time
- `timeout` (default: 300): Maximum execution time with graceful timeout handling
### π Advanced Blocking Queries Analysis
**Real-time lock contention detection and resolution**
The `get_blocking_queries` tool features enterprise-grade capabilities:
**π― Core Features:**
- **Modern Detection**: Uses PostgreSQL's `pg_blocking_pids()` function for accurate blocking identification
- **Lock Hierarchy Visualization**: Complete blocking chains and process relationships
- **Comprehensive Metrics**: Process details, wait events, timing, lock types, and affected relations
- **Intelligent Recommendations**: Severity-based suggestions with specific optimization guidance
- **Production Ready**: Designed for enterprise database monitoring and performance troubleshooting
**π Analysis Output:**
- **Process Information**: PID, user, application name, client address, and connection details
- **Query Context**: Full query text, execution timing, and resource consumption
- **Lock Details**: Lock types, modes, affected database objects, and wait events
- **State Analysis**: Process states, wait information, and blocking duration
- **Trend Analysis**: Summary statistics and pattern recognition
- **Categorized Recommendations**: π¨ Critical, β οΈ Warning, π‘ Optimization, π― Hotspot alerts
**π§ PostgreSQL Compatibility:**
- **Minimum**: PostgreSQL 9.6+ (requires `pg_blocking_pids()` function)
- **Recommended**: PostgreSQL 12+ (enhanced lock monitoring features)
- **Optimal**: PostgreSQL 14+ (includes `pg_locks.waitstart` for precise wait timing)
### π§Ή Vacuum Analysis & Maintenance
**Comprehensive maintenance planning with bloat detection**
The `analyze_vacuum_requirements` tool provides:
- **π Bloat Analysis**: Table and index bloat detection with severity assessment
- **βοΈ Autovacuum Configuration**: Settings analysis and optimization recommendations
- **π Performance Impact**: Vacuum operation performance analysis and bottleneck identification
- **ποΈ Maintenance Planning**: Intelligent scheduling recommendations based on workload patterns
- **π¨ Critical Issue Detection**: Immediate attention alerts for maintenance-related problems
- **β‘ Configuration Optimization**: Tuning suggestions for vacuum parameters
### πΊοΈ Schema Relationship Analysis
**Advanced dependency mapping and visualization**
The `analyze_schema_relationships` tool offers:
- **π Dependency Mapping**: Complete inter-schema relationship visualization
- **π Coupling Analysis**: Schema coupling metrics and isolation scoring
- **π― Impact Assessment**: Change impact analysis for schema modifications
- **π Relationship Quality**: Foreign key relationship quality and consistency scoring
- **π Pattern Detection**: Common anti-patterns and architectural recommendations
### β‘ Index Optimization Intelligence
**AI-powered index recommendations with advanced algorithms**
**Database Tuning Advisor (DTA) Features:**
- **π§ Pareto Optimization**: Multi-objective optimization balancing performance and storage
- **π Workload Analysis**: Pattern recognition from pg_stat_statements data
- **π° Cost-Benefit Analysis**: Storage budget constraints with performance impact assessment
- **π― Query-Specific Tuning**: Targeted optimization for specific query sets
- **β±οΈ Time-bounded Analysis**: Anytime algorithm with configurable runtime limits
**LLM-Powered Optimization:**
- **π€ Intelligent Analysis**: Natural language understanding of query patterns
- **π Contextual Recommendations**: Human-readable explanations with implementation guidance
- **π Advanced Pattern Recognition**: Complex query pattern detection and optimization
## π Quick Start
### Prerequisites
- PostgreSQL 9.6+ (PostgreSQL 12+ recommended, 14+ optimal)
- Python 3.8+
- Optional: HypoPG extension for hypothetical index analysis
### Installation & Setup
#### 1. Environment Configuration
Create a `.env` file in the project root:
```bash
DATABASE_URI=postgresql://username:password@localhost:5432/database_name
```
#### 2. Native Deployment
```bash
# Start the MCP server (default: stdio transport, unrestricted mode)
./start.sh
# Start in read-only mode for safer analysis
./start.sh --access-mode restricted
# Start with SSE transport for web integration
./start.sh --transport sse --sse-port 8099
# Start SSE server accessible externally
./start.sh --transport sse --sse-host 0.0.0.0 --sse-port 8099
# Show all available options
./start.sh --help
```
#### 3. Docker Deployment
```bash
# Start with Docker Compose
docker-compose up -d
# View logs
docker-compose logs -f postgres-mcp
```
#### 4. Interactive Testing (MCP Inspector)
```bash
# Terminal 1: Start the MCP server with SSE transport
./start.sh --transport sse --sse-port 8099
# Terminal 2: Start the MCP Inspector (opens web interface)
./start-inspector.sh
```
The MCP Inspector provides:
- **Interactive Tool Testing**: Test all database analysis tools with a web UI
- **Parameter Exploration**: Discover tool capabilities and configuration options
- **Real-time Results**: View formatted analysis results in a user-friendly interface
- **Documentation**: Built-in tool documentation and usage examples
### π§ Access Modes
**Unrestricted Mode** (Default):
- Full SQL execution capabilities
- Database modification operations
- Complete administrative access
**Restricted Mode** (Recommended for analysis):
- Read-only operations with safety controls
- SQL injection protection
- Timeout enforcement (30s default)
- Safe for production analysis
### π Usage Examples
#### Basic Server Operations
```bash
# Show help and configuration options
./start.sh --help
# Start with default settings (stdio, unrestricted)
./start.sh
# Start in production-safe mode
./start.sh --access-mode restricted
# Start web server for HTTP/SSE integration
./start.sh --transport sse --sse-port 8099
```
#### Health Check Examples
```bash
# Comprehensive health analysis (via MCP client)
analyze_db_health --health-type all
# Specific component checks
analyze_db_health --health-type index,vacuum,buffer
# Performance optimization workflow
get_top_queries --sort-by resources
analyze_workload_indexes --method dta --max-index-size-mb 1000
get_blocking_queries
```
## ποΈ Architecture & Components
### Core Architecture
```
postgres-mcp/
βββ π§ server.py # MCP server & tool registration
βββ π database_health/ # Multi-dimensional health monitoring
βββ β‘ explain/ # Query execution plan analysis
βββ π― index/ # AI-powered index optimization
βββ π top_queries/ # Performance query analysis
βββ π blocking_queries.py # Lock contention analysis
βββ π database_overview.py # Comprehensive assessment
βββ πΊοΈ schema_mapping.py # Relationship visualization
βββ π§Ή vacuum_analysis.py # Maintenance optimization
βββ π‘οΈ sql/ # SQL execution framework
```
### Database Health Components
- **Index Health**: Invalid, duplicate, bloated, and unused index detection
- **Connection Health**: Connection utilization and capacity analysis
- **Vacuum Health**: Transaction wraparound and maintenance monitoring
- **Sequence Health**: Sequence exhaustion and overflow protection
- **Replication Health**: Lag monitoring and slot management
- **Buffer Health**: Cache hit rate optimization for tables and indexes
- **Constraint Health**: Invalid constraint detection and remediation
### π€ AI Integration Features
**Database Tuning Advisor (DTA):**
- Pareto-optimal index selection algorithm
- Multi-query workload optimization
- Budget-constrained recommendation engine
- Time-bounded analysis with anytime approach
**LLM-Powered Analysis:**
- Natural language query pattern understanding
- Contextual optimization recommendations
- Human-readable explanations and guidance
- Advanced pattern recognition capabilities
## π Recent Enhancements
### Latest Features (Recent Commits)
- β
**Comprehensive Tool Analysis**: Detailed analysis document with improvement recommendations
- β
**Enhanced Readability**: Streamlined code formatting across all modules
- β
**Robust Error Handling**: Improved None value handling in vacuum analysis
- β
**Advanced Visualizations**: Enhanced blocking queries analysis with detailed recommendations
- β
**Human-Readable Outputs**: Refactored analysis tools for better text presentation
- β
**Schema Relationship Mapping**: New schema dependency analysis and visualization
- β
**Docker Integration**: Complete containerization with Docker Compose support
- β
**Vacuum Analysis Tool**: Comprehensive maintenance recommendations and bloat detection
### Architecture Improvements
- **Modular Design**: Enhanced component separation and reusability
- **Async Optimization**: Improved performance with better async patterns
- **Safety Framework**: Comprehensive SQL execution safety controls
- **Error Recovery**: Robust error handling and graceful degradation
- **Performance Scaling**: Optimized for large database analysis
- **Enhanced Startup Scripts**: Flexible configuration with comprehensive validation and help system
## π Documentation & Development
### Advanced Documentation
- **[Database Tools Analysis](plan/database-tools-analysis.md)**: Comprehensive analysis of all tools with improvement recommendations
- **[Tool Improvements Roadmap](todos/tool_improvements.md)**: Priority-based enhancement roadmap _(if available)_
- **Technical Implementation**: Detailed code documentation and API references
### Extension Points
- **Custom Health Checks**: Add domain-specific health monitoring
- **Plugin Architecture**: Extend with custom analysis tools
- **Integration APIs**: Connect with external monitoring systems
- **Custom Visualizations**: Add specialized reporting and dashboards
## π Security & Best Practices
### Security Features
- **SQL Injection Protection**: Comprehensive input sanitization
- **Access Mode Controls**: Restricted/unrestricted operation modes
- **Timeout Enforcement**: Configurable query timeout protection
- **Parameter Validation**: Robust input validation and sanitization
- **Error Handling**: Secure error reporting without information leakage
### Production Guidelines
- Use **restricted mode** for production analysis
- Configure appropriate **timeout values** for large operations
- Monitor **resource usage** during analysis operations
- Implement **regular health checks** for proactive monitoring
- Review **security configurations** and user permissions regularly
## π License
MIT License
---
<p align="center">
<strong>π Postgres MCP Pro Plus - Advanced Database Intelligence</strong><br>
<em>Empowering database professionals with AI-driven insights and optimization</em>
</p>
TDQS
Scored across 13 tools
Most tools have distinct purposes, but there is some overlap between analyze_query_indexes and analyze_workload_indexes, both focusing on index recommendations, which could cause confusion. However, their descriptions clarify that one analyzes specific queries while the other analyzes frequently executed queries, helping to differentiate them.
The naming follows a consistent verb_noun pattern (e.g., analyze_db_health, execute_sql, list_schemas) with minor deviations like get_blocking_queries and get_database_overview using 'get' instead of 'list' or 'analyze'. Overall, the pattern is predictable and readable.
With 13 tools, the count is well-scoped for a Postgres database management server, covering analysis, execution, and listing functions. Each tool appears to serve a specific purpose without feeling excessive or insufficient for the domain.
The tool set provides comprehensive coverage for Postgres database management, including health analysis, query optimization, schema exploration, and SQL execution. There are no obvious gaps; it supports CRUD-like operations and lifecycle management through tools like execute_sql and various analysis functions.