Skip to content

Commit

Permalink
executor: fix RU missing for DML stmtatement in explain analyze (#52576)
Browse files Browse the repository at this point in the history
close #52575
  • Loading branch information
guo-shaoge authored Apr 15, 2024
1 parent 224ae15 commit f2fb909
Showing 1 changed file with 8 additions and 24 deletions.
32 changes: 8 additions & 24 deletions pkg/executor/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,25 +94,6 @@ func (e *ExplainExec) Next(ctx context.Context, req *chunk.Chunk) error {
return nil
}

func (e *ExplainExec) handleRUDetails(ctx context.Context, onlyRegister bool) {
if e.explain.Analyze && (e.analyzeExec == nil || !e.executed) {
return
}
if coll := e.Ctx().GetSessionVars().StmtCtx.RuntimeStatsColl; coll != nil {
if onlyRegister {
// Register RU stats to make sure the output of explain analyze doesn't change.
newRUDetails := clientutil.NewRUDetails()
coll.RegisterStats(e.explain.TargetPlan.ID(), &ruRuntimeStats{newRUDetails})
return
}

if ruDetailsRaw := ctx.Value(clientutil.RUDetailsCtxKey); ruDetailsRaw != nil {
ruDetails := ruDetailsRaw.(*clientutil.RUDetails)
coll.RegisterStats(e.explain.TargetPlan.ID(), &ruRuntimeStats{ruDetails})
}
}
}

func (e *ExplainExec) executeAnalyzeExec(ctx context.Context) (err error) {
if e.explain.Analyze && e.analyzeExec != nil && !e.executed {
defer func() {
Expand All @@ -124,10 +105,6 @@ func (e *ExplainExec) executeAnalyzeExec(ctx context.Context) (err error) {
err = err1
}
}

// Handle RU runtime stats after Close() to make sure all ru has been collected.
// For example, localMppCoordinator reports last ru consumption when Close().
e.handleRUDetails(ctx, false)
}()
if minHeapInUse, alarmRatio := e.Ctx().GetSessionVars().MemoryDebugModeMinHeapInUse, e.Ctx().GetSessionVars().MemoryDebugModeAlarmRatio; minHeapInUse != 0 && alarmRatio != 0 {
memoryDebugModeCtx, cancel := context.WithCancel(ctx)
Expand Down Expand Up @@ -156,7 +133,14 @@ func (e *ExplainExec) executeAnalyzeExec(ctx context.Context) (err error) {
}
}
}
e.handleRUDetails(ctx, true)
// Register the RU runtime stats to the runtime stats collection after the analyze executor has been executed.
if e.explain.Analyze && e.analyzeExec != nil && e.executed {
ruDetailsRaw := ctx.Value(clientutil.RUDetailsCtxKey)
if coll := e.Ctx().GetSessionVars().StmtCtx.RuntimeStatsColl; coll != nil && ruDetailsRaw != nil {
ruDetails := ruDetailsRaw.(*clientutil.RUDetails)
coll.RegisterStats(e.explain.TargetPlan.ID(), &ruRuntimeStats{ruDetails})
}
}
return err
}

Expand Down

0 comments on commit f2fb909

Please sign in to comment.