cockroachdb / cockroachdb/cockroach

opt: re-evaluate generic vs custom plan decision per execution

Open
#172,169 0 comments 0 reactions 0 assignees View on GitHub
A-generic-query-plans A-sql-optimizer C-enhancement T-sql-queries
Dominant language
Go
Stars
32.5k
Forks
4.1k
PR merge metrics
PR metrics pending

Description

Currently with `plan_cache_mode=auto`, we make a one-time decision for custom/generic plan pick: if the generic plan cost < average custom plan cost, we use the generic plan from then on. This means we can get stuck with a generic plan that's good on average but bad for specific parameter values, or miss out on a good generic plan because the cost comparison was too conservative.

One idea that @DrewKimball proposed is to statically identify "safe" generic plans where regression is unlikely. For example, if all placeholder filters were successfully incorporated into constrained scans or lookup joins, the generic plan is unlikely to be much worse than a custom plan — we could be more aggressive about choosing it in those cases.

Another idea that @michae2 proposed is to re-cost the generic plan with actual parameter values on each execution and fall back to a custom plan if the cost looks significantly worse. A more ambitious version would cache multiple plans per query and pick the best match for a given set of parameter values, with stochastic exploration to discover better plans over time.

@mw5h also suggested that we could use feedback from actual execution of generic plans to adjust the estimated cost of the generic plan over time, rather than relying purely on the cost model. This would let the system learn whether a generic plan is actually performing well and make better decisions going forward.

Related: #127826, #151373, #158273

Epic CRDB-57272

Contributor guide

Open the contributing guide

Research direction

The issue names no files, tests, or entry points. Start by tracing the plan_cache_mode=auto generic/custom plan decision and read related issues #127826, #151373, and #158273. Clarify which proposed strategy is intended before implementing; done should include a measurable improvement in choosing plans for parameter values without regressing generic-plan performance.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.