From 6d0ffe457e317bc172f0c8228e0e7c8e35eb2bd6 Mon Sep 17 00:00:00 2001 From: ti-srebot <66930949+ti-srebot@users.noreply.github.com> Date: Tue, 26 Jan 2021 22:27:54 +0800 Subject: [PATCH] session: set process info before building plan (#22101) (#22148) Signed-off-by: ti-srebot --- session/session.go | 11 ++++++++++- session/session_test.go | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/session/session.go b/session/session.go index c1a5bde3551ff..07ecaa1b486ab 100644 --- a/session/session.go +++ b/session/session.go @@ -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 { @@ -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) diff --git a/session/session_test.go b/session/session_test.go index dc0811557af68..0da6852f1e114 100644 --- a/session/session_test.go +++ b/session/session_test.go @@ -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() +}