Advanced PostgreSQL query performance monitoring, SQL optimization, and database analytics extension
Monitor slow queries • Track cache efficiency • Analyze WAL generation • Optimize database performance • Real-time metrics • Grafana dashboards
English (current) | 简体中文 | 繁體中文 | 日本語
Track 52 Metrics Across 11 Views - Monitor PostgreSQL Query Performance in Real-Time
Production-ready extension for PostgreSQL 16, 17, 18 - Drop-in replacement for pg_stat_statements with enhanced analytics
PostgreSQL Query Performance Monitoring Made Simple
pg_stat_insights is an advanced PostgreSQL extension for database performance monitoring, query optimization, and SQL analytics. Track and analyze 52 comprehensive metrics across 11 pre-built views to identify slow queries, optimize cache performance, and monitor database health in real-time.
Perfect for:
- Database Administrators monitoring PostgreSQL performance
 - DevOps teams tracking query performance and resource usage
 - Developers optimizing SQL queries and database operations
 - SREs implementing database monitoring and alerting
 
Key Features:
- 52 metric columns - Execution time, cache hits, WAL generation, JIT stats, buffer I/O
 - 11 pre-built views - Instant access to top slow queries, cache misses, I/O intensive operations
 - 11 parameters - Fine-tune tracking, histograms, and statistics collection
 - Drop-in replacement for pg_stat_statements with enhanced metrics
 - PostgreSQL 16-18 - Full compatibility with PostgreSQL 16, 17, and 18
 - 22 regression tests - Comprehensive test coverage for all features
 - Response time tracking - Categorize queries by execution time (less than 1ms to greater than 10s)
 - Cache analysis - Identify buffer cache inefficiencies and optimization opportunities
 - WAL monitoring - Track write-ahead log generation per query
 - Advanced features - JSON/JSONB, arrays, partitioning, triggers, window functions
 - Time-series data - Historical performance trending and bucket analysis
 - Prometheus/Grafana ready - Pre-built dashboards and alert rules included
 - CI/CD ready - GitHub Actions workflows for multi-version testing
 
Monitor PostgreSQL query performance in under 5 minutes:
-- Step 1: Enable extension in PostgreSQL configuration
ALTER SYSTEM SET shared_preload_libraries = 'pg_stat_insights';
-- Restart PostgreSQL server required
-- Step 2: Create the extension in your database
CREATE EXTENSION pg_stat_insights;
-- Step 3: View your slowest queries instantly
SELECT 
    query,
    calls,
    total_exec_time,
    mean_exec_time,
    rows
FROM pg_stat_insights_top_by_time 
LIMIT 10;Result: Instant visibility into query performance, execution times, cache efficiency, and resource usage across your PostgreSQL database.
Complete documentation available at:
Quick Links:
- Getting Started - Installation and setup
 - Configuration - All 11 parameters
 - Views Reference - All 11 views
 - Metrics Guide - All 52 columns
 - Usage Examples - 50+ SQL queries
 - Prometheus & Grafana - Monitoring integration
 - Troubleshooting - Common issues
 
# Build and install
cd pg_stat_insights
make
sudo make install
# Configure
echo "shared_preload_libraries = 'pg_stat_insights'" | \
  sudo tee -a /etc/postgresql/*/main/postgresql.conf
# Restart PostgreSQL
sudo systemctl restart postgresql
# Create extension
psql -d your_database -c "CREATE EXTENSION pg_stat_insights;"Detailed instructions: Installation Guide
All 11 pre-built views:
| View | Purpose | 
|---|---|
pg_stat_insights | 
Main statistics view (52 columns) | 
pg_stat_insights_top_by_time | 
Slowest queries by total time | 
pg_stat_insights_top_by_calls | 
Most frequently called queries | 
pg_stat_insights_top_by_io | 
Highest I/O consumers | 
pg_stat_insights_top_cache_misses | 
Poor cache performers | 
pg_stat_insights_slow_queries | 
Queries with mean time > 100ms | 
pg_stat_insights_errors | 
Queries with errors | 
pg_stat_insights_plan_errors | 
Plan estimation issues | 
pg_stat_insights_histogram_summary | 
Response time distribution | 
pg_stat_insights_by_bucket | 
Time-series aggregation | 
pg_stat_insights_replication | 
Replication monitoring | 
Complete reference: Views Documentation
Solve Common PostgreSQL Performance Problems:
- Find slow queries - Identify queries consuming excessive execution time and resources
 - Optimize cache usage - Detect buffer cache misses and improve shared_buffers efficiency
 - Reduce WAL overhead - Monitor write-ahead log generation per query type
 - Track query patterns - Analyze execution frequency, response times, and resource consumption
 - Monitor in real-time - Integrate with Grafana for live dashboards and alerting
 - PostgreSQL best practices - Built following PostgreSQL coding standards and conventions
 
| Feature | pg_stat_statements | pg_stat_monitor | pg_stat_insights | 
|---|---|---|---|
| Metric Columns | 44 | 58 | 52 | 
| Pre-built Views | 2 | 5 | 11 | 
| Configuration Options | 5 | 12 | 11 | 
| Cache Analysis | Basic | Basic | Enhanced with ratios | 
| Response Time Categories | No | No | Yes (<1ms to >10s) | 
| Time-Series Tracking | No | No | Yes (bucket-based) | 
| TAP Test Coverage | Standard | Limited | 150 tests, 100% coverage | 
| Documentation | Basic | Medium | 30+ pages on GitHub Pages | 
| Prometheus Integration | Manual | Manual | Pre-built queries & dashboards | 
See detailed comparison: Feature Comparison
Comprehensive TAP Test Suite for Quality Assurance:
- 16 test files covering all extension functionality
 - 150 test cases with 100% code coverage
 - Tests all 52 metric columns, 11 views, 11 parameters
 - Custom StatsInsightManager.pm framework
 - No external Perl dependencies required
 - Compatible with PostgreSQL 18 testing infrastructure
 
Run PostgreSQL extension tests:
./run_all_tests.shContinuous Integration: GitHub Actions workflow for automated PostgreSQL testing on every commit
Learn more: Testing Guide
Real-Time PostgreSQL Metrics Visualization:
Turn pg_stat_insights data into actionable dashboards and alerts for PostgreSQL database monitoring:
- Prometheus integration - 5 pre-configured queries for postgres_exporter
 - Grafana dashboards - 8 ready-to-use panels for query performance visualization
 - Alert rules - 11 production-ready alerts for database health monitoring
 - Query rate tracking - Monitor queries per second (QPS) and throughput
 - Cache performance - Real-time buffer cache hit ratio monitoring
 - Response time SLA - Track P95/P99 query latency for service level objectives
 - WAL generation alerts - Monitor write-ahead log growth and disk usage
 
Complete Prometheus/Grafana guide: Monitoring Integration
MIT License - Copyright (c) 2024-2025, pgElephant, Inc.
See LICENSE for details.
- Complete Documentation
 - Interactive Demo - Try pg_stat_insights online
 - Blog Article - Comprehensive guide and best practices
 - GitHub Repository
 - Report Issues
 - Discussions
 
Built by pgElephant, Inc.
Making PostgreSQL monitoring better, one metric at a time