Skip to content

Commit

Permalink
sql/opt: add locking durability
Browse files Browse the repository at this point in the history
In addition to strength and wait policy, we now add a third property to
locks: durability. Locks with `LockDurabilityGuaranteed` are guaranteed
to be held until commit (if the transaction commits). Durable locks must
be used when correctness depends on locking. This is never the case
under our `SERIALIZABLE` isolation, but under `SNAPSHOT` and `READ
COMMITTED` isolation it will be the case for `SELECT FOR UPDATE`
statements, which will be the first users of durable locks.

This commit adds locking durability to the optimizer and `EXPLAIN`
output, but does not plumb it into KV yet. It will be used in the next
commit to temporarily disallow `SELECT FOR UPDATE` statements under
`SNAPSHOT` and `READ COMMITTED` isolation.

Release note: None
  • Loading branch information
michae2 committed May 22, 2023
1 parent 9debca1 commit 4ec407f
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pkg/sql/catalog/descpb/locking.proto
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ enum ScanLockingStrength {
// on each key scanned.
FOR_UPDATE = 4;
}

// LockingWaitPolicy controls the policy used for handling conflicting locks
// held by other active transactions when attempting to lock rows due to FOR
// UPDATE/SHARE clauses (i.e. it represents the NOWAIT and SKIP LOCKED options).
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/opt/exec/execbuilder/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,7 @@ func (b *Builder) canAutoCommit(rel memo.RelExpr) bool {

// forUpdateLocking is the row-level locking mode used by mutations during their
// initial row scan, when such locking is deemed desirable. The locking mode is
// equivalent that used by a SELECT ... FOR UPDATE statement.
// equivalent that used by a SELECT ... FOR UPDATE statement except not durable.
var forUpdateLocking = opt.Locking{Strength: tree.ForUpdate}

// shouldApplyImplicitLockingToMutationInput determines whether or not the
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/opt/exec/execbuilder/relational.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ func (b *Builder) scanParams(

locking := scan.Locking
if b.forceForUpdateLocking {
locking = forUpdateLocking
locking = locking.Max(forUpdateLocking)
}
b.ContainsNonDefaultKeyLocking = b.ContainsNonDefaultKeyLocking || locking.IsLocking()

Expand Down
Loading

0 comments on commit 4ec407f

Please sign in to comment.