Skip to content

Commit

Permalink
planner: check whether plan-cache is disabled each time when getting …
Browse files Browse the repository at this point in the history
…plan from the plan cache (#49373) (#49386)

close #49344
  • Loading branch information
ti-chi-bot authored Feb 27, 2024
1 parent a3de4a8 commit acc9abc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
7 changes: 5 additions & 2 deletions planner/core/plan_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,16 @@ func GetPlanFromSessionPlanCache(ctx context.Context, sctx sessionctx.Context,
sessVars := sctx.GetSessionVars()
stmtCtx := sessVars.StmtCtx
stmtAst := stmt.PreparedAst
stmtCtx.UseCache = stmt.StmtCacheable
cacheEnabled := false
if isNonPrepared {
stmtCtx.CacheType = stmtctx.SessionNonPrepared
cacheEnabled = sctx.GetSessionVars().EnableNonPreparedPlanCache // plan-cache might be disabled after prepare.
} else {
stmtCtx.CacheType = stmtctx.SessionPrepared
cacheEnabled = sctx.GetSessionVars().EnablePreparedPlanCache
}
if !stmt.StmtCacheable {
stmtCtx.UseCache = stmt.StmtCacheable && cacheEnabled
if !stmt.StmtCacheable && stmt.UncacheableReason != "" {
stmtCtx.SetSkipPlanCache(errors.New(stmt.UncacheableReason))
}

Expand Down
11 changes: 11 additions & 0 deletions planner/core/plan_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,17 @@ func TestIssue38533(t *testing.T) {
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0"))
}

func TestIssue49344(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec(`create table t(a int)`)
tk.MustExec(`set @@tidb_enable_prepared_plan_cache=1`)
tk.MustExec(`prepare s from "select * from t"`)
tk.MustExec(`set @@tidb_enable_prepared_plan_cache=0`)
tk.MustExec(`execute s`) // no error
}

func TestInvalidRange(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
Expand Down

0 comments on commit acc9abc

Please sign in to comment.