Skip to content

Commit

Permalink
do not lock table if lock no rows (#20431)
Browse files Browse the repository at this point in the history
do not lock table if lock no rows

Approved by: @m-schen, @aressu1985, @aunjgr, @sukki37
  • Loading branch information
ouyuanning authored Nov 29, 2024
1 parent 2bb1f98 commit 83ffac2
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 18 deletions.
7 changes: 3 additions & 4 deletions pkg/sql/colexec/lockop/lock_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -1014,10 +1014,6 @@ func lockTalbeIfLockCountIsZero(
}
for idx := 0; idx < len(lockOp.targets); idx++ {
target := lockOp.targets[idx]
// do not lock table or rows at the end for hidden table
if !target.lockTableAtTheEnd {
continue
}
if target.lockRows != nil {
vec, free, err := colexec.GetReadonlyResultFromNoColumnExpression(proc, target.lockRows)
if err != nil {
Expand All @@ -1039,6 +1035,9 @@ func lockTalbeIfLockCountIsZero(
return err
}
} else {
if !target.lockTableAtTheEnd {
continue
}
err := LockTable(lockOp.engine, proc, target.tableID, target.primaryColumnType, false)
if err != nil {
return err
Expand Down
5 changes: 1 addition & 4 deletions pkg/sql/plan/opt_misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1155,9 +1155,6 @@ func (builder *QueryBuilder) lockTableIfLockNoRowsAtTheEndForDelAndUpdate() (err
return
}
tableDef := baseNode.TableDef
if !getLockTableAtTheEnd(tableDef) {
return
}
objRef := baseNode.ObjRef
tableIDs := make(map[uint64]bool)
tableIDs[tableDef.TblId] = true
Expand Down Expand Up @@ -1229,7 +1226,7 @@ func (builder *QueryBuilder) lockTableIfLockNoRowsAtTheEndForDelAndUpdate() (err
}

lockTarget.LockRows = lockRows
lockTarget.LockTableAtTheEnd = true
lockTarget.LockTableAtTheEnd = false
}

return
Expand Down
1 change: 0 additions & 1 deletion pkg/sql/plan/query_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2709,7 +2709,6 @@ func (builder *QueryBuilder) bindSelect(stmt *tree.Select, ctx *BindContext, isR
PrimaryColTyp: pkTyp,
Block: true,
RefreshTsIdxInBat: -1, //unsupport now
LockTableAtTheEnd: getLockTableAtTheEnd(tableDef),
}
if tableDef.Partition != nil {
partTableIDs, _ := getPartTableIdsAndNames(builder.compCtx, objRef, tableDef)
Expand Down
17 changes: 9 additions & 8 deletions pkg/sql/plan/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2711,11 +2711,12 @@ func offsetToString(offset int) string {
return fmt.Sprintf("+%02d:%02d", hours, minutes)
}

func getLockTableAtTheEnd(tableDef *TableDef) bool {
if tableDef.Pkey.PkeyColName == catalog.FakePrimaryKeyColName || //fake pk, skip
tableDef.Partition != nil || // unsupport partition table
len(tableDef.Pkey.Names) > 1 { // unsupport multi-column primary key
return false
}
return !strings.HasPrefix(tableDef.Name, catalog.IndexTableNamePrefix)
}
// do not lock table if lock no rows now.
// if need to lock table, uncomment these codes
// func getLockTableAtTheEnd(tableDef *TableDef) bool {
// if tableDef.Pkey.PkeyColName == catalog.FakePrimaryKeyColName || //fake pk, skip
// tableDef.Partition != nil { // unsupport partition table
// return false
// }
// return !strings.HasPrefix(tableDef.Name, catalog.IndexTableNamePrefix)
// }
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,6 @@ select * from su_06 where c1>=2 for update;
use select_for_update;
prepare stmt1 from 'delete from su_06 where c3 in (?)';
set @var = 3;
-- @wait:0:commit
execute stmt1 using @var;
select * from su_06;
-- @session}
Expand Down

0 comments on commit 83ffac2

Please sign in to comment.