Skip to content

Commit a43f62d

Browse files
authored
Merge pull request #160476 from cockroachdb/blathers/backport-release-25.4-160346
release-25.4: sql: step read seq on rollback
2 parents c40a8f3 + f03bed1 commit a43f62d

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

pkg/sql/conn_executor_exec.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2113,6 +2113,18 @@ func (ex *connExecutor) execStmtInOpenStateWithPausablePortal(
21132113
return nil, nil, nil
21142114
}
21152115

2116+
func (ex *connExecutor) stepReadSequence(ctx context.Context) error {
2117+
prevSteppingMode := ex.state.mu.txn.ConfigureStepping(ctx, kv.SteppingEnabled)
2118+
if prevSteppingMode == kv.SteppingEnabled {
2119+
if err := ex.state.mu.txn.Step(ctx, false /* allowReadTimestampStep */); err != nil {
2120+
return err
2121+
}
2122+
} else {
2123+
ex.state.mu.txn.ConfigureStepping(ctx, prevSteppingMode)
2124+
}
2125+
return nil
2126+
}
2127+
21162128
// handleAOST gets the AsOfSystemTime clause from the statement, and sets
21172129
// the timestamps of the transaction accordingly.
21182130
func (ex *connExecutor) handleAOST(ctx context.Context, stmt tree.Statement) error {
@@ -2385,13 +2397,8 @@ func (ex *connExecutor) commitSQLTransactionInternal(ctx context.Context) (retEr
23852397
// write-write contention between transactions by inflating the contention
23862398
// footprint of each transaction (i.e. the duration measured in MVCC time that
23872399
// the transaction holds locks).
2388-
prevSteppingMode := ex.state.mu.txn.ConfigureStepping(ctx, kv.SteppingEnabled)
2389-
if prevSteppingMode == kv.SteppingEnabled {
2390-
if err := ex.state.mu.txn.Step(ctx, false /* allowReadTimestampStep */); err != nil {
2391-
return err
2392-
}
2393-
} else {
2394-
ex.state.mu.txn.ConfigureStepping(ctx, prevSteppingMode)
2400+
if err := ex.stepReadSequence(ctx); err != nil {
2401+
return err
23952402
}
23962403

23972404
if err := ex.createJobs(ctx); err != nil {
@@ -2516,7 +2523,14 @@ func (ex *connExecutor) rollbackSQLTransaction(
25162523
isKVTxnOpen = ex.state.mu.txn.IsOpen()
25172524
}
25182525
if isKVTxnOpen {
2519-
if err := ex.state.mu.txn.Rollback(ctx); err != nil {
2526+
// Step the read sequence before rolling back because the read sequence
2527+
// may be in the span reverted by a savepoint.
2528+
err := ex.stepReadSequence(ctx)
2529+
err = errors.CombineErrors(err, ex.state.mu.txn.Rollback(ctx))
2530+
if err != nil {
2531+
if buildutil.CrdbTestBuild && errors.IsAssertionFailure(err) {
2532+
log.Dev.Fatalf(ctx, "txn rollback failed: %+v", err)
2533+
}
25202534
log.Dev.Warningf(ctx, "txn rollback failed: %s", err)
25212535
}
25222536
}

pkg/sql/logictest/testdata/logic_test/savepoints

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,22 @@ SELECT * FROM a
3333

3434
statement ok
3535
COMMIT
36+
37+
# Regression test for https://github.com/cockroachdb/cockroach/issues/135118.
38+
# Rolling back a savepoint before rolling back the transaction caused the
39+
# transaction rollback to fail.
40+
41+
statement ok
42+
BEGIN
43+
44+
statement ok
45+
SAVEPOINT sp1
46+
47+
statement ok
48+
INSERT INTO a(id) VALUES (1);
49+
50+
statement ok
51+
ROLLBACK TO SAVEPOINT sp1
52+
53+
statement ok
54+
ROLLBACK

0 commit comments

Comments
 (0)