Skip to content

Conversation

@yuzefovich
Copy link
Member

@yuzefovich yuzefovich commented Nov 22, 2025

sql/opt: unify search for latest full stat

This commit extracts the logic for iterating over all statistics on a table to find the most recent full stat that can be utilized (i.e. while paying attention to session variables that might disallow usage of forecasts and merged partial). In particular, this allows us to fix the bug where OptimizerUseMergedPartialStatistics was being ignored when building the scan - the impact of that is pretty minor though, so there is no release note (the variable is true by default, so only non-default configs would be affected, and only that statsCreatedTime would be reported newer than it should - only used in logs - as well as in the telemetry we'd have the wrong row count.

sql/stats: miscellaneous minor cleanup

This commit contains a bunch of minor cleanups I noticed while reading the stats related code:

  • remove no longer used ImportStatsName
  • remove redundant disabling of auto stats in logic tests (the framework itself does it already)
  • fix the pre-allocation slice for out types
  • update TODOs with Faizaan's name to the corresponding issue numbers
  • wrap some comments, remove a couple stale / redundant ones, collapse some lines.

Epic: None
Release note: None

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@yuzefovich yuzefovich force-pushed the stats-nits branch 2 times, most recently from d7f22ef to 3202082 Compare November 23, 2025 20:37
@yuzefovich yuzefovich requested a review from michae2 November 24, 2025 17:38
@yuzefovich yuzefovich marked this pull request as ready for review November 24, 2025 17:38
@yuzefovich yuzefovich requested review from a team as code owners November 24, 2025 17:38
@yuzefovich yuzefovich requested review from kev-cao and removed request for a team and kev-cao November 24, 2025 17:38
Copy link
Collaborator

@mgartner mgartner 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 cleaning up!

@mgartner reviewed 3 of 3 files at r1, 8 of 8 files at r2, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @michae2)


-- commits line 8 at r1:
nit: Is there an easy way to add a regression test for this?


pkg/sql/opt/cat/utils.go line 345 at r1 (raw file):

}

// FindLatestFullStat finds the most recent full statistic that can be used for

nit: mention that it returns the index to be used with tab.Statistic().

Copy link
Collaborator

@michae2 michae2 left a comment

Choose a reason for hiding this comment

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

:lgtm: Nice cleanup!

@michae2 reviewed 3 of 3 files at r1, 8 of 8 files at r2, all commit messages.
Reviewable status: :shipit: complete! 2 of 0 LGTMs obtained (waiting on @yuzefovich)

This commit extracts the logic for iterating over all statistics on
a table to find the most recent full stat that can be utilized (i.e.
while paying attention to session variables that might disallow usage of
forecasts and merged partial). In particular, this allows us to fix the
bug where `OptimizerUseMergedPartialStatistics` was being ignored when
building the scan - the impact of that is pretty minor though, so there
is no release note (the variable is `true` by default, so only
non-default configs would be affected, and only that `statsCreatedTime`
would be reported newer than it should - only used in logs - as well as
in the telemetry we'd have the wrong row count.

Release note: None
This commit contains a bunch of minor cleanups I noticed while reading
the stats related code:
- remove no longer used `ImportStatsName`
- remove redundant disabling of auto stats in logic tests (the framework
itself does it already)
- fix the pre-allocation slice for out types
- update TODOs with Faizaan's name to the corresponding issue numbers
- wrap some comments, remove a couple stale / redundant ones, collapse
some lines.

Release note: None
Copy link
Member Author

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

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

TFTRs!

bors r+

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 2 stale) (waiting on @mgartner and @michae2)


-- commits line 8 at r1:

Previously, mgartner (Marcus Gartner) wrote…

nit: Is there an easy way to add a regression test for this?

I don't think so. IIUC the bug could be observed in two ways:

  • incorrect statsCreatedTime - currently only used in the log message added by Uzair if we have stats misestimate,
  • incorrect TotalScanRowsWithoutForecastsEstimate field of eventpb.SampledQuery in the telemetry which I think is also only observable in the logs.

So the test would need to be an integration test and would need to examine the log sink (or something along those lines), which doesn't seem worth it.

@craig
Copy link
Contributor

craig bot commented Nov 25, 2025

@craig craig bot merged commit 8ebc521 into cockroachdb:master Nov 25, 2025
23 of 24 checks passed
@yuzefovich yuzefovich deleted the stats-nits branch November 25, 2025 01:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants