Skip to content

fix(inkless:control_plane): force generic find batches planning - #786

Closed
giuseppelillo wants to merge 1 commit into
mainfrom
glillo/fix-find-batches-query-plan
Closed

giuseppelillo wants to merge 1 commit into
mainfrom
glillo/fix-find-batches-query-plan

Conversation

@giuseppelillo

@giuseppelillo giuseppelillo commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

find_batches_v2 runs its batches-to-files lookup once for every requested partition. With automatic plan caching, a PostgreSQL backend can select a custom plan for that statement and invoke the planner on every lookup, even though each invocation produces the same ordered index-scan plan. Planning then dominates replica CPU and makes performance depend on backend-local plan-cache history.

plan_cache_mode=auto (default) normally switches to a generic plan when its estimated cost is competitive with the average custom-plan cost. For this parameterized query, benchmarks showed that in some cases PostgreSQL keeps replanning equivalent index scans instead of reliably selecting the reusable generic plan.

Set plan_cache_mode to force_generic_plan at the function boundary to bypass that misleading cost decision. PostgreSQL now builds and reuses one generic inner plan per backend while find_batches_v2 runs. The setting does not affect other control-plane statements, and the generic plan preserves the last-offset index order and budget-bounded early exit.

The migration test checks the function catalog configuration so a future function replacement cannot silently remove the setting.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The focused migration and regression test introduce no unresolved issues.

Pull request overview

Forces reusable generic PostgreSQL plans for find_batches_v2, reducing repeated planning overhead.

Changes:

  • Sets function-level plan_cache_mode=force_generic_plan.
  • Verifies the setting through PostgreSQL’s function catalog.
File summaries
File Description
storage/inkless/src/test/java/io/aiven/inkless/control_plane/postgres/FindBatchesJobTest.java Tests the function-level plan configuration.
storage/inkless/src/main/resources/db/migration/V28__Find_batches_generic_plan.sql Enables generic plan reuse for find_batches_v2.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

`find_batches_v2` runs its `batches`-to-`files` lookup once for every requested partition. With automatic plan caching, a PostgreSQL backend can select a custom plan for that statement and invoke the planner on every lookup, even though each invocation produces the same ordered index-scan plan. Planning then dominates replica CPU and makes performance depend on backend-local plan-cache history.

`plan_cache_mode=auto` (default) normally switches to a generic plan when its estimated cost is competitive with the average custom-plan cost. For this parameterized query, benchmarks showed that in some cases PostgreSQL keeps replanning equivalent index scans instead of reliably selecting the reusable generic plan.

Set `plan_cache_mode` to `force_generic_plan` at the function boundary to bypass that misleading cost decision. PostgreSQL now builds and reuses one generic inner plan per backend while `find_batches_v2` runs. The setting does not affect other control-plane statements, and the generic plan preserves the last-offset index order and budget-bounded early exit.

The migration test checks the function catalog configuration so a future function replacement cannot silently remove the setting.
@giuseppelillo
giuseppelillo force-pushed the glillo/fix-find-batches-query-plan branch from 6081491 to bde1953 Compare September 3, 2026 17:54
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.

2 participants