Getting Started withpg_stat_insights
Deep PostgreSQL performance analytics in minutes. Identify bottlenecks, optimize queries, and monitor database health.
Setup Steps
Follow these steps to start analyzing your PostgreSQL performance
PostgreSQL Extensions
Install required PostgreSQL extensions for deep analytics
Core Extensions
Configuration
Installation
Install pg_stat_insights Python package
Python Package
Dependencies
Basic Usage
Connect and start collecting insights
Simple Connection
Environment Variables
Integration with pgSentinel
Use with pgSentinel monitoring platform
pgSentinel Integration
- Real-time dashboard at /insights
- REST API endpoints for programmatic access
- Prometheus metrics export
- Grafana dashboard integration
API Endpoints
First Insights
Explore your database performance data
Query Performance
- Identify slowest queries by execution time
- Find most frequently executed queries
- Analyze query patterns and optimization opportunities
Table & Index Analysis
- Detect table bloat and vacuum needs
- Find unused or missing indexes
- Monitor cache hit ratios
Quick Start Examples
Get started with common analytics tasks
Query Analysis
Find slow queries and optimization opportunities
# Get top slow queries
queries = await insights.get_query_insights(limit=10)
for query in queries:
print(f"Query: {query['query'][:100]}...")
print(f"Avg time: {query['avg_time']}ms")
print(f"Calls: {query['calls']}")
print("---")Table Statistics
Monitor table health and bloat
# Get table statistics
tables = await insights.get_table_stats()
for table in tables:
print(f"Table: {table['table_name']}")
print(f"Bloat: {table['bloat_percentage']}%")
print(f"Size: {table['size_mb']}MB")
print("---")Cache Analysis
Monitor buffer cache performance
# Get cache hit ratio
cache_stats = await insights.get_cache_stats()
print(f"Buffer hit ratio: {cache_stats['buffer_hit_ratio']}%")
print(f"Index hit ratio: {cache_stats['index_hit_ratio']}%")
print(f"Toast hit ratio: {cache_stats['toast_hit_ratio']}%")Recommendations
Get intelligent optimization suggestions
# Get performance recommendations
recommendations = await insights.get_recommendations()
for rec in recommendations:
print(f"Priority: {rec['priority']}")
print(f"Type: {rec['type']}")
print(f"Description: {rec['description']}")
print("---")Troubleshooting
Common issues and solutions
Extension not found
Ensure PostgreSQL extensions are properly installed and enabled
Connection refused
Check database connection string and PostgreSQL is running
No data in pg_stat_statements
Restart PostgreSQL after enabling extension and run some queries
Permission denied
Ensure user has proper permissions to access system catalogs
Next Steps
Now that pg_stat_insights is set up, explore advanced analytics and integration options.