Skip to content

Commit d015e8f

Browse files
committed
sql, opt: add use_swap_mutations session variable
Release note (sql change): Add new session variable `use_swap_mutations` which controls whether the new update swap and delete swap operators are enabled for use by UPDATE and DELETE statements.
1 parent 76acbd4 commit d015e8f

File tree

11 files changed

+122
-48
lines changed

11 files changed

+122
-48
lines changed

pkg/sql/logictest/testdata/logic_test/information_schema

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4289,6 +4289,7 @@ use_improved_routine_dependency_tracking on
42894289
use_pre_25_2_variadic_builtins off
42904290
use_proc_txn_control_extended_protocol_fix on
42914291
use_soft_limit_for_distribute_scan on
4292+
use_swap_mutations off
42924293
variable_inequality_lookup_join_enabled on
42934294
vector_search_beam_size 32
42944295
vector_search_rerank_multiplier 50

pkg/sql/logictest/testdata/logic_test/pg_catalog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3243,6 +3243,7 @@ use_improved_routine_dependency_tracking on
32433243
use_pre_25_2_variadic_builtins off NULL NULL NULL string
32443244
use_proc_txn_control_extended_protocol_fix on NULL NULL NULL string
32453245
use_soft_limit_for_distribute_scan on NULL NULL NULL string
3246+
use_swap_mutations off NULL NULL NULL string
32463247
variable_inequality_lookup_join_enabled on NULL NULL NULL string
32473248
vector_search_beam_size 32 NULL NULL NULL string
32483249
vector_search_rerank_multiplier 50 NULL NULL NULL string
@@ -3492,6 +3493,7 @@ use_improved_routine_dependency_tracking on
34923493
use_pre_25_2_variadic_builtins off NULL user NULL off off
34933494
use_proc_txn_control_extended_protocol_fix on NULL user NULL on on
34943495
use_soft_limit_for_distribute_scan on NULL user NULL on on
3496+
use_swap_mutations off NULL user NULL off off
34953497
variable_inequality_lookup_join_enabled on NULL user NULL on on
34963498
vector_search_beam_size 32 NULL user NULL 32 32
34973499
vector_search_rerank_multiplier 50 NULL user NULL 50 50
@@ -3733,6 +3735,7 @@ use_improved_routine_dependency_tracking NULL NULL
37333735
use_pre_25_2_variadic_builtins NULL NULL NULL NULL NULL
37343736
use_proc_txn_control_extended_protocol_fix NULL NULL NULL NULL NULL
37353737
use_soft_limit_for_distribute_scan NULL NULL NULL NULL NULL
3738+
use_swap_mutations NULL NULL NULL NULL NULL
37363739
variable_inequality_lookup_join_enabled NULL NULL NULL NULL NULL
37373740
vector_search_beam_size NULL NULL NULL NULL NULL
37383741
vector_search_rerank_multiplier NULL NULL NULL NULL NULL

pkg/sql/logictest/testdata/logic_test/show_source

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ use_improved_routine_dependency_tracking on
260260
use_pre_25_2_variadic_builtins off
261261
use_proc_txn_control_extended_protocol_fix on
262262
use_soft_limit_for_distribute_scan on
263+
use_swap_mutations off
263264
variable_inequality_lookup_join_enabled on
264265
vector_search_beam_size 32
265266
vector_search_rerank_multiplier 50

pkg/sql/logictest/testdata/logic_test/swap_mutation

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# LogicTest: local
22

3+
statement ok
4+
SET use_swap_mutations = on
5+
36
# Test that unique indexes work correctly with swap mutations.
47

58
statement ok
@@ -180,3 +183,6 @@ SELECT * FROM mno ORDER BY m
180183
----
181184
3 3 3
182185
4 4 4
186+
187+
statement ok
188+
RESET use_swap_mutations

pkg/sql/opt/exec/execbuilder/testdata/swap_mutation

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# LogicTest: local
22

3+
statement ok
4+
SET use_swap_mutations = on
5+
36
# Test that update swap and delete swap work correctly and use a single batch.
47

58
# Test swap mutations with only primary key columns.
@@ -730,3 +733,6 @@ SELECT * FROM rstuv ORDER BY r, s
730733
----
731734
5 6 NULL 8 NULL
732735
15 16 NULL 18 19
736+
737+
statement ok
738+
RESET use_swap_mutations

pkg/sql/opt/memo/memo.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ type Memo struct {
216216
clampInequalitySelectivity bool
217217
useMaxFrequencySelectivity bool
218218
usingHintInjection bool
219+
useSwapMutations bool
219220

220221
// txnIsoLevel is the isolation level under which the plan was created. This
221222
// affects the planning of some locking operations, so it must be included in
@@ -332,6 +333,7 @@ func (m *Memo) Init(ctx context.Context, evalCtx *eval.Context) {
332333
clampInequalitySelectivity: evalCtx.SessionData().OptimizerClampInequalitySelectivity,
333334
useMaxFrequencySelectivity: evalCtx.SessionData().OptimizerUseMaxFrequencySelectivity,
334335
usingHintInjection: evalCtx.Planner != nil && evalCtx.Planner.UsingHintInjection(),
336+
useSwapMutations: evalCtx.SessionData().UseSwapMutations,
335337
txnIsoLevel: evalCtx.TxnIsoLevel,
336338
}
337339
m.metadata.Init()
@@ -512,6 +514,7 @@ func (m *Memo) IsStale(
512514
m.clampInequalitySelectivity != evalCtx.SessionData().OptimizerClampInequalitySelectivity ||
513515
m.useMaxFrequencySelectivity != evalCtx.SessionData().OptimizerUseMaxFrequencySelectivity ||
514516
m.usingHintInjection != (evalCtx.Planner != nil && evalCtx.Planner.UsingHintInjection()) ||
517+
m.useSwapMutations != evalCtx.SessionData().UseSwapMutations ||
515518
m.txnIsoLevel != evalCtx.TxnIsoLevel {
516519
return true, nil
517520
}

pkg/sql/opt/norm/mutation_funcs.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,10 @@ func (c *CustomFuncs) SimplifyPartialIndexProjections(
226226
// CanUseSwapMutation checks whether an Update or Delete can become an
227227
// UpdateSwap or DeleteSwap, respectively.
228228
func (c *CustomFuncs) CanUseSwapMutation(op opt.Operator, private *memo.MutationPrivate) bool {
229+
if !c.f.evalCtx.SessionData().UseSwapMutations {
230+
return false
231+
}
232+
229233
// Checks are not currently handled by UpdateSwap or DeleteSwap.
230234
if !private.CheckCols.IsEmpty() {
231235
return false

0 commit comments

Comments
 (0)