diff --git a/bindinfo/bind_test.go b/bindinfo/bind_test.go index 88bc4ab52fc31..a9b035d445b92 100644 --- a/bindinfo/bind_test.go +++ b/bindinfo/bind_test.go @@ -1909,6 +1909,29 @@ func (s *testSuite) TestCapturedBindingCharset(c *C) { c.Assert(rows[0][7], Equals, "") } +func (s *testSuite) TestConcurrentCapture(c *C) { + tk := testkit.NewTestKit(c, s.store) + s.cleanBindingEnv(tk) + // Simulate an existing binding generated by concurrent CREATE BINDING, which has not been synchronized to current tidb-server yet. + // Actually, it is more common to be generated by concurrent baseline capture, I use Manual just for simpler test verification. + tk.MustExec("insert into mysql.bind_info values('select * from test . t', 'select * from test . t', '', 'using', '2000-01-01 09:00:00', '2000-01-01 09:00:00', '', '','" + + bindinfo.Manual + "')") + tk.MustQuery("select original_sql, source from mysql.bind_info where source != 'builtin'").Check(testkit.Rows( + "select * from test . t manual", + )) + stmtsummary.StmtSummaryByDigestMap.Clear() + tk.MustExec("use test") + tk.MustExec("drop table if exists t") + tk.MustExec("create table t(a int, b int)") + c.Assert(tk.Se.Auth(&auth.UserIdentity{Username: "root", Hostname: "%"}, nil, nil), IsTrue) + tk.MustExec("select * from t") + tk.MustExec("select * from t") + tk.MustExec("admin capture bindings") + tk.MustQuery("select original_sql, source from mysql.bind_info where source != 'builtin'").Check(testkit.Rows( + "select * from test . t capture", + )) +} + func (s *testSuite) TestUpdateSubqueryCapture(c *C) { tk := testkit.NewTestKit(c, s.store) s.cleanBindingEnv(tk) diff --git a/bindinfo/handle.go b/bindinfo/handle.go index 21349685db15a..1f52bf5ed2dd7 100644 --- a/bindinfo/handle.go +++ b/bindinfo/handle.go @@ -651,9 +651,9 @@ func (h *BindHandle) CaptureBaselines() { Source: Capture, } // We don't need to pass the `sctx` because the BindSQL has been validated already. - err = h.AddBindRecord(nil, &BindRecord{OriginalSQL: normalizedSQL, Db: dbName, Bindings: []Binding{binding}}) + err = h.CreateBindRecord(nil, &BindRecord{OriginalSQL: normalizedSQL, Db: dbName, Bindings: []Binding{binding}}) if err != nil { - logutil.BgLogger().Debug("[sql-bind] add bind record failed in baseline capture", zap.String("SQL", bindableStmt.Query), zap.Error(err)) + logutil.BgLogger().Debug("[sql-bind] create bind record failed in baseline capture", zap.String("SQL", bindableStmt.Query), zap.Error(err)) } } }