From f3148da27fe2cf501c18356377d321d2a9216783 Mon Sep 17 00:00:00 2001 From: Jack Yu Date: Wed, 17 Oct 2018 10:19:04 +0800 Subject: [PATCH] executor: add an variable to compatible with MySQL insert for OGG (#7863) --- executor/insert_common.go | 4 +++- executor/set_test.go | 5 +++++ executor/write_test.go | 14 ++++++++++++++ session/session.go | 1 + sessionctx/variable/session.go | 5 +++++ sessionctx/variable/sysvar.go | 1 + sessionctx/variable/tidb_vars.go | 5 +++++ sessionctx/variable/varsutil.go | 2 +- 8 files changed, 35 insertions(+), 2 deletions(-) diff --git a/executor/insert_common.go b/executor/insert_common.go index 2517420943feb..8e53f899d72c5 100644 --- a/executor/insert_common.go +++ b/executor/insert_common.go @@ -516,7 +516,9 @@ func (e *InsertValues) batchCheckAndInsert(rows [][]types.Datum, addRecord func( } func (e *InsertValues) addRecord(row []types.Datum) (int64, error) { - e.ctx.Txn().SetOption(kv.PresumeKeyNotExists, nil) + if !e.ctx.GetSessionVars().ConstraintCheckInPlace { + e.ctx.Txn().SetOption(kv.PresumeKeyNotExists, nil) + } h, err := e.Table.AddRecord(e.ctx, row, false) e.ctx.Txn().DelOption(kv.PresumeKeyNotExists) if err != nil { diff --git a/executor/set_test.go b/executor/set_test.go index 147bc22d16636..1bba2c11f2f67 100644 --- a/executor/set_test.go +++ b/executor/set_test.go @@ -234,6 +234,11 @@ func (s *testSuite) TestSetVar(c *C) { tk.MustQuery(`select @@tidb_force_priority;`).Check(testkit.Rows("NO_PRIORITY")) _, err = tk.Exec(`set global tidb_force_priority = ""`) c.Assert(err, NotNil) + + tk.MustExec("set tidb_constraint_check_in_place = 1") + tk.MustQuery(`select @@session.tidb_constraint_check_in_place;`).Check(testkit.Rows("1")) + tk.MustExec("set global tidb_constraint_check_in_place = 0") + tk.MustQuery(`select @@global.tidb_constraint_check_in_place;`).Check(testkit.Rows("0")) } func (s *testSuite) TestSetCharset(c *C) { diff --git a/executor/write_test.go b/executor/write_test.go index 3079c9c35f001..4b5ab134f6e51 100644 --- a/executor/write_test.go +++ b/executor/write_test.go @@ -2091,3 +2091,17 @@ func (s *testSuite) TestRebaseIfNeeded(c *C) { tk.MustExec(`insert into t (b) values (6);`) tk.MustQuery(`select a from t where b = 6;`).Check(testkit.Rows("30003")) } + +func (s *testSuite) TestDeferConstraintCheckForInsert(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec(`use test`) + tk.MustExec(`drop table if exists t;create table t (i int key);`) + tk.MustExec(`insert t values (1);`) + tk.MustExec(`set tidb_constraint_check_in_place = 1;`) + tk.MustExec(`begin;`) + _, err := tk.Exec(`insert t values (1);`) + c.Assert(err, NotNil) + tk.MustExec(`update t set i = 2 where i = 1;`) + tk.MustExec(`commit;`) + tk.MustQuery(`select * from t;`).Check(testkit.Rows("2")) +} diff --git a/session/session.go b/session/session.go index f71ef274f6855..996c75d5a4b30 100644 --- a/session/session.go +++ b/session/session.go @@ -1281,6 +1281,7 @@ const loadCommonGlobalVarsSQL = "select HIGH_PRIORITY * from mysql.global_variab variable.TiDBHashAggPartialConcurrency + quoteCommaQuote + variable.TiDBHashAggFinalConcurrency + quoteCommaQuote + variable.TiDBBackoffLockFast + quoteCommaQuote + + variable.TiDBConstraintCheckInPlace + quoteCommaQuote + variable.TiDBDDLReorgWorkerCount + quoteCommaQuote + variable.TiDBOptInSubqUnFolding + quoteCommaQuote + variable.TiDBDistSQLScanConcurrency + quoteCommaQuote + diff --git a/sessionctx/variable/session.go b/sessionctx/variable/session.go index 2ce0a2fb5a447..954a72a568718 100644 --- a/sessionctx/variable/session.go +++ b/sessionctx/variable/session.go @@ -308,6 +308,9 @@ type SessionVars struct { // HashJoin. EnableRadixJoin bool + // ConstraintCheckInPlace indicates whether to check the constraint when the SQL executing. + ConstraintCheckInPlace bool + // CommandValue indicates which command current session is doing. CommandValue uint32 } @@ -573,6 +576,8 @@ func (s *SessionVars) SetSystemVar(name string, val string) error { s.IndexSerialScanConcurrency = tidbOptPositiveInt32(val, DefIndexSerialScanConcurrency) case TiDBBackoffLockFast: s.KVVars.BackoffLockFast = tidbOptPositiveInt32(val, kv.DefBackoffLockFast) + case TiDBConstraintCheckInPlace: + s.ConstraintCheckInPlace = TiDBOptOn(val) case TiDBBatchInsert: s.BatchInsert = TiDBOptOn(val) case TiDBBatchDelete: diff --git a/sessionctx/variable/sysvar.go b/sessionctx/variable/sysvar.go index b35e91cb8a1e6..4e4534935c819 100644 --- a/sessionctx/variable/sysvar.go +++ b/sessionctx/variable/sysvar.go @@ -658,6 +658,7 @@ var defaultSysVars = []*SysVar{ {ScopeGlobal | ScopeSession, TiDBBackoffLockFast, strconv.Itoa(kv.DefBackoffLockFast)}, {ScopeGlobal | ScopeSession, TiDBRetryLimit, strconv.Itoa(DefTiDBRetryLimit)}, {ScopeGlobal | ScopeSession, TiDBDisableTxnAutoRetry, boolToIntStr(DefTiDBDisableTxnAutoRetry)}, + {ScopeGlobal | ScopeSession, TiDBConstraintCheckInPlace, boolToIntStr(DefTiDBConstraintCheckInPlace)}, {ScopeSession, TiDBOptimizerSelectivityLevel, strconv.Itoa(DefTiDBOptimizerSelectivityLevel)}, /* The following variable is defined as session scope but is actually server scope. */ {ScopeSession, TiDBGeneralLog, strconv.Itoa(DefTiDBGeneralLog)}, diff --git a/sessionctx/variable/tidb_vars.go b/sessionctx/variable/tidb_vars.go index bc48d33363bf8..c754743d5ac8b 100644 --- a/sessionctx/variable/tidb_vars.go +++ b/sessionctx/variable/tidb_vars.go @@ -198,6 +198,10 @@ const ( // tidb_enable_radix_join indicates to use radix hash join algorithm to execute // HashJoin. TiDBEnableRadixJoin = "tidb_enable_radix_join" + + // tidb_constraint_check_in_place indicates to check the constraint when the SQL executing. + // It could hurt the performance of bulking insert when it is ON. + TiDBConstraintCheckInPlace = "tidb_constraint_check_in_place" ) // Default TiDB system variable values. @@ -231,6 +235,7 @@ const ( DefTiDBGeneralLog = 0 DefTiDBRetryLimit = 10 DefTiDBDisableTxnAutoRetry = false + DefTiDBConstraintCheckInPlace = false DefTiDBHashJoinConcurrency = 5 DefTiDBProjectionConcurrency = 4 DefTiDBOptimizerSelectivityLevel = 0 diff --git a/sessionctx/variable/varsutil.go b/sessionctx/variable/varsutil.go index b45544288d2a7..a64f0a31a7a67 100644 --- a/sessionctx/variable/varsutil.go +++ b/sessionctx/variable/varsutil.go @@ -281,7 +281,7 @@ func ValidateSetSystemVar(vars *SessionVars, name string, value string) (string, case WarningCount, ErrorCount: return value, ErrReadOnly.GenWithStackByArgs(name) case GeneralLog, TiDBGeneralLog, AvoidTemporalUpgrade, BigTables, CheckProxyUsers, CoreFile, EndMakersInJSON, SQLLogBin, OfflineMode, - PseudoSlaveMode, LowPriorityUpdates, SkipNameResolve, ForeignKeyChecks, SQLSafeUpdates: + PseudoSlaveMode, LowPriorityUpdates, SkipNameResolve, ForeignKeyChecks, SQLSafeUpdates, TiDBConstraintCheckInPlace: if strings.EqualFold(value, "ON") || value == "1" { return "1", nil } else if strings.EqualFold(value, "OFF") || value == "0" {