Skip to content

Skip empty ordering scans for a scoped numeric query - #2908

Merged
stopachka merged 1 commit into
mainfrom
codex/scoped-upper-bound-plan
Sep 13, 2026
Merged

stopachka merged 1 commit into
mainfrom
codex/scoped-upper-bound-plan

Conversation

@stopachka

Copy link
Copy Markdown
Contributor

An allowlisted numeric query can scan the full ordering index when its filter has no matches. Add a one-row existence probe inside the same SQL statement so that PostgreSQL can skip that empty join. The original joins, result envelope and pagination remain in place; nonempty requests continue through the original plan.

The override requires the exact app and complete normalized query shape plus the measured schema, scan choices and SQL layout. Existing hint-disable controls bypass it. The precheck copies the filter predicates, so correctness does not depend on cached cardinality metadata remaining current until execution.

Validation:

  • Eight tests and 86 assertions pass, including complete-result/page comparisons, null/missing values, ties, scope and hint switches, and a stale-schema regression.
  • Final-code synthetic benchmark with 20,000 entities and verified applied hints: empty range 27.85ms→0.06ms; shared-buffer hits 101,432→3, with the original scans never executed.
  • Nonempty bounds retain the original scans and add four buffer hits; no measured slowdown across the tested cases. These are local measurements, not production savings.
  • Independent review, clj-kondo and git diff --check pass.

@coderabbitai

coderabbitai Bot commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: c1095bee-d280-41dd-8762-1529a87e0e68

📥 Commits

Reviewing files that changed from the base of the PR and between cddd016 and fe4a857.

📒 Files selected for processing (2)
  • server/src/instant/db/scoped_query_plans.clj
  • server/test/instant/db/numeric_range_plan_test.clj

Included review availability: Your plan provides up to 10 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

The query planner adds a guarded upper-bound rewrite for a specific timestamp query shape. Tests verify the generated CTE and hints, rewrite gates, feature flags, and equivalence of paged results.

Changes

Numeric upper-bound planning

Layer / File(s) Summary
Upper-bound plan rewrite
server/src/instant/db/scoped_query_plans.clj
Adds numeric-upper-bound, which validates the measured query shape and adds an upper-bound CTE, an :exists predicate, and an index-scan hint. apply-plan tries this rewrite after numeric-range.
Rewrite validation and result checks
server/test/instant/db/numeric_range_plan_test.clj
Adds the upper-bound query fixture and tests for plan structure, exact matching conditions, hint switches, query-hash flags, schema changes, boundary values, ties, and paged-result equality.

Priority: ⬇️ Low

Estimated code review effort: 3 (Moderate) | ~30 minutes

Change: Refactor

Sequence Diagram(s)

sequenceDiagram
  participant QueryCompiler
  participant apply-plan
  participant numeric-upper-bound
  participant PostgreSQL
  QueryCompiler->>apply-plan: compile timestamp upper-bound query
  apply-plan->>numeric-upper-bound: validate query and scan layout
  numeric-upper-bound-->>apply-plan: return upper-bound CTE and exists predicate
  apply-plan-->>QueryCompiler: return rewritten query plan
  QueryCompiler->>PostgreSQL: execute rewritten SQL
  PostgreSQL-->>QueryCompiler: return paged results
Loading

Merge Risk: ⚪ Minimal · up to fe4a8

The narrowly scoped query optimization retains equivalent paged results in the covered cases and respects existing hint-disable controls. No actionable merge risk remains.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: skipping empty ordering scans for scoped numeric queries.
Description check ✅ Passed The description directly explains the existence-probe optimization, its scope, safeguards, unchanged behavior, and validation results.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 0…
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@stopachka
stopachka marked this pull request as ready for review September 13, 2026 05:13
@stopachka
stopachka merged commit 2cc9ef9 into main Sep 13, 2026
34 checks passed
@stopachka
stopachka deleted the codex/scoped-upper-bound-plan branch September 13, 2026 05:13
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