Skip to content

Commit

Permalink
Merge #61863
Browse files Browse the repository at this point in the history
61863: sql: don't save memo unnecessarily r=RaduBerinde a=RaduBerinde

We save a reference to the Memo, which is useful for explaining plans.
However, this means that we're holding on to the memory used by the
entire explored memo during execution of the query. This change makes
it so that we only save it only if we're building an explain plan.

Fixes #59065.

Release note: None

Co-authored-by: Radu Berinde <[email protected]>
  • Loading branch information
craig[bot] and RaduBerinde committed Mar 12, 2021
2 parents a5b80ff + 0416f01 commit 4a39f9a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
5 changes: 5 additions & 0 deletions pkg/sql/instrumentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ func (ih *instrumentationHelper) ShouldCollectExecStats() bool {
return ih.collectExecStats
}

// ShouldSaveMemo returns true if we should save the memo and catalog in planTop.
func (ih *instrumentationHelper) ShouldSaveMemo() bool {
return ih.ShouldBuildExplainPlan()
}

// RecordExplainPlan records the explain.Plan for this query.
func (ih *instrumentationHelper) RecordExplainPlan(explainPlan *explain.Plan) {
ih.explainPlan = explainPlan
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ type planTop struct {
planComponents

// mem/catalog retains the memo and catalog that were used to create the
// plan.
// plan. Only set if needed by instrumentation (see ShouldSaveMemo).
mem *memo.Memo
catalog *optCatalog

Expand Down
6 changes: 4 additions & 2 deletions pkg/sql/plan_opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,8 +584,6 @@ func (opc *optPlanningCtx) runExecBuilder(
}

planTop.planComponents = *result
planTop.mem = mem
planTop.catalog = &opc.catalog
planTop.stmt = stmt
planTop.flags = opc.flags
if isDDL {
Expand All @@ -597,5 +595,9 @@ func (opc *optPlanningCtx) runExecBuilder(
if containsFullIndexScan {
planTop.flags.Set(planFlagContainsFullIndexScan)
}
if planTop.instrumentation.ShouldSaveMemo() {
planTop.mem = mem
planTop.catalog = &opc.catalog
}
return nil
}

0 comments on commit 4a39f9a

Please sign in to comment.