Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release-20.2: opt: don't hold on to evalCtx from detached Memo #57251

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions pkg/sql/opt/memo/memo.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,17 +383,29 @@ func (m *Memo) NextWithID() opt.WithID {
return m.curWithID
}

// ClearColStats clears all column statistics from every relational expression
// in the memo. This is used to free up the potentially large amount of memory
// used by histograms.
func (m *Memo) ClearColStats(parent opt.Expr) {
for i, n := 0, parent.ChildCount(); i < n; i++ {
child := parent.Child(i)
m.ClearColStats(child)
}
// Detach is used when we detach a memo that is to be reused later (either for
// execbuilding or with AssignPlaceholders). New expressions should no longer be
// constructed in this memo.
func (m *Memo) Detach() {
m.interner = interner{}
// It is important to not hold on to the EvalCtx in the logicalPropsBuilder
// (#57059).
m.logPropsBuilder = logicalPropsBuilder{}

// Clear all column statistics from every relational expression in the memo.
// This is used to free up the potentially large amount of memory used by
// histograms.
var clearColStats func(parent opt.Expr)
clearColStats = func(parent opt.Expr) {
for i, n := 0, parent.ChildCount(); i < n; i++ {
child := parent.Child(i)
clearColStats(child)
}

switch t := parent.(type) {
case RelExpr:
t.Relational().Stats.ColStats = props.ColStatsMap{}
switch t := parent.(type) {
case RelExpr:
t.Relational().Stats.ColStats = props.ColStatsMap{}
}
}
clearColStats(m.RootExpr())
}
6 changes: 3 additions & 3 deletions pkg/sql/opt/norm/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ func (f *Factory) FoldingControl() *FoldingControl {
// placeholders are assigned. If there are no placeholders, there is no need
// for column statistics, since the memo is already fully optimized.
func (f *Factory) DetachMemo() *memo.Memo {
f.mem.ClearColStats(f.mem.RootExpr())
detach := f.mem
m := f.mem
f.mem = nil
m.Detach()
f.Init(f.evalCtx, nil /* catalog */)
return detach
return m
}

// DisableOptimizations disables all transformation rules. The unaltered input
Expand Down