Skip to content

Reuse matched section IDs for scoped calendar child queries - #2913

Draft
stopachka wants to merge 2 commits into
codex/scoped-5ff-write-planfrom
codex/reassign-calendar-child-ids
Draft

stopachka wants to merge 2 commits into
codex/scoped-5ff-write-planfrom
codex/reassign-calendar-child-ids

Conversation

@stopachka

@stopachka stopachka commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Reassign’s calendar query scans triples again to collect child entity IDs that its OR branches already found. Replace that intermediate scan with a distinct projection of the existing IDs. Directed Leading and NestLoop hints retain the child join order, but production plan validation found that they do not reliably preserve the entity-bound inner index scan.

The change requires the exact app, normalized query, compiler structure and existing child index hint. It defaults off behind scoped-query-plans / reassign-calendar-child-ids, respects the kill switches, and falls back when the guards differ. Returned CTEs, filters, permissions, row multiplicities and invalidation topics are preserved. The hint formatter gains nested Leading pairs; existing flat hints use the same formatting path.

Validation: 23 targeted tests and 275 assertions passed on PostgreSQL 17.10 with pg_hint_plan 1.7.1; clj-kondo is clean. Differential tests cover complete results, permissions, tenant isolation, overlapping branches, empty ranges, dangling children and many-valued dates. Plan tests verify an entity-bound child index lookup for sparse, empty and broad ranges.

A 7,960-triple synthetic benchmark preserved complete results for all three ranges. Local buffer hits fell from 1,447 to 952 for the sparse range and from 15,533 to 14,734 for the broad range; the empty range was unchanged. These measurements do not establish production CPU savings. The change is not deployed or enabled.

Rollout is blocked. A read-only EXPLAIN (ANALYZE FALSE) on the current Aurora writer, using the current compiler and fresh sketches, produced a materialized inner t8 scan bound by app and attribute but not entity. The baseline retained all three index conditions. This gap needs a regression test and a structural fix before activation. No production workload was executed by the probe, and the calendar change remains undeployed and disabled.

Stacked on #2912. The separate release candidate preserves all subsequent production changes and passed 43 tests / 459 assertions, but its local plan tests do not cover the production planner choice above.

@coderabbitai

coderabbitai Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

Understand this PR’s impact

Explore downstream dependencies and potential security impact with Blast Radius.

View blast radius →

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: bb0675ac-5223-4168-9ca5-cb590970ba3d

📥 Commits

Reviewing files that changed from the base of the PR and between 05ee981 and 40b5995.

📒 Files selected for processing (5)
  • server/src/instant/db/scoped_query_plans.clj
  • server/src/instant/util/hsql.clj
  • server/test/instant/db/reassign_calendar_child_ids_integration_test.clj
  • server/test/instant/db/reassign_calendar_child_ids_test.clj
  • server/test/instant/util/hsql_test.clj

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


📝 Walkthrough

Walkthrough

The change adds an opt-in scoped query-plan override for one calendar query shape. The override validates the compiled query, rewrites an intermediate collector, and adds child-lookup hints. Unit and integration tests compare projected plans with baseline results.

Changes

Calendar query projection

Layer / File(s) Summary
Nested join hint formatting
server/src/instant/util/hsql.clj, server/test/instant/util/hsql_test.clj
Adds formatting for nested Leading join pairs and tests valid and malformed hint structures.
Scoped query-plan implementation
server/src/instant/db/scoped_query_plans.clj
Adds the guarded reassign-calendar-child-ids plan. The plan validates flags, query shape, CTE structure, child attributes, and the t8 scan before replacing m-7 and adding join hints.
Compilation contract coverage
server/test/instant/db/reassign_calendar_child_ids_test.clj
Tests exact activation, hint switches, query-shape requirements, compiler-drift fallback, CTE preservation, hint augmentation, and schema-dependent compilation.
Integration result coverage
server/test/instant/db/reassign_calendar_child_ids_integration_test.clj
Compares baseline and projected results across date ranges, permission rules, tenant data, empty branches, query plans, and many-valued date joins.

Priority: ⬇️ Low

Estimated code review effort: 4 (Complex) | ~45 minutes

Change: Refactor

Sequence Diagram(s)

sequenceDiagram
  participant CalendarQueryCompiler
  participant apply_plan
  participant reassign_calendar_child_ids
  participant sql_select_arrays
  CalendarQueryCompiler->>apply_plan: compile calendar query with CTEs
  apply_plan->>reassign_calendar_child_ids: apply configured app-specific plan
  reassign_calendar_child_ids->>reassign_calendar_child_ids: validate flags, query shape, CTEs, and child lookup scan
  reassign_calendar_child_ids-->>apply_plan: return rewritten CTEs and join hints
  apply_plan->>sql_select_arrays: execute projected SQL
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
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.
Title check ✅ Passed The title clearly and concisely describes the main change: reusing matched section IDs for scoped calendar child queries.
Description check ✅ Passed The description directly explains the query-plan optimization, safeguards, validation, rollout status, and known production-planner limitation.

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 draft September 21, 2026 07:39

This branch has not been deployed

No deployments
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