diff --git a/config/config.go b/config/config.go index 8bb9cf1776920..1ba2c7fbd1595 100644 --- a/config/config.go +++ b/config/config.go @@ -751,6 +751,8 @@ type PessimisticTxn struct { DeadlockHistoryCollectRetryable bool `toml:"deadlock-history-collect-retryable" json:"deadlock-history-collect-retryable"` // PessimisticAutoCommit represents if true it means the auto-commit transactions will be in pessimistic mode. PessimisticAutoCommit AtomicBool `toml:"pessimistic-auto-commit" json:"pessimistic-auto-commit"` + // ConstraintCheckInPlacePessimistic is the default value for the session variable `tidb_constraint_check_in_place_pessimistic` + ConstraintCheckInPlacePessimistic bool `toml:"constraint-check-in-place-pessimistic" json:"constraint-check-in-place-pessimistic"` } // TrxSummary is the config for transaction summary collecting. @@ -772,10 +774,11 @@ func (config *TrxSummary) Valid() error { // DefaultPessimisticTxn returns the default configuration for PessimisticTxn func DefaultPessimisticTxn() PessimisticTxn { return PessimisticTxn{ - MaxRetryCount: 256, - DeadlockHistoryCapacity: 10, - DeadlockHistoryCollectRetryable: false, - PessimisticAutoCommit: *NewAtomicBool(false), + MaxRetryCount: 256, + DeadlockHistoryCapacity: 10, + DeadlockHistoryCollectRetryable: false, + PessimisticAutoCommit: *NewAtomicBool(false), + ConstraintCheckInPlacePessimistic: true, } } @@ -1040,7 +1043,7 @@ var removedConfig = map[string]struct{}{ "log.query-log-max-len": {}, "performance.committer-concurrency": {}, "experimental.enable-global-kill": {}, - "performance.run-auto-analyze": {}, //use tidb_enable_auto_analyze + "performance.run-auto-analyze": {}, // use tidb_enable_auto_analyze // use tidb_enable_prepared_plan_cache, tidb_prepared_plan_cache_size and tidb_prepared_plan_cache_memory_guard_ratio "prepared-plan-cache.enabled": {}, "prepared-plan-cache.capacity": {}, diff --git a/metrics/session.go b/metrics/session.go index 483ebe1826790..11c3bf7a143b8 100644 --- a/metrics/session.go +++ b/metrics/session.go @@ -166,7 +166,7 @@ var ( Namespace: "tidb", Subsystem: "session", Name: "lazy_pessimistic_unique_check_set_count", - Help: "Counter of setting tidb_constraint_check_in_place to false", + Help: "Counter of setting tidb_constraint_check_in_place to false, note that it doesn't count the default value set by tidb config", }, ) ) diff --git a/session/session.go b/session/session.go index 553e529c8a736..d69f7b94b5acb 100644 --- a/session/session.go +++ b/session/session.go @@ -1352,6 +1352,10 @@ func createSessionFunc(store kv.Storage) pools.Factory { if err != nil { return nil, errors.Trace(err) } + err = se.sessionVars.SetSystemVar(variable.TiDBConstraintCheckInPlacePessimistic, variable.On) + if err != nil { + return nil, errors.Trace(err) + } se.sessionVars.CommonGlobalLoaded = true se.sessionVars.InRestrictedSQL = true // Internal session uses default format to prevent memory leak problem. @@ -1378,6 +1382,10 @@ func createSessionWithDomainFunc(store kv.Storage) func(*domain.Domain) (pools.R if err != nil { return nil, errors.Trace(err) } + err = se.sessionVars.SetSystemVar(variable.TiDBConstraintCheckInPlacePessimistic, variable.On) + if err != nil { + return nil, errors.Trace(err) + } se.sessionVars.CommonGlobalLoaded = true se.sessionVars.InRestrictedSQL = true // Internal session uses default format to prevent memory leak problem. diff --git a/sessionctx/variable/sysvar.go b/sessionctx/variable/sysvar.go index d132d90e3b9ed..580a8deca7b7e 100644 --- a/sessionctx/variable/sysvar.go +++ b/sessionctx/variable/sysvar.go @@ -1929,7 +1929,7 @@ var defaultSysVars = []*SysVar{ DDLDiskQuota.Store(TidbOptUint64(val, DefTiDBDDLDiskQuota)) return nil }}, - {Scope: ScopeGlobal | ScopeSession, Name: TiDBConstraintCheckInPlacePessimistic, Value: BoolToOnOff(DefTiDBConstraintCheckInPlacePessimistic), Type: TypeBool, + {Scope: ScopeSession, Name: TiDBConstraintCheckInPlacePessimistic, Value: BoolToOnOff(config.GetGlobalConfig().PessimisticTxn.ConstraintCheckInPlacePessimistic), Type: TypeBool, SetSession: func(s *SessionVars, val string) error { s.ConstraintCheckInPlacePessimistic = TiDBOptOn(val) if !s.ConstraintCheckInPlacePessimistic { diff --git a/sessionctx/variable/tidb_vars.go b/sessionctx/variable/tidb_vars.go index ed7fa9c8e08ad..4c261d8def2e4 100644 --- a/sessionctx/variable/tidb_vars.go +++ b/sessionctx/variable/tidb_vars.go @@ -1070,19 +1070,18 @@ const ( DefTiDBGeneralPlanCacheSize = 100 DefTiDBEnableTiFlashReadForWriteStmt = false // MaxDDLReorgBatchSize is exported for testing. - MaxDDLReorgBatchSize int32 = 10240 - MinDDLReorgBatchSize int32 = 32 - MinExpensiveQueryTimeThreshold uint64 = 10 // 10s - DefTiDBRcWriteCheckTs = false - DefTiDBConstraintCheckInPlacePessimistic = true - DefTiDBForeignKeyChecks = false - DefTiDBAnalyzePartitionConcurrency = 1 - DefTiDBOptRangeMaxSize = 64 * int64(size.MB) // 64 MB - DefTiDBCostModelVer = 1 - DefTiDBServerMemoryLimitSessMinSize = 128 << 20 - DefTiDBMergePartitionStatsConcurrency = 1 - DefTiDBServerMemoryLimitGCTrigger = 0.7 - DefTiDBEnableGOGCTuner = true + MaxDDLReorgBatchSize int32 = 10240 + MinDDLReorgBatchSize int32 = 32 + MinExpensiveQueryTimeThreshold uint64 = 10 // 10s + DefTiDBRcWriteCheckTs = false + DefTiDBForeignKeyChecks = false + DefTiDBAnalyzePartitionConcurrency = 1 + DefTiDBOptRangeMaxSize = 64 * int64(size.MB) // 64 MB + DefTiDBCostModelVer = 1 + DefTiDBServerMemoryLimitSessMinSize = 128 << 20 + DefTiDBMergePartitionStatsConcurrency = 1 + DefTiDBServerMemoryLimitGCTrigger = 0.7 + DefTiDBEnableGOGCTuner = true // DefTiDBGOGCTunerThreshold is to limit TiDBGOGCTunerThreshold. DefTiDBGOGCTunerThreshold float64 = 0.6 DefTiDBOptPrefixIndexSingleScan = true diff --git a/table/tables/index.go b/table/tables/index.go index 9eae5418f71b8..bc1a90f70ba71 100644 --- a/table/tables/index.go +++ b/table/tables/index.go @@ -229,7 +229,8 @@ func (c *index) Create(sctx sessionctx.Context, txn kv.Transaction, indexedValue } if lazyCheck { flags := []kv.FlagsOp{kv.SetPresumeKeyNotExists} - if !vars.ConstraintCheckInPlacePessimistic && vars.TxnCtx.IsPessimistic && vars.InTxn() { + if !vars.ConstraintCheckInPlacePessimistic && vars.TxnCtx.IsPessimistic && vars.InTxn() && + !vars.InRestrictedSQL && vars.ConnectionID > 0 { flags = append(flags, kv.SetNeedConstraintCheckInPrewrite) } err = txn.GetMemBuffer().SetWithFlags(key, idxVal, flags...) diff --git a/table/tables/tables.go b/table/tables/tables.go index 48d162394d739..1961e0cd335e8 100644 --- a/table/tables/tables.go +++ b/table/tables/tables.go @@ -854,7 +854,8 @@ func (t *TableCommon) AddRecord(sctx sessionctx.Context, r []types.Datum, opts . if setPresume { flags := []kv.FlagsOp{kv.SetPresumeKeyNotExists} - if !sessVars.ConstraintCheckInPlacePessimistic && sessVars.TxnCtx.IsPessimistic && sessVars.InTxn() { + if !sessVars.ConstraintCheckInPlacePessimistic && sessVars.TxnCtx.IsPessimistic && sessVars.InTxn() && + !sessVars.InRestrictedSQL && sessVars.ConnectionID > 0 { flags = append(flags, kv.SetNeedConstraintCheckInPrewrite) } err = memBuffer.SetWithFlags(key, value, flags...) diff --git a/tidb-server/main.go b/tidb-server/main.go index a2cc2046d960c..9b1e58b736844 100644 --- a/tidb-server/main.go +++ b/tidb-server/main.go @@ -671,6 +671,7 @@ func setGlobalVars() { variable.SetSysVar(variable.TiDBIsolationReadEngines, strings.Join(cfg.IsolationRead.Engines, ",")) variable.SetSysVar(variable.TiDBEnforceMPPExecution, variable.BoolToOnOff(config.GetGlobalConfig().Performance.EnforceMPP)) variable.MemoryUsageAlarmRatio.Store(cfg.Instance.MemoryUsageAlarmRatio) + variable.SetSysVar(variable.TiDBConstraintCheckInPlacePessimistic, variable.BoolToOnOff(cfg.PessimisticTxn.ConstraintCheckInPlacePessimistic)) if hostname, err := os.Hostname(); err == nil { variable.SetSysVar(variable.Hostname, hostname) }