@@ -2560,18 +2560,18 @@ func (ex *connExecutor) commitSQLTransactionInternal(ctx context.Context) (retEr
2560
2560
2561
2561
ex .extraTxnState .prepStmtsNamespace .closePortals (ctx , & ex .extraTxnState .prepStmtsNamespaceMemAcc )
2562
2562
2563
- // We need to step the transaction's internal read sequence before committing
2564
- // if it has stepping enabled. If it doesn't have stepping enabled, then we
2565
- // just set the stepping mode back to what it was.
2563
+ // We need to step the transaction's read sequence before committing if it has
2564
+ // stepping enabled. If it doesn't have stepping enabled, then we just set the
2565
+ // stepping mode back to what it was.
2566
2566
//
2567
- // Even if we do step the transaction's internal read sequence, we do not
2568
- // advance its external read timestamp (applicable only to read committed
2569
- // transactions). This is because doing so is not needed before committing,
2570
- // and it would cause the transaction to commit at a higher timestamp than
2571
- // necessary. On heavily contended workloads like the one from #109628, this
2572
- // can cause unnecessary write-write contention between transactions by
2573
- // inflating the contention footprint of each transaction (i.e. the duration
2574
- // measured in MVCC time that the transaction holds locks).
2567
+ // Even if we do step the transaction's read sequence, we do not advance its
2568
+ // read timestamp (applicable only to read committed transactions). This is
2569
+ // because doing so is not needed before committing, and it would cause the
2570
+ // transaction to commit at a higher timestamp than necessary. On heavily
2571
+ // contended workloads like the one from #109628, this can cause unnecessary
2572
+ // write-write contention between transactions by inflating the contention
2573
+ // footprint of each transaction (i.e. the duration measured in MVCC time that
2574
+ // the transaction holds locks).
2575
2575
prevSteppingMode := ex .state .mu .txn .ConfigureStepping (ctx , kv .SteppingEnabled )
2576
2576
if prevSteppingMode == kv .SteppingEnabled {
2577
2577
if err := ex .state .mu .txn .Step (ctx , false /* allowReadTimestampStep */ ); err != nil {
@@ -2722,6 +2722,14 @@ func (ex *connExecutor) rollbackSQLTransaction(
2722
2722
func (ex * connExecutor ) dispatchReadCommittedStmtToExecutionEngine (
2723
2723
ctx context.Context , p * planner , res RestrictedCommandResult ,
2724
2724
) error {
2725
+ if ex .executorType == executorTypeInternal {
2726
+ // Because we step the read timestamp below, this is not safe to call within
2727
+ // internal executor.
2728
+ return errors .AssertionFailedf (
2729
+ "call of dispatchReadCommittedStmtToExecutionEngine within internal executor" ,
2730
+ )
2731
+ }
2732
+
2725
2733
getPausablePortalInfo := func () * portalPauseInfo {
2726
2734
if p != nil && p .pausablePortal != nil {
2727
2735
return p .pausablePortal .pauseInfo
@@ -2749,7 +2757,17 @@ func (ex *connExecutor) dispatchReadCommittedStmtToExecutionEngine(
2749
2757
ex .sessionTracing .TraceRetryInformation (
2750
2758
ctx , "statement" , p .autoRetryStmtCounter , p .autoRetryStmtReason ,
2751
2759
)
2760
+ // Step both the sequence number and the read timestamp so that we can see
2761
+ // the results of the conflicting transactions that caused us to fail and
2762
+ // any other transactions that occurred in the meantime.
2763
+ if err := ex .state .mu .txn .Step (ctx , true /* allowReadTimestampStep */ ); err != nil {
2764
+ return err
2765
+ }
2766
+ // Also step statement_timestamp so that any SQL using it is up-to-date.
2767
+ stmtTS := ex .server .cfg .Clock .PhysicalTime ()
2768
+ p .extendedEvalCtx .StmtTimestamp = stmtTS
2752
2769
}
2770
+
2753
2771
bufferPos := res .BufferedResultsLen ()
2754
2772
if err = ex .dispatchToExecutionEngine (ctx , p , res ); err != nil {
2755
2773
return err
@@ -2802,9 +2820,6 @@ func (ex *connExecutor) dispatchReadCommittedStmtToExecutionEngine(
2802
2820
if err := ex .state .mu .txn .PrepareForPartialRetry (ctx ); err != nil {
2803
2821
return err
2804
2822
}
2805
- if err := ex .state .mu .txn .Step (ctx , false /* allowReadTimestampStep */ ); err != nil {
2806
- return err
2807
- }
2808
2823
p .autoRetryStmtCounter ++
2809
2824
p .autoRetryStmtReason = maybeRetriableErr
2810
2825
if ppInfo := getPausablePortalInfo (); ppInfo != nil {
0 commit comments