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

sessionctx, executor: fix failure of reading MaxPreparedStmtCount #39736

Merged
merged 11 commits into from
Dec 15, 2022
11 changes: 11 additions & 0 deletions executor/prepared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/pingcap/tidb/parser/auth"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/parser/terror"
plannercore "github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/server"
"github.com/pingcap/tidb/sessionctx/variable"
Expand Down Expand Up @@ -1255,3 +1256,13 @@ func TestIssue31141(t *testing.T) {
tk.MustExec("set @@tidb_txn_mode = 'optimistic'")
tk.MustExec("prepare stmt1 from 'do 1'")
}

func TestMaxPreparedStmtCount(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("set @@global.max_prepared_stmt_count = 2")
tk.MustExec("prepare stmt1 from 'select ? as num from dual'")
tk.MustExec("prepare stmt2 from 'select ? as num from dual'")
err := tk.ExecToErr("prepare stmt3 from 'select ? as num from dual'")
require.True(t, terror.ErrorEqual(err, variable.ErrMaxPreparedStmtCountReached))
}
6 changes: 1 addition & 5 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -2097,11 +2097,7 @@ func (s *SessionVars) GetNonPreparedPlanCacheStmt(sql string) interface{} {
// AddPreparedStmt adds prepareStmt to current session and count in global.
func (s *SessionVars) AddPreparedStmt(stmtID uint32, stmt interface{}) error {
if _, exists := s.PreparedStmts[stmtID]; !exists {
valStr, _ := s.GetSystemVar(MaxPreparedStmtCount)
maxPreparedStmtCount, err := strconv.ParseInt(valStr, 10, 64)
if err != nil {
maxPreparedStmtCount = DefMaxPreparedStmtCount
}
maxPreparedStmtCount := MaxPreparedStmtCountValue.Load()
newPreparedStmtCount := atomic.AddInt64(&PreparedStmtCount, 1)
if maxPreparedStmtCount >= 0 && newPreparedStmtCount > maxPreparedStmtCount {
atomic.AddInt64(&PreparedStmtCount, -1)
Expand Down
10 changes: 9 additions & 1 deletion sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,15 @@ var defaultSysVars = []*SysVar{
}},

/* The system variables below have GLOBAL scope */
{Scope: ScopeGlobal, Name: MaxPreparedStmtCount, Value: strconv.FormatInt(DefMaxPreparedStmtCount, 10), Type: TypeInt, MinValue: -1, MaxValue: 1048576},
{Scope: ScopeGlobal, Name: MaxPreparedStmtCount, Value: strconv.FormatInt(DefMaxPreparedStmtCount, 10), Type: TypeInt, MinValue: -1, MaxValue: 1048576,
SetGlobal: func(_ context.Context, s *SessionVars, val string) error {
num, err := strconv.ParseInt(val, 10, 64)
if err != nil {
return errors.Trace(err)
}
MaxPreparedStmtCountValue.Store(num)
return nil
}},
{Scope: ScopeGlobal, Name: InitConnect, Value: "", Validation: func(vars *SessionVars, normalizedValue string, originalValue string, scope ScopeFlag) (string, error) {
p := parser.New()
p.SetSQLMode(vars.SQLMode)
Expand Down
1 change: 1 addition & 0 deletions sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,7 @@ var (
PasswordHistory = atomic.NewInt64(DefPasswordReuseHistory)
PasswordReuseInterval = atomic.NewInt64(DefPasswordReuseTime)
IsSandBoxModeEnabled = atomic.NewBool(false)
MaxPreparedStmtCountValue = atomic.NewInt64(DefMaxPreparedStmtCount)
HistoricalStatsDuration = atomic.NewDuration(DefTiDBHistoricalStatsDuration)
)

Expand Down