feat(H001): add decision tree recommendations for invalid indexes
Summary
Extends H001 Invalid Indexes report with a decision tree to recommend whether to DROP, RECREATE, or flag as UNCERTAIN for each invalid index.
Decision Tree
┌───────────────────────────────────────────┐
│ Is there a valid index on same column(s)? │
└─────────────────────┬─────────────────────┘
┌──────────┴──────────┐
YES NO
│ │
▼ ▼
┌────────────────┐ ┌─────────────────────────┐
│ DROP index │ │ Backs a constraint? │
│ (duplicate) │ │ (UNIQUE / PK) │
└────────────────┘ └────────────┬────────────┘
┌────────┴────────┐
YES NO
│ │
▼ ▼
┌────────────┐ ┌─────────────────────┐
│ RECREATE │ │ Table < 10K rows? │
└────────────┘ └──────────┬──────────┘
┌───────┴───────┐
YES NO
│ │
▼ ▼
┌─────────────┐ ┌─────────────┐
│ DROP │ │ UNCERTAIN │
│ monitor │ │ needs RCA │
└─────────────┘ └─────────────┘
New Fields
| Field | Description |
|---|---|
is_pk |
True if index backs PRIMARY KEY |
is_unique |
True if index is UNIQUE |
constraint_name |
Constraint name if any |
table_row_estimate |
Estimated rows from reltuples |
has_valid_duplicate |
Valid index on same columns exists |
valid_duplicate_name |
Name of valid duplicate index |
valid_duplicate_definition |
CREATE INDEX statement of valid duplicate |
Note
Recommendations (DROP, RECREATE, UNCERTAIN) and commands (drop_command, reindex_command) are computed at render time based on the decision tree, not stored in JSON.
Changes
-
config/pgwatch-prometheus/metrics.yml: Extended SQL query to gather decision data -
reporter/schemas/H001.schema.json: Extended schema with new fields -
cli/lib/checkup.ts: Express mode decision logic -
reporter/postgres_reports.py: Full mode decision logic
Test Plan
- Verify H001 report generates correctly in full mode
- Verify H001 report generates correctly in express mode
- Test with invalid index that has valid duplicate → should recommend DROP
- Test with invalid PK/UNIQUE index → should recommend RECREATE
- Test with invalid index on small table (< 10K rows) → should recommend DROP
- Test with invalid index on large table → should recommend UNCERTAIN
- Verify JSON schema validation passes
Edited by Bogdan Tsechoev