Skip to content

About

Adaptive PostgreSQL 18 query plan regression gate & counterfactual index advisor using pglast, hypopg, HOT-aware write modeling, and FastAPI.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

QueryPulse

Adaptive PostgreSQL 18 Query Plan Regression Gate & Counterfactual Index Advisor

QueryPulse is a local-first PostgreSQL performance engineering CLI and interactive web explorer built in Python 3.12. It combines real libpg_query SQL AST analysis (pglast), hardware-invariant EXPLAIN (ANALYZE, BUFFERS) plan-tree differencing, zero-lock hypopg counterfactual index evaluation, HOT-update write-amplification modeling, and physical shadow replay verification.

Live Explorer & Playground: https://querypulse-explorer.onrender.com


Measured PostgreSQL 18.7 Benchmark Results

Reproducible end-to-end benchmark (querypulse benchmark --rows 25000 across 25,000 orders and 50,000 line items on Dockerized PostgreSQL 18.7 + hypopg):

Stage / Metric Measured Result
Injected Regression Gate FAIL (SEQ_SCAN_FLIP, 37.27x shared buffer explosion)
Baseline Solver (knapsack) -100.0% estimated cost (12,296.0 KiB — over-indexes overlapping candidates)
Interactive Solver (extend_kmn) -96.0% estimated cost (2,160.0 KiB, 4 indexes — 5.7x smaller footprint)
Unused Physical Index Scan (hypopg_hide_index) 1 / 1 planted redundant index detected
Physical Shadow Replay Verification (3 rounds) -96.2% p95 latency | -69.5% shared buffers | 1,256.0 KiB physical on disk

Core Architecture

flowchart LR
    SRC["SQL Logs / .sql<br/>pg_stat_statements"]
    S1["1. Ingestion & AST<br/>(pglast, PII scrub,<br/>HOT write profile)"]

    S2["2. Telemetry<br/>EXPLAIN BUFFERS<br/>q-error & PG 18"]
    S3["3. Plan-Tree Differ<br/>SEQ_SCAN_FLIP<br/>TEMP_SPILL"]
    CI["CI/CD Gate<br/>CLI / MD / SARIF"]

    S4["4. Synthesizer<br/>B-Tree, INCLUDE,<br/>Partial & GIN"]
    S5["5. hypopg & Solvers<br/>Knapsack vs.<br/>Extend+KMN"]
    S6["6. Shadow Verifier<br/>Physical build &<br/>p95 / buffer check"]

    UI["7. SQLite WAL +<br/>FastAPI Explorer UI"]

    SRC --> S1
    S1 --> S2 --> S3 --> CI
    S1 --> S4 --> S5 --> S6
    S3 --> UI
    S5 --> UI
    S6 --> UI
Loading

Key Engineering Design Choices

  • Hardware-Invariant CI Gating: Instead of flaky wall-clock latency assertions on shared CI runners, querypulse diff gates PRs on deterministic plan topology, shared/temp buffer ratios, disk spills, cardinality $q$-error, and PostgreSQL 18 Index Searches (exit code 1 on FAIL, SARIF 2.1.0 + GitHub PR Markdown output).
  • Strict Read-Only & Rollback Guardrails: Production/target connections enforce SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY, statement_timeout, and lock_timeout. DML EXPLAIN ANALYZE calls on shadow databases execute strictly inside rolled-back SAVEPOINT blocks.
  • HOT-Update Breakage Modeling: Indexes covering frequently UPDATEd columns that are not yet indexed permanently disable PostgreSQL Heap-Only Tuple (HOT) updates. QueryPulse weights unindexed updated columns separately from already-indexed columns and surfaces HOT impact per recommendation.
  • Submodular Multi-Index Selection (extend_kmn): Keeps chosen hypothetical indexes active inside a pinned hypopg session across greedy rounds so PostgreSQL's real planner accounts for overlapping index interaction rather than summing isolated gains.

Quick Start

1. Install (Python 3.12+)

python -m venv .venv
# Windows PowerShell: .\.venv\Scripts\Activate.ps1
# Linux/macOS:        source .venv/bin/activate
pip install -e ".[dev]"

2. Start the Local PostgreSQL 18 + hypopg Docker Harness

docker compose up -d --build
querypulse doctor

3. Run the End-to-End Benchmark & Launch Explorer UI

querypulse benchmark --rows 25000
querypulse ui
# Open [http://127.0.0.1:8765](http://127.0.0.1:8765) in your browser

CLI Reference

Command Purpose
querypulse doctor Verify SQLite WAL store, PG 18 version, read-only guardrail, hypopg, and pg_stat_statements
querypulse ingest -n NAME -s sql_file -f workload.sql Parse, scrub PII, fingerprint, and store a workload (supports sql_file, log, pg_stat)
querypulse snapshot -l LABEL -f queries.sql Capture normalized EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) plans
querypulse diff -b main -c pr-42 -F sarif -o diff.sarif Diff two snapshots (table, md, json, sarif); exits 1 on critical regressions
querypulse advise -w NAME -s extend_kmn --budget-mb 50 Synthesize and optimize indexes under storage + HOT write-penalty constraints
querypulse verify -a ADVICE_ID -f queries.sql Physically materialize advised indexes on shadow PG 18 and verify p95 & buffer drops
querypulse benchmark --rows 25000 Seed multi-table schema on shadow DB and benchmark the full pipeline
querypulse ui --port 8765 Launch the local FastAPI + Interactive Plan-Diff, Pareto Frontier & Live Playground UI

Verification & Quality Checks

ruff check src tests
mypy src
pytest tests/unit -v
pytest tests/integration -v -m integration

GitHub Actions (.github/workflows/ci.yml) runs ruff, mypy --strict, all unit tests, and all live Dockerized PostgreSQL 18 + hypopg integration tests on every push and pull request.

About

Adaptive PostgreSQL 18 query plan regression gate & counterfactual index advisor using pglast, hypopg, HOT-aware write modeling, and FastAPI.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages