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

session: set process info before building plan (#22101) #22148

Merged
merged 6 commits into from
Jan 26, 2021
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
11 changes: 10 additions & 1 deletion session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1080,16 +1080,20 @@ func (s *session) SetProcessInfo(sql string, t time.Time, command byte, maxExecu
MaxExecutionTime: maxExecutionTime,
RedactSQL: s.sessionVars.EnableRedactLog,
}
oldPi := s.ShowProcess()
if p == nil {
// Store the last valid plan when the current plan is nil.
// This is for `explain for connection` statement has the ability to query the last valid plan.
oldPi := s.ShowProcess()
if oldPi != nil && oldPi.Plan != nil && len(oldPi.PlanExplainRows) > 0 {
pi.Plan = oldPi.Plan
pi.PlanExplainRows = oldPi.PlanExplainRows
pi.RuntimeStatsColl = oldPi.RuntimeStatsColl
}
}
// We set process info before building plan, so we extended execution time.
if oldPi != nil && oldPi.Info == pi.Info {
pi.Time = oldPi.Time
}
_, pi.Digest = s.sessionVars.StmtCtx.SQLDigest()
s.currentPlan = nil
if s.sessionVars.User != nil {
Expand Down Expand Up @@ -1212,6 +1216,11 @@ func (s *session) execute(ctx context.Context, sql string) (recordSets []sqlexec
if err := executor.ResetContextOfStmt(s, stmtNode); err != nil {
return nil, err
}

// Uncorrelated subqueries will execute once when building plan, so we reset process info before building plan.
cmd32 := atomic.LoadUint32(&s.GetSessionVars().CommandValue)
s.SetProcessInfo(stmtNode.Text(), time.Now(), byte(cmd32), 0)

stmt, err := compiler.Compile(ctx, stmtNode)
if err != nil {
s.rollbackOnError(ctx)
Expand Down
18 changes: 18 additions & 0 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3466,3 +3466,21 @@ func (s *testSessionSerialSuite) TestDefaultWeekFormat(c *C) {
tk2 := testkit.NewTestKitWithInit(c, s.store)
tk2.MustQuery("select week('2020-02-02'), @@default_week_format, week('2020-02-02');").Check(testkit.Rows("6 4 6"))
}

func (s *testSessionSerialSuite) TestProcessInfoIssue22068(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table t(a int)")
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
tk.MustQuery("select 1 from t where a = (select sleep(5));").Check(testkit.Rows())
wg.Done()
}()
time.Sleep(2 * time.Second)
pi := tk.Se.ShowProcess()
c.Assert(pi, NotNil)
c.Assert(pi.Info, Equals, "select 1 from t where a = (select sleep(5));")
c.Assert(pi.Plan, IsNil)
wg.Wait()
}