Skip to content

Commit

Permalink
variable: rename tmp_table_size to tidb_tmp_table_max_size (pingc…
Browse files Browse the repository at this point in the history
  • Loading branch information
djshow832 authored Oct 28, 2021
1 parent 97776fd commit ce8e734
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 37 deletions.
30 changes: 28 additions & 2 deletions executor/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -933,16 +933,42 @@ func (s *testSuite5) TestValidateSetVar(c *C) {
result = tk.MustQuery("select @@tmp_table_size;")
result.Check(testkit.Rows("167772161"))

tk.MustExec("set @@tmp_table_size=9223372036854775807")
tk.MustExec("set @@tmp_table_size=18446744073709551615")
result = tk.MustQuery("select @@tmp_table_size;")
result.Check(testkit.Rows("9223372036854775807"))
result.Check(testkit.Rows("18446744073709551615"))

_, err = tk.Exec("set @@tmp_table_size=18446744073709551616")
c.Assert(terror.ErrorEqual(err, variable.ErrWrongTypeForVar), IsTrue)

_, err = tk.Exec("set @@tmp_table_size='hello'")
c.Assert(terror.ErrorEqual(err, variable.ErrWrongTypeForVar), IsTrue)

tk.MustExec("set @@tidb_tmp_table_max_size=-1")
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect tidb_tmp_table_max_size value: '-1'"))
result = tk.MustQuery("select @@tidb_tmp_table_max_size;")
result.Check(testkit.Rows("1048576"))

tk.MustExec("set @@tidb_tmp_table_max_size=1048575")
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect tidb_tmp_table_max_size value: '1048575'"))
result = tk.MustQuery("select @@tidb_tmp_table_max_size;")
result.Check(testkit.Rows("1048576"))

tk.MustExec("set @@tidb_tmp_table_max_size=167772161")
result = tk.MustQuery("select @@tidb_tmp_table_max_size;")
result.Check(testkit.Rows("167772161"))

tk.MustExec("set @@tidb_tmp_table_max_size=137438953472")
result = tk.MustQuery("select @@tidb_tmp_table_max_size;")
result.Check(testkit.Rows("137438953472"))

tk.MustExec("set @@tidb_tmp_table_max_size=137438953473")
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect tidb_tmp_table_max_size value: '137438953473'"))
result = tk.MustQuery("select @@tidb_tmp_table_max_size;")
result.Check(testkit.Rows("137438953472"))

_, err = tk.Exec("set @@tidb_tmp_table_max_size='hello'")
c.Assert(terror.ErrorEqual(err, variable.ErrWrongTypeForVar), IsTrue)

tk.MustExec("set @@global.connect_timeout=1")
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|Truncated incorrect connect_timeout value: '1'"))
result = tk.MustQuery("select @@global.connect_timeout;")
Expand Down
51 changes: 28 additions & 23 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3802,6 +3802,11 @@ func (s *testSessionSuite3) TestSetVarHint(c *C) {
c.Assert(tk.Se.GetSessionVars().StmtCtx.GetWarnings(), HasLen, 0)
tk.MustQuery("SELECT @@max_heap_table_size;").Check(testkit.Rows("16777216"))

tk.Se.GetSessionVars().SetSystemVar("tmp_table_size", "16777216")
tk.MustQuery("SELECT /*+ SET_VAR(tmp_table_size=16384) */ @@tmp_table_size;").Check(testkit.Rows("16384"))
c.Assert(tk.Se.GetSessionVars().StmtCtx.GetWarnings(), HasLen, 0)
tk.MustQuery("SELECT @@tmp_table_size;").Check(testkit.Rows("16777216"))

tk.Se.GetSessionVars().SetSystemVar("div_precision_increment", "4")
tk.MustQuery("SELECT /*+ SET_VAR(div_precision_increment=0) */ @@div_precision_increment;").Check(testkit.Rows("0"))
c.Assert(tk.Se.GetSessionVars().StmtCtx.GetWarnings(), HasLen, 0)
Expand Down Expand Up @@ -4739,47 +4744,47 @@ func (s *testSessionSuite) TestInTxnPSProtoPointGet(c *C) {
}

func (s *testSessionSuite) TestTMPTableSize(c *C) {
// Test the @@tmp_table_size system variable.
// Test the @@tidb_tmp_table_max_size system variable.
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create global temporary table t (c1 int, c2 varchar(512)) on commit delete rows")
tk.MustExec("create temporary table tl (c1 int, c2 varchar(512))")
tk.MustExec("create global temporary table t (c1 int, c2 mediumtext) on commit delete rows")
tk.MustExec("create temporary table tl (c1 int, c2 mediumtext)")

tk.MustQuery("select @@global.tmp_table_size").Check(testkit.Rows(strconv.Itoa(variable.DefTMPTableSize)))
c.Assert(tk.Se.GetSessionVars().TMPTableSize, Equals, int64(variable.DefTMPTableSize))
tk.MustQuery("select @@global.tidb_tmp_table_max_size").Check(testkit.Rows(strconv.Itoa(variable.DefTiDBTmpTableMaxSize)))
c.Assert(tk.Se.GetSessionVars().TMPTableSize, Equals, int64(variable.DefTiDBTmpTableMaxSize))

// Min value 1024, so the result is change to 1024, with a warning.
tk.MustExec("set @@global.tmp_table_size = 123")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1292 Truncated incorrect tmp_table_size value: '123'"))
// Min value 1M, so the result is change to 1M, with a warning.
tk.MustExec("set @@global.tidb_tmp_table_max_size = 123")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1292 Truncated incorrect tidb_tmp_table_max_size value: '123'"))

// Change the session scope value.
tk.MustExec("set @@session.tmp_table_size = 2097152")
// Change the session scope value to 2M.
tk.MustExec("set @@session.tidb_tmp_table_max_size = 2097152")
c.Assert(tk.Se.GetSessionVars().TMPTableSize, Equals, int64(2097152))

// Check in another sessin, change session scope value does not affect the global scope.
// Check in another session, change session scope value does not affect the global scope.
tk1 := testkit.NewTestKit(c, s.store)
tk1.MustQuery("select @@global.tmp_table_size").Check(testkit.Rows("1024"))
tk1.MustQuery("select @@global.tidb_tmp_table_max_size").Check(testkit.Rows(strconv.Itoa(1 << 20)))

// The value is now 1024, check the error when table size exceed it.
tk.MustExec("set @@session.tmp_table_size = 1024")
// The value is now 1M, check the error when table size exceed it.
tk.MustExec(fmt.Sprintf("set @@session.tidb_tmp_table_max_size = %d", 1<<20))
tk.MustExec("begin")
tk.MustExec("insert into t values (1, repeat('x', 512))")
tk.MustExec("insert into t values (1, repeat('x', 512))")
tk.MustGetErrCode("insert into t values (1, repeat('x', 512))", errno.ErrRecordFileFull)
tk.MustExec("insert into t values (1, repeat('x', 512*1024))")
tk.MustExec("insert into t values (1, repeat('x', 512*1024))")
tk.MustGetErrCode("insert into t values (1, repeat('x', 512*1024))", errno.ErrRecordFileFull)
tk.MustExec("rollback")

// Check local temporary table
tk.MustExec("begin")
tk.MustExec("insert into tl values (1, repeat('x', 512))")
tk.MustExec("insert into tl values (1, repeat('x', 512))")
tk.MustGetErrCode("insert into tl values (1, repeat('x', 512))", errno.ErrRecordFileFull)
tk.MustExec("insert into tl values (1, repeat('x', 512*1024))")
tk.MustExec("insert into tl values (1, repeat('x', 512*1024))")
tk.MustGetErrCode("insert into tl values (1, repeat('x', 512*1024))", errno.ErrRecordFileFull)
tk.MustExec("rollback")

// Check local temporary table with some data in session
tk.MustExec("insert into tl values (1, repeat('x', 512))")
tk.MustExec("insert into tl values (1, repeat('x', 512*1024))")
tk.MustExec("begin")
tk.MustExec("insert into tl values (1, repeat('x', 512))")
tk.MustGetErrCode("insert into tl values (1, repeat('x', 512))", errno.ErrRecordFileFull)
tk.MustExec("insert into tl values (1, repeat('x', 512*1024))")
tk.MustGetErrCode("insert into tl values (1, repeat('x', 512*1024))", errno.ErrRecordFileFull)
tk.MustExec("rollback")
}

Expand Down
1 change: 1 addition & 0 deletions sessionctx/variable/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ var noopSysVars = []*SysVar{
{Scope: ScopeGlobal, Name: "innodb_max_dirty_pages_pct_lwm", Value: "0"},
{Scope: ScopeGlobal, Name: LogQueriesNotUsingIndexes, Value: Off, Type: TypeBool},
{Scope: ScopeGlobal | ScopeSession, Name: "max_heap_table_size", Value: "16777216", IsHintUpdatable: true},
{Scope: ScopeGlobal | ScopeSession, Name: "tmp_table_size", Value: "16777216", Type: TypeUnsigned, MinValue: 1024, MaxValue: math.MaxUint64, IsHintUpdatable: true},
{Scope: ScopeGlobal | ScopeSession, Name: "div_precision_increment", Value: "4", IsHintUpdatable: true},
{Scope: ScopeGlobal, Name: "innodb_lru_scan_depth", Value: "1024"},
{Scope: ScopeGlobal, Name: "innodb_purge_rseg_truncate_frequency", Value: ""},
Expand Down
7 changes: 2 additions & 5 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -925,10 +925,7 @@ type SessionVars struct {
// see https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_cte_max_recursion_depth
CTEMaxRecursionDepth int

// The temporary table size threshold
// In MySQL, when a temporary table exceed this size, it spills to disk.
// In TiDB, as we do not support spill to disk for now, an error is reported.
// See https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_tmp_table_size
// The temporary table size threshold, which is different from MySQL. See https://github.com/pingcap/tidb/issues/28691.
TMPTableSize int64

// EnableStableResultMode if stabilize query results.
Expand Down Expand Up @@ -1192,7 +1189,7 @@ func NewSessionVars() *SessionVars {
EnableIndexMergeJoin: DefTiDBEnableIndexMergeJoin,
AllowFallbackToTiKV: make(map[kv.StoreType]struct{}),
CTEMaxRecursionDepth: DefCTEMaxRecursionDepth,
TMPTableSize: DefTMPTableSize,
TMPTableSize: DefTiDBTmpTableMaxSize,
MPPStoreLastFailTime: make(map[string]time.Time),
MPPStoreFailTTL: DefTiDBMPPStoreFailTTL,
EnablePlacementChecks: DefEnablePlacementCheck,
Expand Down
8 changes: 3 additions & 5 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -1762,9 +1762,9 @@ var defaultSysVars = []*SysVar{
}, SetGlobal: func(s *SessionVars, val string) error {
return setTiDBTableValue(s, "tikv_gc_scan_lock_mode", val, "Mode of scanning locks, \"physical\" or \"legacy\"")
}},
// See https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_tmp_table_size
{Scope: ScopeGlobal | ScopeSession, Name: TMPTableSize, Value: strconv.Itoa(DefTMPTableSize), Type: TypeUnsigned, MinValue: 1024, MaxValue: math.MaxInt64, IsHintUpdatable: true, AllowEmpty: true, SetSession: func(s *SessionVars, val string) error {
s.TMPTableSize = tidbOptInt64(val, DefTMPTableSize)
// It's different from tmp_table_size or max_heap_table_size. See https://github.com/pingcap/tidb/issues/28691.
{Scope: ScopeGlobal | ScopeSession, Name: TiDBTmpTableMaxSize, Value: strconv.Itoa(DefTiDBTmpTableMaxSize), Type: TypeUnsigned, MinValue: 1 << 20, MaxValue: 1 << 37, SetSession: func(s *SessionVars, val string) error {
s.TMPTableSize = tidbOptInt64(val, DefTiDBTmpTableMaxSize)
return nil
}},
// variable for top SQL feature.
Expand Down Expand Up @@ -1943,8 +1943,6 @@ const (
MaxConnectErrors = "max_connect_errors"
// TableDefinitionCache is the name for 'table_definition_cache' system variable.
TableDefinitionCache = "table_definition_cache"
// TMPTableSize is the name for 'tmp_table_size' system variable.
TMPTableSize = "tmp_table_size"
// Timestamp is the name for 'timestamp' system variable.
Timestamp = "timestamp"
// ConnectTimeout is the name for 'connect_timeout' system variable.
Expand Down
5 changes: 4 additions & 1 deletion sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,9 @@ const (
// TiDBEnablePseudoForOutdatedStats indicates whether use pseudo for outdated stats
TiDBEnablePseudoForOutdatedStats = "tidb_enable_pseudo_for_outdated_stats"

// TiDBTmpTableMaxSize indicates the max memory size of temporary tables.
TiDBTmpTableMaxSize = "tidb_tmp_table_max_size"

// TiDBEnableMPPBalanceWithContinuousRegion indicates whether MPP balance logic will take account of region's continuity in TiFlash.
TiDBEnableMPPBalanceWithContinuousRegion = "tidb_enable_mpp_balance_with_continuous_region"
// TiDBEnableMPPBalanceWithContinuousRegionCount indicates the continuous region count that balance logic assigns to a TiFlash instance each time.
Expand Down Expand Up @@ -760,7 +763,7 @@ const (
DefTiDBTopSQLMaxStatementCount = 200
DefTiDBTopSQLMaxCollect = 10000
DefTiDBTopSQLReportIntervalSeconds = 60
DefTMPTableSize = 16777216
DefTiDBTmpTableMaxSize = 64 << 20 // 64MB.
DefTiDBEnableLocalTxn = false
DefTiDBEnableOrderedResultMode = false
DefTiDBEnablePseudoForOutdatedStats = true
Expand Down
2 changes: 1 addition & 1 deletion sessionctx/variable/varsutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestNewSessionVars(t *testing.T) {
require.Equal(t, int64(DefTiDBShardAllocateStep), vars.ShardAllocateStep)
require.Equal(t, DefTiDBAnalyzeVersion, vars.AnalyzeVersion)
require.Equal(t, DefCTEMaxRecursionDepth, vars.CTEMaxRecursionDepth)
require.Equal(t, int64(DefTMPTableSize), vars.TMPTableSize)
require.Equal(t, int64(DefTiDBTmpTableMaxSize), vars.TMPTableSize)

assertFieldsGreaterThanZero(t, reflect.ValueOf(vars.MemQuota))
assertFieldsGreaterThanZero(t, reflect.ValueOf(vars.BatchSize))
Expand Down

0 comments on commit ce8e734

Please sign in to comment.