Skip to content

Integration EXPLAIN Plan Parser + Rule-Based Suggestion Engine - #112

Open
sarthaksarthak9 wants to merge 2 commits into
BNLNPPS:mainfrom
sarthaksarthak9:feat/rule-based-engine
Open

Integration EXPLAIN Plan Parser + Rule-Based Suggestion Engine#112
sarthaksarthak9 wants to merge 2 commits into
BNLNPPS:mainfrom
sarthaksarthak9:feat/rule-based-engine

Conversation

@sarthaksarthak9

@sarthaksarthak9 sarthaksarthak9 commented Jul 7, 2026

Copy link
Copy Markdown

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 typed PlanNode model, 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, and Suggestion models. 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 via suggestion_hash, enabling idempotent persistence and preventing duplicate entries across repeated analyses.

Finally, the analysis pipeline is integrated into the collector workflow. Collected EXPLAIN plan JSON is parsed, evaluated against the rule engine using both the execution plan and runtime context, and the generated optimization suggestions are persisted in the ai_optimizer.suggestions table.

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.

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.

1 participant