feat(database): add automatic index optimizer#660
Open
distributed-nerd wants to merge 1 commit into
Open
Conversation
Implements a PostgreSQL index optimizer that recommends, creates, monitors and retires indexes from the catalog and pg_stat_* views. - Query analysis for index recommendations: QueryAnalysisService finds foreign-key columns lacking a covering index (Postgres does not index FK columns automatically), scores/prioritises them by seq-scan activity from pg_stat_user_tables, and surfaces slow statements from pg_stat_statements when available. - Automatic index creation: IndexCreationService applies recommendations via CREATE INDEX CONCURRENTLY IF NOT EXISTS, caps creations per run, verifies indisvalid and drops invalid concurrent builds. - Index usage monitoring: IndexUsageMonitorService samples pg_stat_user_indexes scan counts/sizes and classifies indexes (primary/unique/constraint-backed). - Stale index removal: StaleIndexService drops unused, sufficiently large indexes via DROP INDEX CONCURRENTLY, never touching primary, unique or constraint-backed indexes. - IndexOptimizationService orchestrates the cycle on a weekly @Cron, inert unless INDEX_OPT_ENABLED=true; every destructive action is gated behind dry-run and explicit auto-create/auto-drop flags. Exposes admin-only endpoints under /database/index-optimization and wires the module plus ScheduleModule.forRoot() into AppModule.
|
@distributed-nerd Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a PostgreSQL automatic index optimizer (new module
src/database/index-optimization/) covering all acceptance criteria of the issue. All analysis is read-only; the only writes are explicitCREATE INDEX/DROP INDEX, both gated behind configuration flags.QueryAnalysisServicefinds foreign-key columns lacking a covering index (Postgres does not index FK columns automatically), scores/prioritises them bypg_stat_user_tablesseq-scan activity, and surfaces slow statements frompg_stat_statementswhen available.GET .../recommendations,GET .../slow-queriesIndexCreationServiceapplies recommendations viaCREATE INDEX CONCURRENTLY IF NOT EXISTS, caps creations per run, verifiesindisvalidand drops failed concurrent buildsIndexUsageMonitorServicesamplespg_stat_user_indexesscan counts/sizes and classifies indexes (primary/unique/constraint-backed).GET .../usageStaleIndexServicedrops unused, sufficiently large indexes viaDROP INDEX CONCURRENTLY, never touching primary, unique or constraint-backed indexes.GET .../staleSafety
IndexOptimizationServiceorchestrates the cycle on a weekly@Cron, inert unlessINDEX_OPT_ENABLED=true.INDEX_OPT_AUTO_CREATE/INDEX_OPT_AUTO_DROP_STALEflags (all default off).POST /database/index-optimization/run?apply=true; admin-only endpoints.IndexOptimizationModule+ScheduleModule.forRoot()wired intoAppModule. Seesrc/database/index-optimization/README.md.Closes #647