Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reduce NOWAIT usage to tables with unique keys for foreign key plans #14772

Merged
merged 2 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions go/vt/vtgate/planbuilder/operators/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func createOperatorFromDelete(ctx *plancontext.PlanningContext, deleteStmt *sqlp
panic(vterrors.VT12001("foreign keys management at vitess with limit"))
}

return createFkCascadeOpForDelete(ctx, delOp, delClone, childFks)
return createFkCascadeOpForDelete(ctx, delOp, delClone, childFks, vindexTable)
}

func createDeleteOperator(
Expand Down Expand Up @@ -152,7 +152,7 @@ func createDeleteOperator(
return sqc.getRootOperator(route, nil)
}

func createFkCascadeOpForDelete(ctx *plancontext.PlanningContext, parentOp Operator, delStmt *sqlparser.Delete, childFks []vindexes.ChildFKInfo) Operator {
func createFkCascadeOpForDelete(ctx *plancontext.PlanningContext, parentOp Operator, delStmt *sqlparser.Delete, childFks []vindexes.ChildFKInfo, deletedTbl *vindexes.Table) Operator {
var fkChildren []*FkChild
var selectExprs []sqlparser.SelectExpr
for _, fk := range childFks {
Expand All @@ -169,7 +169,7 @@ func createFkCascadeOpForDelete(ctx *plancontext.PlanningContext, parentOp Opera
fkChildren = append(fkChildren,
createFkChildForDelete(ctx, fk, offsets))
}
selectionOp := createSelectionOp(ctx, selectExprs, delStmt.TableExprs, delStmt.Where, nil, nil, sqlparser.ForUpdateLockNoWait)
selectionOp := createSelectionOp(ctx, selectExprs, delStmt.TableExprs, delStmt.Where, nil, nil, getUpdateLock(deletedTbl))

return &FkCascade{
Selection: selectionOp,
Expand Down
20 changes: 17 additions & 3 deletions go/vt/vtgate/planbuilder/operators/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func createFKCascadeOp(ctx *plancontext.PlanningContext, parentOp Operator, updS
fkChildren = append(fkChildren, fkChild)
}

selectionOp := createSelectionOp(ctx, selectExprs, updStmt.TableExprs, updStmt.Where, updStmt.OrderBy, nil, sqlparser.ForUpdateLockNoWait)
selectionOp := createSelectionOp(ctx, selectExprs, updStmt.TableExprs, updStmt.Where, updStmt.OrderBy, nil, getUpdateLock(updatedTable))

return &FkCascade{
Selection: selectionOp,
Expand Down Expand Up @@ -658,7 +658,21 @@ func createFkVerifyOpForParentFKForUpdate(ctx *plancontext.PlanningContext, upda
sqlparser.NewWhere(sqlparser.WhereClause, whereCond),
nil,
sqlparser.NewLimitWithoutOffset(1),
sqlparser.ForShareLockNoWait)
getVerifyLock(updatedTable))
}

func getVerifyLock(vTbl *vindexes.Table) sqlparser.Lock {
if len(vTbl.UniqueKeys) > 0 {
return sqlparser.ForShareLockNoWait
}
return sqlparser.ForShareLock
}

func getUpdateLock(vTbl *vindexes.Table) sqlparser.Lock {
if len(vTbl.UniqueKeys) > 0 {
return sqlparser.ForUpdateLockNoWait
}
return sqlparser.ForUpdateLock
}

// Each child foreign key constraint is verified by a join query of the form:
Expand Down Expand Up @@ -728,7 +742,7 @@ func createFkVerifyOpForChildFKForUpdate(ctx *plancontext.PlanningContext, updat
sqlparser.NewWhere(sqlparser.WhereClause, whereCond),
nil,
sqlparser.NewLimitWithoutOffset(1),
sqlparser.ForShareLockNoWait)
getVerifyLock(updatedTable))
}

// nullSafeNotInComparison is used to compare the child columns in the foreign key constraint aren't the same as the updateExpressions exactly.
Expand Down
Loading
Loading