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) (#49387)

close #49344
  • Loading branch information
ti-chi-bot authored Feb 18, 2024
1 parent 4c89812 commit 9068c4b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
7 changes: 5 additions & 2 deletions pkg/planner/core/plan_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,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 pkg/planner/core/plan_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,17 @@ func TestInvalidRange(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 TestIssue40093(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
Expand Down

0 comments on commit 9068c4b

Please sign in to comment.