Problem Statement
Database performance degrades over time as data grows and query patterns change, often due to missing or unused indexes. An index health monitoring service must be built that analyzes PostgreSQL index usage statistics (pg_stat_user_indexes, pg_stat_all_tables), identifies unused indexes (candidates for removal), recommends missing indexes based on sequential scan patterns, and generates monthly optimization reports.
Technical Bounds
- Analysis interval: daily cron (off-peak hours).
- Indexes analyzed:
idx_scan, idx_tup_read, idx_tup_fetch from pg_stat_user_indexes.
- Unused threshold:
idx_scan < 10 in 30 days -> candidate for removal; notify DBA.
- Missing index detection: find queries with sequential scans on large tables (> 10K pages) and suggest indexes on filter columns.
- Report: monthly PDF/email with index usage summary, unused candidates, and recommended new indexes.
- Safety: recommended index DDL is reviewed; never auto-execute DROP INDEX.
Steps
- Implement analyzer: query
pg_stat_user_indexes, compute scan frequency, identify unused indexes.
- Implement missing index detector: analyze
pg_stat_statements for sequential scans on large tables; extract filter columns.
- Store results in
index_health_reports table: (table_name, index_name, scans_30d, size_mb, recommendation).
- Generate monthly report: PDF with tables and recommendations.
- Add API:
GET /api/v1/db/index-health returns current recommendations.
Problem Statement
Database performance degrades over time as data grows and query patterns change, often due to missing or unused indexes. An index health monitoring service must be built that analyzes PostgreSQL index usage statistics (
pg_stat_user_indexes,pg_stat_all_tables), identifies unused indexes (candidates for removal), recommends missing indexes based on sequential scan patterns, and generates monthly optimization reports.Technical Bounds
idx_scan,idx_tup_read,idx_tup_fetchfrompg_stat_user_indexes.idx_scan < 10in 30 days -> candidate for removal; notify DBA.Steps
pg_stat_user_indexes, compute scan frequency, identify unused indexes.pg_stat_statementsfor sequential scans on large tables; extract filter columns.index_health_reportstable:(table_name, index_name, scans_30d, size_mb, recommendation).GET /api/v1/db/index-healthreturns current recommendations.