Skip to content

Commit 0b6987f

Browse files
committed
sql: rewind txn sequence number in internal executor
If the internal executor is used by a user-initiated query, then it shares the same transaction as the user's query. In that case, it's important to step back the sequence number so that a single statement does not read its own writes. Release note: None
1 parent a598a75 commit 0b6987f

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

pkg/sql/conn_executor_exec.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,18 @@ func (ex *connExecutor) execStmtInOpenState(
961961
// single/common function. That would be where the stepping mode
962962
// gets enabled once for all SQL statements executed "underneath".
963963
prevSteppingMode := ex.state.mu.txn.ConfigureStepping(ctx, kv.SteppingEnabled)
964-
defer func() { _ = ex.state.mu.txn.ConfigureStepping(ctx, prevSteppingMode) }()
964+
prevSeqNum := ex.state.mu.txn.GetReadSeqNum()
965+
defer func() {
966+
_ = ex.state.mu.txn.ConfigureStepping(ctx, prevSteppingMode)
967+
968+
// If this is an internal executor that is running on behalf of an outer
969+
// txn, then we need to step back the txn so that the outer executor uses
970+
// the proper sequence number.
971+
if ex.executorType == executorTypeInternal && ex.extraTxnState.fromOuterTxn {
972+
err := ex.state.mu.txn.SetReadSeqNum(prevSeqNum)
973+
retEv, retPayload, retErr = makeErrEvent(err)
974+
}
975+
}()
965976

966977
// Then we create a sequencing point.
967978
//

pkg/sql/conn_executor_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,8 @@ SELECT IF(x::INT::FLOAT = x,
450450
'NOOPE', 'insert saw its own writes: ' || x::STRING || ' (it is halloween today)')::FLOAT)
451451
+ 0.1
452452
FROM t.test
453+
-- the function used here is implemented by using the internal executor.
454+
WHERE has_table_privilege('root', ((x+.1)/(x+1) + 1)::int::oid, 'INSERT') IS NULL
453455
`); err != nil {
454456
t.Fatal(err)
455457
}

0 commit comments

Comments
 (0)