Skip to content

Commit

Permalink
bindinfo: add status vars for 'last_plan_binding_update_time' (#26340)
Browse files Browse the repository at this point in the history
  • Loading branch information
Reminiscent authored Aug 3, 2021
1 parent 2745baa commit ec8351c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
28 changes: 28 additions & 0 deletions bindinfo/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2132,3 +2132,31 @@ func (s *testSuite) TestTemporaryTable(c *C) {
tk.MustGetErrCode("create binding for update t set a = 1 where b = 1 and c > 1 using update /*+ use_index(t, c) */ t set a = 1 where b = 1 and c > 1", errno.ErrOptOnTemporaryTable)
tk.MustGetErrCode("create binding for delete from t where b = 1 and c > 1 using delete /*+ use_index(t, c) */ from t where b = 1 and c > 1", errno.ErrOptOnTemporaryTable)
}

func (s *testSuite) TestBindingLastUpdateTime(c *C) {
tk := testkit.NewTestKit(c, s.store)
s.cleanBindingEnv(tk)
tk.MustExec("use test")
tk.MustExec("drop table if exists t0;")
tk.MustExec("create table t0(a int, key(a));")
tk.MustExec("create global binding for select * from t0 using select * from t0 use index(a);")
tk.MustExec("admin reload bindings;")

bindHandle := bindinfo.NewBindHandle(tk.Se)
err := bindHandle.Update(true)
c.Check(err, IsNil)
sql, hash := parser.NormalizeDigest("select * from test . t0")
bindData := bindHandle.GetBindRecord(hash.String(), sql, "test")
c.Assert(len(bindData.Bindings), Equals, 1)
bind := bindData.Bindings[0]
updateTime := bind.UpdateTime.String()

rows1 := tk.MustQuery("show status like 'last_plan_binding_update_time';").Rows()
updateTime1 := rows1[0][1]
c.Assert(updateTime1, Equals, updateTime)

rows2 := tk.MustQuery("show session status like 'last_plan_binding_update_time';").Rows()
updateTime2 := rows2[0][1]
c.Assert(updateTime2, Equals, updateTime)
tk.MustQuery(`show global status like 'last_plan_binding_update_time';`).Check(testkit.Rows())
}
1 change: 1 addition & 0 deletions bindinfo/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func NewBindHandle(ctx sessionctx.Context) *BindHandle {
// BindSQL has already been validated when coming here, so we use nil sctx parameter.
return handle.AddBindRecord(nil, record)
}
variable.RegisterStatistics(handle)
return handle
}

Expand Down
39 changes: 39 additions & 0 deletions bindinfo/stat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2021 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package bindinfo

import (
"github.com/pingcap/tidb/sessionctx/variable"
)

var (
lastPlanBindingUpdateTime = "last_plan_binding_update_time"
)

// GetScope gets the status variables scope.
func (h *BindHandle) GetScope(status string) variable.ScopeFlag {
return variable.ScopeSession
}

// Stats returns the server statistics.
func (h *BindHandle) Stats(vars *variable.SessionVars) (map[string]interface{}, error) {
h.bindInfo.Lock()
defer func() {
h.bindInfo.Unlock()
}()
m := make(map[string]interface{})
m[lastPlanBindingUpdateTime] = h.bindInfo.lastUpdateTime.String()

return m, nil
}

0 comments on commit ec8351c

Please sign in to comment.