Integration EXPLAIN Plan Parser + Rule-Based Suggestion Engine - #112
Open
sarthaksarthak9 wants to merge 2 commits into
Open
Integration EXPLAIN Plan Parser + Rule-Based Suggestion Engine#112sarthaksarthak9 wants to merge 2 commits into
sarthaksarthak9 wants to merge 2 commits into
Conversation
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.
Description
This implements AI query optimization pipeline by adding deterministic EXPLAIN plan analysis and rule-based suggestion generation for PostgreSQL queries.
The implementation introduces a structured parser for PostgreSQL
EXPLAIN (FORMAT JSON)output, including a typedPlanNodemodel, recursive parsing logic, and helper utilities for traversing execution plan trees. This provides a consistent representation of query execution plans that can be analyzed programmatically.On top of the parsed plan tree, it adds a rule-based analysis engine consisting of
RuleEngine,RuleContext, andSuggestionmodels. The engine evaluates execution plans using rules R1–R13 to identify common performance issues and generate optimization recommendations. It also includes deduplication logic to prevent duplicate suggestions from being stored.To improve reliability and safety, the PR introduces SQL validation through
validate_safe_sql, ensuring that only allowlisted SQL statements are analyzed. It also adds deterministic suggestion hashing viasuggestion_hash, enabling idempotent persistence and preventing duplicate entries across repeated analyses.Finally, the analysis pipeline is integrated into the collector workflow. Collected
EXPLAINplan JSON is parsed, evaluated against the rule engine using both the execution plan and runtime context, and the generated optimization suggestions are persisted in theai_optimizer.suggestionstable.Additionally, this phase includes accuracy refinements by ensuring that GlobalTag-related checks are only evaluated for applicable queries, preventing false-positive recommendations. Rule R13 has also been updated to generate advisory suggestion text without relying on unresolved SQL placeholders, resulting in clearer and more reliable optimization guidance.