diff --git a/pkg/sql/conn_executor_exec.go b/pkg/sql/conn_executor_exec.go index 02e520bfa360..1f599453000d 100644 --- a/pkg/sql/conn_executor_exec.go +++ b/pkg/sql/conn_executor_exec.go @@ -1305,12 +1305,11 @@ func (ex *connExecutor) commitSQLTransaction( ex.extraTxnState.idleLatency += ex.statsCollector.PhaseTimes(). GetIdleLatency(ex.statsCollector.PreviousPhaseTimes()) if ex.sessionData().InjectRetryErrorsOnCommitEnabled && ast.StatementTag() == "COMMIT" { - if ex.planner.Txn().Epoch() < ex.state.lastEpoch+numTxnRetryErrors { + if ex.state.injectedTxnRetryCounter < numTxnRetryErrors { retryErr := ex.state.mu.txn.GenerateForcedRetryableErr( ctx, "injected by `inject_retry_errors_on_commit_enabled` session variable") + ex.state.injectedTxnRetryCounter++ return ex.makeErrEvent(retryErr, ast) - } else { - ex.state.lastEpoch = ex.planner.Txn().Epoch() } } ex.phaseTimes.SetSessionPhaseTime(sessionphase.SessionStartTransactionCommit, timeutil.Now()) @@ -1740,12 +1739,11 @@ func (ex *connExecutor) dispatchToExecutionEngine( isSetOrShow := stmt.AST.StatementTag() == "SET" || stmt.AST.StatementTag() == "SHOW" if ex.sessionData().InjectRetryErrorsEnabled && !isSetOrShow && planner.Txn().Sender().TxnStatus() == roachpb.PENDING { - if planner.Txn().Epoch() < ex.state.lastEpoch+numTxnRetryErrors { + if ex.state.injectedTxnRetryCounter < numTxnRetryErrors { retryErr := planner.Txn().GenerateForcedRetryableErr( ctx, "injected by `inject_retry_errors_enabled` session variable") res.SetError(retryErr) - } else { - ex.state.lastEpoch = planner.Txn().Epoch() + ex.state.injectedTxnRetryCounter++ } } } diff --git a/pkg/sql/txn_state.go b/pkg/sql/txn_state.go index e40481319388..192ddfa9007d 100644 --- a/pkg/sql/txn_state.go +++ b/pkg/sql/txn_state.go @@ -22,7 +22,6 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sessiondatapb" - "github.com/cockroachdb/cockroach/pkg/storage/enginepb" "github.com/cockroachdb/cockroach/pkg/util/hlc" "github.com/cockroachdb/cockroach/pkg/util/metric" "github.com/cockroachdb/cockroach/pkg/util/mon" @@ -108,8 +107,10 @@ type txnState struct { // through the use of AS OF SYSTEM TIME. isHistorical bool - // lastEpoch is the last observed epoch in the current txn. - lastEpoch enginepb.TxnEpoch + // injectedTxnRetryCounter keeps track of how many errors have been + // injected in this transaction with the inject_retry_errors_enabled + // flag. + injectedTxnRetryCounter int // mon tracks txn-bound objects like the running state of // planNode in the midst of performing a computation. @@ -191,7 +192,7 @@ func (ts *txnState) resetForNewSQLTxn( // Reset state vars to defaults. ts.sqlTimestamp = sqlTimestamp ts.isHistorical = false - ts.lastEpoch = 0 + ts.injectedTxnRetryCounter = 0 // Create a context for this transaction. It will include a root span that // will contain everything executed as part of the upcoming SQL txn, including