release-26.2: sql/stats,sql/opt: detect canary window expiration in digest fast path#167503
Conversation
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>
e55b77e to
0b64088
Compare
|
😎 Merged successfully - details. |
|
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. |
mw5h
left a comment
There was a problem hiding this comment.
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.
|
Yup this commit is not going to affect the normal cases without canary stats enabled. TFTR! |
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