diff --git a/session/tidb.go b/session/tidb.go index eaa5aedfbe39d..1d70c164aa7e9 100644 --- a/session/tidb.go +++ b/session/tidb.go @@ -209,11 +209,14 @@ func runStmt(ctx context.Context, sctx sessionctx.Context, s sqlexec.Statement) defer span1.Finish() } se := sctx.(*session) + sessVars := se.GetSessionVars() + // Save origTxnCtx here to avoid it reset in the transaction retry. + origTxnCtx := sessVars.TxnCtx defer func() { // If it is not a select statement, we record its slow log here, // then it could include the transaction commit time. if rs == nil { - s.(*executor.ExecStmt).LogSlowQuery(se.GetSessionVars().TxnCtx.StartTS, err != nil) + s.(*executor.ExecStmt).LogSlowQuery(origTxnCtx.StartTS, err != nil) } }() @@ -222,7 +225,6 @@ func runStmt(ctx context.Context, sctx sessionctx.Context, s sqlexec.Statement) return nil, err } rs, err = s.Exec(ctx) - sessVars := se.GetSessionVars() // All the history should be added here. sessVars.TxnCtx.StatementCount++ if !s.IsReadOnly(sessVars) {