c77_dbh

PostgreSQL Database Health Monitoring Extension

← Back to Portfolio

c77_dbh - PostgreSQL Database Health Extension

A lightweight, intelligent PostgreSQL extension for database health monitoring and maintenance. Provides actionable insights through JSON-formatted metrics on table bloat, index efficiency, materialized view performance, and more.

PostgreSQL 13+ MIT License Version 1.0

🔍 Overview

c77_dbh embeds health monitoring directly into your PostgreSQL database, eliminating the need for external monitoring tools. It uses statistical analysis to establish dynamic, database-specific thresholds for various health indicators, enabling both technical and non-technical users to identify and address performance issues.

✨ Features

🧠 Intelligent Vacuum Monitoring

Tracks table bloat with context-aware thresholds and TOAST overhead detection

📊 Index Usage Analysis

Identifies unused or bloated indexes with efficiency metrics and specific recommendations

⚡ Materialized View Performance

Monitors refresh times and bottlenecks (I/O, CPU, memory) with historical trending

🎯 Adaptive Precision

Automatically leverages pgstattuple and pg_stat_statements when available, with graceful fallbacks

👥 Human-readable Insights

Provides clear status indicators (Red/Yellow/Green), severity scores (1-5), and actionable recommendations

📈 Dashboard-Ready Outputs

Structured JSON format ideal for integration with monitoring dashboards

🚀 Low Overhead

Minimal impact on database performance with configurable collection frequencies

📊 Example Output

[ { "metric": "Table Vacuum Health", "table_name": "public.large_table", "status": "Red", "severity": 5, "insight": "Table bloat is critically high; TOAST overhead is significant.", "action": "Run VACUUM FULL or CLUSTER; review large fields for TOAST impact.", "last_vacuum": "2025-03-10 14:30:22", "last_autovacuum": "2025-03-12 02:15:44", "vacuum_count": 127, "dead_tuples": 1543267, "live_tuples": 4329856, "table_size": "2.3 GB", "bloat_estimate": "42% (precise)", "details": { "median_bloat": "15%", "bloat_stddev": "8%", "toast_bloat": "22%", "explanation": "Includes TOAST bloat in total health assessment." } } ]

📋 Requirements

📘 Usage

Basic Setup

After installation, enable the extension in your database:

CREATE EXTENSION c77_dbh;

Collecting Statistics

Initialize the statistics tables:

-- Update table and vacuum statistics
SELECT c77_dbh_update_vacuum_stats();

-- Update index statistics (including TOAST indexes)
SELECT c77_dbh_update_index_stats();

Monitoring Health

Query health metrics:

-- Table health with bloat estimates and recommendations
SELECT * FROM c77_dbh_get_vacuum_health();

-- Index health analysis
SELECT * FROM c77_dbh_get_index_health();

-- TOAST index specific analysis
SELECT * FROM c77_dbh_get_toast_health();

-- Materialized view refresh performance
SELECT * FROM c77_dbh_get_mv_health();

Automation

With pg_cron extension:

-- Update vacuum stats hourly
SELECT cron.schedule('update_vacuum_stats', '0 * * * *', 
                    'SELECT c77_dbh_update_vacuum_stats()');

-- Update index stats hourly
SELECT cron.schedule('update_index_stats', '0 * * * *', 
                    'SELECT c77_dbh_update_index_stats()');

🔄 Update Frequency Recommendations

Database Characteristics Recommended Update Frequency
Mixed read/write Every 6 hours
Read-heavy, stable Daily
Development/staging On-demand or daily

🖥️ Dashboard Integration

The JSON output format makes integration with monitoring dashboards straightforward:

📊 Grafana

Use a PostgreSQL data source with JSON parsing to create rich visualizations

📈 Pganalyze

Import as custom metrics for comprehensive database monitoring

🎯 Custom Dashboards

Use the structured JSON for consistency across different monitoring platforms

🔧 Advanced Configuration

Permissions Setup

-- Grant read access to a reporting role
GRANT SELECT ON ALL TABLES IN SCHEMA public TO reporting_role;

-- Grant full access to admin roles
GRANT ALL ON ALL TABLES IN SCHEMA public TO admin_role;

Schema Backup Integration

# Include in schema backups
pg_dump -s -n public mydatabase > schema.sql

📊 Technical Details

The extension consists of:

Tables

Functions

Event Triggers

🎯 Health Metrics Deep Dive

Table Vacuum Health

📊 What's Monitored

  • Bloat percentage: Ratio of dead tuples to live tuples
  • TOAST bloat: Overhead from large object storage
  • Vacuum frequency: Last vacuum and autovacuum timestamps
  • Statistical thresholds: Dynamic thresholds based on database patterns

Index Health Analysis

📈 Usage Metrics

  • Index scan frequency
  • Tuple read efficiency
  • Sequential scan vs index scan ratios

💾 Bloat Detection

  • Index size vs expected size
  • Fill factor analysis
  • Fragmentation indicators

🎯 Recommendations

  • Unused index identification
  • Duplicate index detection
  • REINDEX suggestions

Materialized View Performance

-- Example materialized view health output
{
  "metric": "Materialized View Performance",
  "view_name": "public.sales_summary",
  "status": "Yellow",
  "severity": 3,
  "insight": "Refresh times trending upward; consider optimization",
  "action": "Review refresh query performance and consider incremental refresh",
  "last_refresh": "2025-06-12 08:30:15",
  "refresh_duration": "00:03:42",
  "rows_affected": 1234567,
  "trend": "increasing",
  "details": {
    "avg_refresh_time": "00:02:45",
    "max_refresh_time": "00:05:12",
    "bottleneck": "I/O intensive operations"
  }
}

🔍 Statistical Analysis

Dynamic Thresholds

The extension uses statistical process control principles to establish intelligent thresholds:

📊 Threshold Calculation

  • Baseline establishment: Uses historical data to determine normal operating ranges
  • Standard deviation analysis: Identifies outliers based on statistical variation
  • Adaptive adjustments: Thresholds evolve with database growth and usage patterns
  • Context awareness: Different thresholds for different table sizes and types

Severity Scoring

Health issues are scored on a 1-5 scale:

🚀 Performance Optimization

Efficient Data Collection

-- Optimize statistics collection for large databases
-- The extension automatically uses sampling for large tables

-- Check collection performance
SELECT 
    table_name,
    collection_time,
    sample_size,
    accuracy_estimate
FROM c77_dbh_collection_stats 
ORDER BY collection_time DESC;

Resource Management

⚡ Low Overhead Design

  • Minimal impact on production workloads
  • Efficient sampling algorithms
  • Batch processing for multiple tables

📊 Configurable Precision

  • Automatic fallbacks when extensions unavailable
  • Graceful degradation of precision
  • Cost-based analysis selection

🛠️ Troubleshooting

Common Issues

🚨 No Data in Health Views

Ensure statistics collection has been run:

-- Check if statistics have been collected
SELECT COUNT(*) FROM c77_dbh_vacuum_stats;
SELECT COUNT(*) FROM c77_dbh_index_stats;

-- If empty, run collection manually
SELECT c77_dbh_update_vacuum_stats();
SELECT c77_dbh_update_index_stats();

Performance Tuning

🎯 Integration Examples

Grafana Dashboard Query

-- Query for Grafana visualization
SELECT 
    table_name,
    CASE status 
        WHEN 'Red' THEN 3
        WHEN 'Yellow' THEN 2
        WHEN 'Green' THEN 1
    END as status_code,
    severity,
    insight
FROM c77_dbh_get_vacuum_health()
WHERE status != 'Green'
ORDER BY severity DESC;

Alert Integration

-- Query for alerting systems
SELECT 
    'Database Health Alert' as alert_type,
    table_name,
    status,
    severity,
    insight,
    action,
    NOW() as alert_time
FROM c77_dbh_get_vacuum_health()
WHERE severity >= 4
UNION ALL
SELECT 
    'Index Health Alert',
    index_name,
    status,
    severity,
    insight,
    action,
    NOW()
FROM c77_dbh_get_index_health()
WHERE severity >= 4;

🔄 Maintenance

Regular Maintenance Tasks

Statistics Cleanup

-- Clean up old statistics (older than 30 days)
DELETE FROM c77_dbh_vacuum_stats 
WHERE collected_at < NOW() - INTERVAL '30 days';

DELETE FROM c77_dbh_index_stats 
WHERE collected_at < NOW() - INTERVAL '30 days';

Intelligent database health monitoring made simple!

This extension brings enterprise-grade database monitoring directly into PostgreSQL, providing actionable insights without the complexity of external tools.

View Git Repository →