Skip to content

Commit

Permalink
executor: fix issue of can not get the TxnStart if the SQL has a non-…
Browse files Browse the repository at this point in the history
…related subquery (#44232) (#44243)

close #40851
  • Loading branch information
ti-chi-bot authored May 30, 2023
1 parent 7bd4528 commit 0c692be
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ var (
// processinfoSetter is the interface use to set current running process info.
type processinfoSetter interface {
SetProcessInfo(string, time.Time, byte, uint64)
UpdateProcessInfo()
}

// recordSet wraps an executor, implements sqlexec.RecordSet interface
Expand Down
5 changes: 5 additions & 0 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,11 @@ func init() {
if err != nil {
return nil, err
}
if pi, ok := sctx.(processinfoSetter); ok {
// Before executing the sub-query, we need update the processinfo to make the progress bar more accurate.
// because the sub-query may take a long time.
pi.UpdateProcessInfo()
}
chk := tryNewCacheChunk(exec)
err = Next(ctx, exec, chk)
if err != nil {
Expand Down
17 changes: 17 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6277,3 +6277,20 @@ func TestIssue39211(t *testing.T) {
tk.MustExec("set @@tidb_enable_null_aware_anti_join=true;")
tk.MustQuery("select * from t where (a,b) not in (select a, b from s);").Check(testkit.Rows())
}

func TestProcessInfoOfSubQuery(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk2 := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t (i int, j int);")
var wg sync.WaitGroup
wg.Add(1)
go func() {
tk.MustQuery("select 1, (select sleep(count(1) + 2) from t);")
wg.Done()
}()
time.Sleep(time.Second)
tk2.MustQuery("select 1 from information_schema.processlist where TxnStart != '' and info like 'select%sleep% from t%'").Check(testkit.Rows("1"))
wg.Wait()
}
10 changes: 10 additions & 0 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1648,6 +1648,16 @@ func (s *session) SetProcessInfo(sql string, t time.Time, command byte, maxExecu
s.processInfo.Store(&pi)
}

// UpdateProcessInfo updates the session's process info for the running statement.
func (s *session) UpdateProcessInfo() {
pi := s.ShowProcess()
if pi == nil || pi.CurTxnStartTS != 0 {
return
}
// Update the current transaction start timestamp.
pi.CurTxnStartTS = s.sessionVars.TxnCtx.StartTS
}

func (s *session) getOomAlarmVariablesInfo() util.OOMAlarmVariablesInfo {
return util.OOMAlarmVariablesInfo{
SessionAnalyzeVersion: s.sessionVars.AnalyzeVersion,
Expand Down

0 comments on commit 0c692be

Please sign in to comment.