diff --git a/executor/adapter.go b/executor/adapter.go index 3f1504b654..4cf27e849d 100644 --- a/executor/adapter.go +++ b/executor/adapter.go @@ -763,7 +763,7 @@ func (a *ExecStmt) buildExecutor() (Executor, error) { } e = executorExec.stmtExec } - a.isSelectForUpdate = b.hasLock && (!stmtCtx.InDeleteStmt && !stmtCtx.InUpdateStmt) + a.isSelectForUpdate = b.hasLock && (!stmtCtx.InDeleteStmt && !stmtCtx.InUpdateStmt && !stmtCtx.InInsertStmt) return e, nil } diff --git a/server/tidb_test.go b/server/tidb_test.go index cdabeb1c2d..9c7b0a78e5 100644 --- a/server/tidb_test.go +++ b/server/tidb_test.go @@ -931,3 +931,24 @@ func (ts *tidbTestSuite) TestNullFlag(c *C) { expectFlag := uint16(tmysql.NotNullFlag | tmysql.BinaryFlag) c.Assert(dumpFlag(cols[0].Type, cols[0].Flag), Equals, expectFlag) } + +func (ts *tidbTestSuite) TestPessimisticInsertSelectForUpdate(c *C) { + qctx, err := ts.tidbdrv.OpenCtx(uint64(0), 0, uint8(tmysql.DefaultCollationID), "test", nil) + c.Assert(err, IsNil) + ctx := context.Background() + _, err = Execute(ctx, qctx, "use test;") + c.Assert(err, IsNil) + _, err = Execute(ctx, qctx, "drop table if exists t1, t2") + c.Assert(err, IsNil) + _, err = Execute(ctx, qctx, "create table t1 (id int)") + c.Assert(err, IsNil) + _, err = Execute(ctx, qctx, "create table t2 (id int)") + c.Assert(err, IsNil) + _, err = Execute(ctx, qctx, "insert into t1 select 1") + c.Assert(err, IsNil) + _, err = Execute(ctx, qctx, "begin pessimistic") + c.Assert(err, IsNil) + rs, err := Execute(ctx, qctx, "INSERT INTO t2 (id) select id from t1 where id = 1 for update") + c.Assert(err, IsNil) + c.Assert(rs, IsNil) // should be no delay +}