-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
infoschema: don't load table info to get auto_increment value #57296
Changes from 2 commits
46cb7bb
ef06307
fbe0fd2
671d89f
bc5e3d1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -242,11 +242,29 @@ func getAutoIncrementID( | |
sctx sessionctx.Context, | ||
tblInfo *model.TableInfo, | ||
) int64 { | ||
if raw, ok := is.(*infoschema.SessionExtendedInfoSchema); ok { | ||
v2, ok := raw.InfoSchema.(interface { | ||
TableIsCached(id int64) bool | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
}) | ||
if ok { | ||
isCached := v2.TableIsCached(tblInfo.ID) | ||
if !isCached { | ||
// Loading table info from kv storage invalidates the cached auto_increment id. | ||
return 0 | ||
} | ||
} | ||
} | ||
tbl, ok := is.TableByID(context.Background(), tblInfo.ID) | ||
if !ok { | ||
return 0 | ||
} | ||
return tbl.Allocators(sctx.GetTableCtx()).Get(autoid.AutoIncrementType).Base() + 1 | ||
alloc := tbl.Allocators(sctx.GetTableCtx()).Get(autoid.AutoIncrementType) | ||
if alloc == nil || alloc.Base() == 0 { | ||
// It may not be loaded yet. | ||
// To show global next autoID, one should use `show table x next_row_id`. | ||
return 0 | ||
} | ||
return alloc.Base() + 1 | ||
} | ||
|
||
func hasPriv(ctx sessionctx.Context, priv mysql.PrivilegeType) bool { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ import ( | |
"github.com/pingcap/tidb/pkg/domain/infosync" | ||
"github.com/pingcap/tidb/pkg/infoschema" | ||
infoschemacontext "github.com/pingcap/tidb/pkg/infoschema/context" | ||
"github.com/pingcap/tidb/pkg/meta/autoid" | ||
"github.com/pingcap/tidb/pkg/parser/auth" | ||
"github.com/pingcap/tidb/pkg/parser/model" | ||
"github.com/pingcap/tidb/pkg/sessionctx/variable" | ||
|
@@ -534,3 +535,26 @@ func TestSnapshotInfoschemaReader(t *testing.T) { | |
sql = fmt.Sprintf("select * from INFORMATION_SCHEMA.TABLES as of timestamp '%s' where table_schema = 'issue55827'", timeStr) | ||
tk.MustQuery(sql).Check(testkit.Rows()) | ||
} | ||
|
||
func TestInfoSchemaCachedAutoIncrement(t *testing.T) { | ||
store := testkit.CreateMockStore(t) | ||
tk := testkit.NewTestKit(t, store) | ||
tk.MustExec("use test") | ||
autoid.SetStep(1) | ||
tk.MustExec("set @@global.tidb_schema_cache_size = 0;") | ||
tk.MustExec("create table t (a int primary key auto_increment);") | ||
autoIncQuery := "select auto_increment from information_schema.tables where table_name = 't' and table_schema = 'test';" | ||
|
||
tk.MustQuery(autoIncQuery).Check(testkit.Rows("0")) | ||
tk.MustExec("insert into t values (),(),();") | ||
tk.MustQuery(autoIncQuery).Check(testkit.Rows("4")) | ||
|
||
tk.MustExec("set @@global.tidb_schema_cache_size = 1024 * 1024 * 1024;") | ||
tk.MustExec("create table t1 (a int);") // trigger infoschema cache reload | ||
tk.MustQuery(autoIncQuery).Check(testkit.Rows("0")) | ||
tk.MustExec("insert into t values ();") | ||
tk.MustQuery(autoIncQuery).Check(testkit.Rows("5")) | ||
tk.MustExec("set @@global.tidb_schema_cache_size = 0;") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is an API you can use to evict table cache directly, although trigger reload can get the same effect.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The schema is not v2 if we don't reload. |
||
tk.MustExec("drop table t1;") // trigger infoschema cache reload | ||
tk.MustQuery(autoIncQuery).Check(testkit.Rows("0")) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need this type assertion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Infoschema
that we can get from session is wrapped withSessionExtendedInfoSchema
:tidb/pkg/table/temptable/infoschema.go
Line 23 in a5832b4