Skip to content

Commit

Permalink
session: set process info before building plan (#22101) (#22148)
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
ti-srebot authored Jan 26, 2021
1 parent 7c3940c commit 6d0ffe4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
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()
}

0 comments on commit 6d0ffe4

Please sign in to comment.