From c763eb94f4d8b47a4806e8ecb1ec24ada973c5d1 Mon Sep 17 00:00:00 2001 From: zyguan Date: Fri, 16 Dec 2022 20:30:52 +0800 Subject: [PATCH] planner: fix pessimistic-auto-commit not working for point plans (#39993) close pingcap/tidb#39928 --- planner/core/point_get_plan.go | 3 ++- .../pessimistictest/pessimistic_test.go | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/planner/core/point_get_plan.go b/planner/core/point_get_plan.go index 4ff2a319c2d12..f0dff0a18a9f8 100644 --- a/planner/core/point_get_plan.go +++ b/planner/core/point_get_plan.go @@ -22,6 +22,7 @@ import ( "unsafe" "github.com/pingcap/errors" + "github.com/pingcap/tidb/config" "github.com/pingcap/tidb/expression" "github.com/pingcap/tidb/infoschema" "github.com/pingcap/tidb/kv" @@ -608,7 +609,7 @@ func getLockWaitTime(ctx sessionctx.Context, lockInfo *ast.SelectLockInfo) (lock // autocommit to 0. If autocommit is enabled, the rows matching the specification are not locked. // See https://dev.mysql.com/doc/refman/5.7/en/innodb-locking-reads.html sessVars := ctx.GetSessionVars() - if !sessVars.IsAutocommit() || sessVars.InTxn() { + if !sessVars.IsAutocommit() || sessVars.InTxn() || config.GetGlobalConfig().PessimisticTxn.PessimisticAutoCommit.Load() { lock = true waitTime = sessVars.LockWaitTimeout if lockInfo.LockType == ast.SelectLockForUpdateWaitN { diff --git a/tests/realtikvtest/pessimistictest/pessimistic_test.go b/tests/realtikvtest/pessimistictest/pessimistic_test.go index 0383272f8b1ed..ae7545e0e91f6 100644 --- a/tests/realtikvtest/pessimistictest/pessimistic_test.go +++ b/tests/realtikvtest/pessimistictest/pessimistic_test.go @@ -3074,13 +3074,21 @@ func TestPessimisticAutoCommitTxn(t *testing.T) { tk.MustExec("set tidb_txn_mode = 'pessimistic'") tk.MustExec("drop table if exists t") - tk.MustExec("create table t (i int)") + tk.MustExec("create table t (i int primary key)") tk.MustExec("insert into t values (1)") tk.MustExec("set autocommit = on") rows := tk.MustQuery("explain update t set i = -i").Rows() explain := fmt.Sprintf("%v", rows[1]) require.NotRegexp(t, ".*SelectLock.*", explain) + rows = tk.MustQuery("explain update t set i = -i where i = -1").Rows() + explain = fmt.Sprintf("%v", rows[1]) + require.Regexp(t, ".*handle:-1.*", explain) + require.NotRegexp(t, ".*handle:-1, lock.*", explain) + rows = tk.MustQuery("explain update t set i = -i where i in (-1, 1)").Rows() + explain = fmt.Sprintf("%v", rows[1]) + require.Regexp(t, ".*handle:\\[-1 1\\].*", explain) + require.NotRegexp(t, ".*handle:\\[-1 1\\].*, lock.*", explain) originCfg := config.GetGlobalConfig() defer config.StoreGlobalConfig(originCfg) @@ -3091,6 +3099,12 @@ func TestPessimisticAutoCommitTxn(t *testing.T) { rows = tk.MustQuery("explain update t set i = -i").Rows() explain = fmt.Sprintf("%v", rows[1]) require.Regexp(t, ".*SelectLock.*", explain) + rows = tk.MustQuery("explain update t set i = -i where i = -1").Rows() + explain = fmt.Sprintf("%v", rows[1]) + require.Regexp(t, ".*handle:-1, lock.*", explain) + rows = tk.MustQuery("explain update t set i = -i where i in (-1, 1)").Rows() + explain = fmt.Sprintf("%v", rows[1]) + require.Regexp(t, ".*handle:\\[-1 1\\].*, lock.*", explain) } func TestPessimisticLockOnPartition(t *testing.T) {