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

tidb: do not add history for read-only statements. (#5660) #5661

Merged
merged 1 commit into from
Jan 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 18 additions & 0 deletions new_session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,24 @@ func (s *testSessionSuite) TestRetryCleanTxn(c *C) {
c.Assert(tk.Se.GetSessionVars().InTxn(), IsFalse)
}

func (s *testSessionSuite) TestReadOnlyNotInHistory(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("create table history (a int)")
tk.MustExec("insert history values (1), (2), (3)")
tk.MustExec("set @@autocommit = 0")
tk.MustQuery("select * from history")
history := tidb.GetHistory(tk.Se)
c.Assert(history.Count(), Equals, 0)

tk.MustExec("insert history values (4)")
tk.MustExec("insert history values (5)")
c.Assert(history.Count(), Equals, 2)
tk.MustExec("commit")
tk.MustQuery("select * from history")
history = tidb.GetHistory(tk.Se)
c.Assert(history.Count(), Equals, 0)
}

// TestTruncateAlloc tests that the auto_increment ID does not reuse the old table's allocator.
func (s *testSessionSuite) TestTruncateAlloc(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
Expand Down
5 changes: 5 additions & 0 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ func (h *StmtHistory) Add(stmtID uint32, st ast.Statement, stmtCtx *variable.Sta
h.history = append(h.history, s)
}

// Count returns the count of the history.
func (h *StmtHistory) Count() int {
return len(h.history)
}

type session struct {
// processInfo is used by ShowProcess(), and should be modified atomically.
processInfo atomic.Value
Expand Down
4 changes: 3 additions & 1 deletion tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ func runStmt(ctx context.Context, s ast.Statement) (ast.RecordSet, error) {
se := ctx.(*session)
rs, err = s.Exec(ctx)
// All the history should be added here.
GetHistory(ctx).Add(0, s, se.sessionVars.StmtCtx)
if !s.IsReadOnly() {
GetHistory(ctx).Add(0, s, se.sessionVars.StmtCtx)
}
if !se.sessionVars.InTxn() {
if err != nil {
log.Info("RollbackTxn for ddl/autocommit error.")
Expand Down