Skip to content

Conversation

@jackschultz
Copy link
Owner

Summary

Implements the diagnose → fix → verify workflow for pgcrate:

  • pgcrate fix sequence - Upgrade sequence types (smallint → integer → bigint) to prevent exhaustion
  • pgcrate fix index --drop - Safely drop unused/duplicate indexes with comprehensive safety checks
  • pgcrate fix vacuum - Trigger vacuum operations (regular, freeze, full, analyze)
  • pgcrate vacuum - New diagnostic for vacuum/bloat health
  • pgcrate triage --include-fixes - Get structured fix recommendations in triage output

Safety Model

All fix commands follow a gate system:

  • --read-write required for any mutations
  • --primary required to confirm connection to primary
  • --yes required for medium/high risk operations (DROP INDEX, VACUUM FULL)
  • Dry-run by default - shows exact SQL without executing

Risk Levels

Operation Risk Confirmation
ALTER SEQUENCE Low No
VACUUM / VACUUM FREEZE / VACUUM ANALYZE Low No
DROP INDEX CONCURRENTLY Medium Yes
VACUUM FULL High Yes (ACCESS EXCLUSIVE lock)

Key Files

  • src/commands/fix/ - Fix command implementations
  • src/commands/vacuum.rs - Vacuum diagnostic
  • src/commands/triage.rs - Enhanced with --include-fixes
  • tests/diagnostics/fix.rs - 16 integration tests

Test plan

  • cargo fmt --check passes
  • cargo clippy -- -D warnings passes
  • 377 bin tests pass
  • 122 integration tests pass (16 new fix tests)
  • Execution test verifies actual database mutation works
  • Special identifier tests (reserved words, quoted names)

Implements the diagnose→fix→verify loop for database remediation:

Vacuum diagnostic (pgcrate vacuum):
- Table bloat analysis using pg_stat_user_tables
- Optional pgstattuple integration for accurate measurements
- Thresholds: warning at 10%, critical at 25% or 1M dead tuples

Fix commands:
- pgcrate fix sequence: Upgrade sequence types (smallint→int→bigint)
- pgcrate fix index --drop: Safely drop unused indexes (CONCURRENTLY)
- pgcrate fix vacuum: Run VACUUM with options (freeze, full, analyze)

Gate system for safety:
- --read-write: Required for all fixes
- --primary: Required for DB-modifying operations
- --yes: Required for medium/high risk operations

Verification runner:
- Post-fix validation via JSONPath conditions
- Runs diagnostic commands and checks expected results

Triage enhancements:
- --include-fixes flag generates structured fix actions
- Actions include SQL preview, evidence, gates, and verify steps

Enhanced index evidence:
- stats_since, stats_age_days for usage confidence
- is_replica_identity, backing_constraint for safety checks
Tests cover:
- fix sequence: dry-run, gates, JSON output, downgrade blocking
- fix index: dry-run, primary key blocking, JSON output
- fix vacuum: dry-run, FULL requires --yes, JSON output
- vacuum diagnostic: JSON structure
- triage --include-fixes: actions array present
README.md:
- Add vacuum to diagnostics section
- Add Fix Commands section with examples
- Update Commands table

CHANGELOG.md:
- Document Phase 2a features under Unreleased

llms.txt:
- Add DIAGNOSTICS section with health check commands
- Add Fix Commands section with gate flags and risk levels
- Update JSON supported commands list
Review fixes:
- Replace duplicate quote_ident with crate::sql::quote_ident (safer always-quoting version)
- Extract common print_fix_result helper to fix/common.rs
- Remove unused parameters from get_verify_steps functions
- Add execution test that verifies sequence upgrade works
- Add tests for special identifiers (reserved words, quoted names)

Net reduction of ~90 lines while adding 3 new tests.
@jackschultz jackschultz merged commit e53ecfb into main Jan 19, 2026
8 checks passed
@jackschultz jackschultz deleted the feature/phase2a-fix-commands branch January 19, 2026 17:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants