Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: logically delete the bindinfo when create the new binding #26015

Merged
merged 19 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions bindinfo/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ func (s *testSuite) TestGlobalBinding(c *C) {
c.Check(err, IsNil)
c.Check(chk.NumRows(), Equals, 0)

_, err = tk.Exec("delete from mysql.bind_info")
_, err = tk.Exec("delete from mysql.bind_info where source != 'builtin'")
c.Assert(err, IsNil)
}
}
Expand Down Expand Up @@ -1102,6 +1102,7 @@ func (s *testSuite) TestBaselineDBLowerCase(c *C) {
// default_db should have lower case.
c.Assert(rows[0][2], Equals, "spm")
tk.MustQuery("select original_sql, default_db, status from mysql.bind_info where original_sql = 'select * from `spm` . `t`'").Check(testkit.Rows(
"select * from `spm` . `t` SPM deleted",
"select * from `spm` . `t` spm using",
))
}
Expand Down Expand Up @@ -2015,13 +2016,15 @@ func (s *testSuite) TestReCreateBind(c *C) {
c.Assert(rows[0][3], Equals, "using")

tk.MustExec("create global binding for select * from t using select * from t")
tk.MustQuery("select original_sql, status from mysql.bind_info").Check(testkit.Rows(
"select * from `test` . `t` using",
))
rows = tk.MustQuery("show global bindings").Rows()
c.Assert(len(rows), Equals, 1)
c.Assert(rows[0][0], Equals, "select * from `test` . `t`")
c.Assert(rows[0][3], Equals, "using")

rows = tk.MustQuery("select original_sql, status from mysql.bind_info where source != 'builtin';").Rows()
c.Assert(len(rows), Equals, 2)
c.Assert(rows[0][1], Equals, "deleted")
c.Assert(rows[1][1], Equals, "using")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the builtin row not returned by this query?

}

func (s *testSuite) TestExplainShowBindSQL(c *C) {
Expand Down Expand Up @@ -2096,8 +2099,9 @@ func (s *testSuite) TestConcurrentCapture(c *C) {
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",
tk.MustQuery("select original_sql, source, status from mysql.bind_info where source != 'builtin'").Check(testkit.Rows(
eurekaka marked this conversation as resolved.
Show resolved Hide resolved
"select * from `test` . `t` manual deleted",
"select * from `test` . `t` capture using",
))
}

Expand Down
12 changes: 7 additions & 5 deletions bindinfo/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (h *BindHandle) Update(fullLoad bool) (err error) {

exec := h.sctx.Context.(sqlexec.RestrictedSQLExecutor)
stmt, err := exec.ParseWithParams(context.TODO(), `SELECT original_sql, bind_sql, default_db, status, create_time, update_time, charset, collation, source
FROM mysql.bind_info WHERE update_time > %? ORDER BY update_time`, updateTime)
FROM mysql.bind_info WHERE update_time > %? ORDER BY update_time, create_time`, updateTime)
if err != nil {
return err
}
Expand Down Expand Up @@ -218,14 +218,16 @@ func (h *BindHandle) CreateBindRecord(sctx sessionctx.Context, record *BindRecor
if err = h.lockBindInfoTable(); err != nil {
return err
}
// Binding recreation should physically delete previous bindings.
_, err = exec.ExecuteInternal(context.TODO(), `DELETE FROM mysql.bind_info WHERE original_sql = %?`, record.OriginalSQL)

now := types.NewTime(types.FromGoTime(time.Now()), mysql.TypeTimestamp, 3)

updateTs := now.String()
_, err = exec.ExecuteInternal(context.TODO(), `UPDATE mysql.bind_info SET status = %?, update_time = %? WHERE original_sql = %? AND update_time < %?`,
deleted, updateTs, record.OriginalSQL, updateTs)
if err != nil {
return err
}

now := types.NewTime(types.FromGoTime(time.Now()), mysql.TypeTimestamp, 3)

for i := range record.Bindings {
record.Bindings[i].CreateTime = now
record.Bindings[i].UpdateTime = now
Expand Down
2 changes: 1 addition & 1 deletion infoschema/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,7 @@ func (s *testTableSuite) TestStmtSummaryInternalQuery(c *C) {
"where digest_text like \"select `original_sql` , `bind_sql` , `default_db` , status%\""
tk.MustQuery(sql).Check(testkit.Rows(
"select `original_sql` , `bind_sql` , `default_db` , status , `create_time` , `update_time` , charset , " +
"collation , source from `mysql` . `bind_info` where `update_time` > ? order by `update_time`"))
"collation , source from `mysql` . `bind_info` where `update_time` > ? order by `update_time` , `create_time`"))

// Test for issue #21642.
tk.MustQuery(`select tidb_version()`)
Expand Down