Skip to content

release-26.2: sql/stats,sql/opt: detect canary window expiration in digest fast path#167503

Merged
trunk-io[bot] merged 2 commits intorelease-26.2from
blathers/backport-release-26.2-166957
Apr 7, 2026
Merged

release-26.2: sql/stats,sql/opt: detect canary window expiration in digest fast path#167503
trunk-io[bot] merged 2 commits intorelease-26.2from
blathers/backport-release-26.2-166957

Conversation

@blathers-crl
Copy link
Copy Markdown

@blathers-crl blathers-crl bot commented Apr 3, 2026

Backport 2/2 commits from #166957 on behalf of @ZhouXing19.


Fixes #166688

Previously, the CheckDependencies digest fast path compared only
StatsGeneration to decide whether cached memos were still valid.
StatsGeneration only increments when stats are added to, removed from,
or refreshed in the TableStatisticsCache — but canary window expiration
is purely a function of time. When a canary window expired, no cache
mutation occurred, so the digest fast path saw an unchanged generation,
concluded "nothing changed," and skipped the per-object Equals() check
that would have detected different stats being returned by
getStableStatsLocked.

This commit threads a canaryExpiration timestamp from the stats cache
through the cat.Table interface into the optimizer metadata's digest
struct. During the slow path of CheckDependencies, the earliest canary
expiration across all referenced tables is computed from freshly resolved
data sources and stored atomically alongside the digest. The fast path
now additionally checks whether any canary window has expired (comparing
against StatsAsOf or the current wall clock), and if so, falls through
to the slow path.

Release note: None

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


Release justification: Fix a performance issue with a feature requested by customer

ZhouXing19 and others added 2 commits March 28, 2026 00:00
Add a Timestamp.OrNow() method that returns the receiver if non-empty,
or a wall-clock-now timestamp otherwise. This provides a single source
of truth for the common pattern of resolving an optional timestamp
override (e.g. a session setting like StatsAsOf) where an empty value
means "use the current time."

Release note: None

Co-Authored-By: roachdev-claude <roachdev-claude-bot@cockroachlabs.com>
Previously, the CheckDependencies digest fast path compared only
StatsGeneration to decide whether cached memos were still valid.
StatsGeneration only increments when stats are added to, removed from,
or refreshed in the TableStatisticsCache — but canary window expiration
is purely a function of time. When a canary window expired, no cache
mutation occurred, so the digest fast path saw an unchanged generation,
concluded "nothing changed," and skipped the per-object Equals() check
that would have detected different stats being returned by
getStableStatsLocked.

This commit threads a canaryExpiration timestamp from the stats cache
through the cat.Table interface into the optimizer metadata.
During the slow path of CheckDependencies, the earliest canary
expiration across all referenced tables is computed from freshly resolved
data sources and stored atomically alongside the digest. The fast path
now additionally checks whether any canary window has expired (comparing
against the current wall clock via OrNow), and if so, falls through to
the slow path.

When stats_as_of is set, the fast path is skipped
entirely — analogous to the existing AOST handling — because
stats_as_of can change the canary-vs-stable stats perspective between
executions.

Resolves: #166688

Release note (performance): Statement executions using canary stats
will not use cached plans, which prevents cache thrashing but causes
a slight increase in planning time over statement executions using
stable stats.

Co-Authored-By: roachdev-claude <roachdev-claude-bot@cockroachlabs.com>
@blathers-crl blathers-crl bot force-pushed the blathers/backport-release-26.2-166957 branch from e55b77e to 0b64088 Compare April 3, 2026 21:21
@blathers-crl blathers-crl bot requested a review from a team as a code owner April 3, 2026 21:21
@blathers-crl blathers-crl bot added blathers-backport This is a backport that Blathers created automatically. O-robot Originated from a bot. labels Apr 3, 2026
@blathers-crl blathers-crl bot requested review from yuzefovich and removed request for a team April 3, 2026 21:21
@blathers-crl blathers-crl bot requested review from ZhouXing19 and michae2 April 3, 2026 21:21
@trunk-io
Copy link
Copy Markdown
Contributor

trunk-io bot commented Apr 3, 2026

😎 Merged successfully - details.

@blathers-crl
Copy link
Copy Markdown
Author

blathers-crl bot commented Apr 3, 2026

Thanks for opening a backport.

Before merging, please confirm that the change does not break backwards compatibility and otherwise complies with the backport policy. Include a brief release justification in the PR description explaining why the backport is appropriate. All backports must be reviewed by the TL for the owning area. While the stricter LTS policy does not yet apply, please exercise judgment and consider gating non-critical changes behind a disabled-by-default feature flag when appropriate.

@blathers-crl blathers-crl bot added backport Label PR's that are backports to older release branches T-sql-queries SQL Queries Team labels Apr 3, 2026
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@ZhouXing19 ZhouXing19 requested a review from mw5h April 3, 2026 21:48
Copy link
Copy Markdown
Contributor

@mw5h mw5h left a comment

Choose a reason for hiding this comment

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

A little scary, but it doesn't look like this code does anything if canary stats are disabled and we absolutely have to have this if they are enabled.

@ZhouXing19
Copy link
Copy Markdown
Collaborator

Yup this commit is not going to affect the normal cases without canary stats enabled.

TFTR!

@trunk-io trunk-io bot merged commit 218d577 into release-26.2 Apr 7, 2026
23 checks passed
@trunk-io trunk-io bot deleted the blathers/backport-release-26.2-166957 branch April 7, 2026 15:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport Label PR's that are backports to older release branches blathers-backport This is a backport that Blathers created automatically. O-robot Originated from a bot. T-sql-queries SQL Queries Team v26.2.0-prerelease

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants