Skip to content

opt: add optimizer_span_limit and use in GenerateConstrainedScans#167180

Merged
trunk-io[bot] merged 2 commits intocockroachdb:masterfrom
michae2:michae2/optimizer-span-limit
Apr 17, 2026
Merged

opt: add optimizer_span_limit and use in GenerateConstrainedScans#167180
trunk-io[bot] merged 2 commits intocockroachdb:masterfrom
michae2:michae2/optimizer-span-limit

Conversation

@michae2
Copy link
Copy Markdown
Collaborator

@michae2 michae2 commented Mar 31, 2026

opt: add optimizer_span_limit and use in GenerateConstrainedScans

This commit creates a new session variable optimizer_span_limit, and
uses it to bound the number of spans in constrained scans produced by
GenerateConstrainedScans.

Fixes: #167620

Release note: Add a new session variable optimizer_span_limit which
bounds the number of spans the optimizer will allow in a single
constrained index scan. If a single IN set has more items than this
limit, that IN set will not be used to build a constrained index
scan. If the cross product of two or more IN sets would produce more
spans than this limit for a composite index, then only a prefix of the
IN sets will be used to produce spans.

For example, for the following table and query, only the predicates on
columns a and b will be used to construct the constrained scan of
abc_idx, because including the predicate on column c would produce
more spans than optimizer_span_limit:

CREATE TABLE abc (a INT, b INT, c INT, INDEX abc_idx (a, b, c));
SET optimizer_span_limit = 10;
SELECT * FROM abc WHERE a IN (1, 3, 5) AND b IN (2, 4, 6) AND c IN (7, 9, 11);

Co-Authored-By: roachdev-claude roachdev-claude-bot@cockroachlabs.com


opt: set default optimizer_span_limit to 131072

Set the default value of the optimizer_span_limit session variable to
2^17 (131072) instead of 0 (no limit). This provides a reasonable upper
bound on the number of spans the optimizer will produce, preventing
pathological cases where large cross-products of IN sets produce
excessive spans.

Informs: #167620

Release note (sql change): The session variable optimizer_span_limit
now defaults to 131072. This bounds the number of spans the optimizer
will allow in constrained index scans generated during query
optimization. Queries that would exceed this limit will use fewer looser
spans with remaining filters instead. Set to 0 to disable the limit.

Co-Authored-By: roachdev-claude roachdev-claude-bot@cockroachlabs.com

@blathers-crl

This comment was marked as resolved.

@trunk-io
Copy link
Copy Markdown
Contributor

trunk-io Bot commented Mar 31, 2026

😎 Merged successfully - details.

@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@michae2 michae2 force-pushed the michae2/optimizer-span-limit branch from e1e40df to 12490b3 Compare April 13, 2026 23:30
@blathers-crl

This comment was marked as resolved.

1 similar comment
@blathers-crl

This comment was marked as resolved.

@michae2 michae2 force-pushed the michae2/optimizer-span-limit branch from 12490b3 to c40d9da Compare April 14, 2026 05:46
@blathers-crl

This comment was marked as resolved.

1 similar comment
@blathers-crl

This comment was marked as resolved.

@michae2 michae2 force-pushed the michae2/optimizer-span-limit branch from c40d9da to 3715752 Compare April 15, 2026 22:09
@blathers-crl

This comment was marked as resolved.

1 similar comment
@blathers-crl

This comment was marked as resolved.

@michae2 michae2 force-pushed the michae2/optimizer-span-limit branch from 3715752 to b9754ea Compare April 15, 2026 23:02
@blathers-crl

This comment was marked as resolved.

1 similar comment
@blathers-crl

This comment was marked as resolved.

@michae2 michae2 force-pushed the michae2/optimizer-span-limit branch from 22fe585 to be22817 Compare April 16, 2026 05:08
@blathers-crl

This comment was marked as resolved.

1 similar comment
@blathers-crl

This comment was marked as resolved.

@michae2 michae2 force-pushed the michae2/optimizer-span-limit branch from be22817 to 317e881 Compare April 16, 2026 17:20
@cockroach-teamcity

This comment was marked as resolved.

@cockroach-teamcity cockroach-teamcity added the X-perf-check Microbenchmarks CI: Added to a PR if a performance regression is detected and should be checked label Apr 16, 2026
@michae2 michae2 force-pushed the michae2/optimizer-span-limit branch from 317e881 to ea695fb Compare April 16, 2026 18:19
@cockroach-teamcity

This comment was marked as resolved.

michae2 and others added 2 commits April 16, 2026 14:38
This commit creates a new session variable `optimizer_span_limit`, and
uses it to bound the number of spans in constrained scans produced by
`GenerateConstrainedScans`.

Fixes: cockroachdb#167620

Release note: Add a new session variable `optimizer_span_limit` which
bounds the number of spans the optimizer will allow in a single
constrained index scan. If a single IN set has more items than this
limit, that IN set will not be used to build a constrained index
scan. If the cross product of two or more IN sets would produce more
spans than this limit for a composite index, then only a prefix of the
IN sets will be used to produce spans.

For example, for the following table and query, only the predicates on
columns `a` and `b` will be used to construct the constrained scan of
`abc_idx`, because including the predicate on column `c` would produce
more spans than `optimizer_span_limit`:

```sql
CREATE TABLE abc (a INT, b INT, c INT, INDEX abc_idx (a, b, c));
SET optimizer_span_limit = 10;
SELECT * FROM abc WHERE a IN (1, 3, 5) AND b IN (2, 4, 6) AND c IN (7, 9, 11);
```

Co-Authored-By: roachdev-claude <roachdev-claude-bot@cockroachlabs.com>
Set the default value of the `optimizer_span_limit` session variable to
2^17 (131072) instead of 0 (no limit). This provides a reasonable upper
bound on the number of spans the optimizer will produce, preventing
pathological cases where large cross-products of IN sets produce
excessive spans.

Informs: cockroachdb#167620

Release note (sql change): The session variable `optimizer_span_limit`
now defaults to 131072. This bounds the number of spans the optimizer
will allow in constrained index scans generated during query
optimization. Queries that would exceed this limit will use fewer looser
spans with remaining filters instead. Set to 0 to disable the limit.

Co-Authored-By: roachdev-claude <roachdev-claude-bot@cockroachlabs.com>
@michae2 michae2 force-pushed the michae2/optimizer-span-limit branch from ea695fb to 9887a57 Compare April 16, 2026 21:43
@michae2 michae2 changed the title Michae2/optimizer span limit opt: add optimizer_span_limit and use in GenerateConstrainedScans Apr 16, 2026
@michae2 michae2 removed the X-perf-check Microbenchmarks CI: Added to a PR if a performance regression is detected and should be checked label Apr 16, 2026
@michae2 michae2 requested review from DrewKimball and mw5h April 16, 2026 22:05
@michae2 michae2 requested review from a team April 16, 2026 22:05
@michae2 michae2 marked this pull request as ready for review April 16, 2026 22:05
@blathers-crl

This comment was marked as resolved.

@michae2 michae2 added backport-25.4.x Flags PRs that need to be backported to 25.4 backport-26.1.x Flags PRs that need to be backported to 26.1 backport-26.2.x Flags PRs that need to be backported to 26.2 labels Apr 17, 2026
@michae2
Copy link
Copy Markdown
Collaborator Author

michae2 commented Apr 17, 2026

I'm just looking to backport the first commit.

Copy link
Copy Markdown
Collaborator

@DrewKimball DrewKimball left a comment

Choose a reason for hiding this comment

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

:lgtm: Thanks for doing this!

@DrewKimball reviewed 23 files and all commit messages, and made 1 comment.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on mw5h).

@michae2
Copy link
Copy Markdown
Collaborator Author

michae2 commented Apr 17, 2026

You're welcome! TFTR!

/trunk merge

@trunk-io trunk-io Bot merged commit e9e41e4 into cockroachdb:master Apr 17, 2026
42 of 44 checks passed
@blathers-crl
Copy link
Copy Markdown

blathers-crl Bot commented Apr 17, 2026

Based on the specified backports for this PR, I applied new labels to the following linked issue(s). Please adjust the labels as needed to match the branches actually affected by the issue(s), including adding any known older branches.


Issue #167620: branch-release-25.4, branch-release-26.1, branch-release-26.2.


🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@blathers-crl
Copy link
Copy Markdown

blathers-crl Bot commented Apr 17, 2026

Encountered an error creating backports. Some common things that can go wrong:

  1. The backport branch might have already existed.
  2. There was a merge conflict.
  3. The backport branch contained merge commits.

You might need to create your backport manually using the backport tool.


💡 Consider backporting to the fork repo instead of the main repo. See instructions for more details.

merge conflict cherry-picking 52376f6 to blathers/backport-release-25.4-167180

Backport to branch 25.4.x failed. See errors above.


💡 Consider backporting to the fork repo instead of the main repo. See instructions for more details.

merge conflict cherry-picking 52376f6 to blathers/backport-release-26.1-167180

Backport to branch 26.1.x failed. See errors above.


💡 Consider backporting to the fork repo instead of the main repo. See instructions for more details.

merge conflict cherry-picking 52376f6 to blathers/backport-release-26.2-167180

Backport to branch 26.2.x failed. See errors above.


🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@michae2
Copy link
Copy Markdown
Collaborator Author

michae2 commented Apr 17, 2026

blathers auth-check

Copy link
Copy Markdown
Collaborator Author

michae2 commented Apr 17, 2026

You shall pass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-25.4.x Flags PRs that need to be backported to 25.4 backport-26.1.x Flags PRs that need to be backported to 26.1 backport-26.2.x Flags PRs that need to be backported to 26.2 backport-failed target-release-26.3.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

opt: no limit on spans created by GenerateConstrainedScans

3 participants