diff --git a/infoschema/tables.go b/infoschema/tables.go index c1257ab1bea8f..bb93446ee2342 100644 --- a/infoschema/tables.go +++ b/infoschema/tables.go @@ -651,6 +651,29 @@ func getRowCountAllTable(ctx sessionctx.Context) (map[int64]uint64, error) { return rowCountMap, nil } +func getAutoIncrementID(ctx sessionctx.Context, schema *model.DBInfo, tblInfo *model.TableInfo) (int64, error) { + hasAutoIncID := false + for _, col := range tblInfo.Cols() { + if mysql.HasAutoIncrementFlag(col.Flag) { + hasAutoIncID = true + break + } + } + autoIncID := tblInfo.AutoIncID + if hasAutoIncID { + is := ctx.GetSessionVars().TxnCtx.InfoSchema.(InfoSchema) + tbl, err := is.TableByName(schema.Name, tblInfo.Name) + if err != nil { + return 0, errors.Trace(err) + } + autoIncID, err = tbl.Allocator(ctx).NextGlobalAutoID(tblInfo.ID) + if err != nil { + return 0, errors.Trace(err) + } + } + return autoIncID, nil +} + func dataForTables(ctx sessionctx.Context, schemas []*model.DBInfo) ([][]types.Datum, error) { tableRowsMap, err := getRowCountAllTable(ctx) if err != nil { @@ -676,6 +699,10 @@ func dataForTables(ctx sessionctx.Context, schemas []*model.DBInfo) ([][]types.D continue } + autoIncID, err := getAutoIncrementID(ctx, schema, table) + if err != nil { + return nil, errors.Trace(err) + } record := types.MakeDatums( catalogVal, // TABLE_CATALOG schema.Name.O, // TABLE_SCHEMA @@ -690,7 +717,7 @@ func dataForTables(ctx sessionctx.Context, schemas []*model.DBInfo) ([][]types.D uint64(0), // MAX_DATA_LENGTH uint64(0), // INDEX_LENGTH uint64(0), // DATA_FREE - table.AutoIncID, // AUTO_INCREMENT + autoIncID, // AUTO_INCREMENT createTime, // CREATE_TIME nil, // UPDATE_TIME nil, // CHECK_TIME diff --git a/infoschema/tables_test.go b/infoschema/tables_test.go index 40dc72a095bc6..90dd34a0292a7 100644 --- a/infoschema/tables_test.go +++ b/infoschema/tables_test.go @@ -65,6 +65,15 @@ func (s *testSuite) TestDataForTableRowsCountField(c *C) { tk.MustQuery("select table_rows from information_schema.tables where table_name='t'").Check( testkit.Rows("2")) + // Test for auto increment ID. + tk.MustExec("drop table t") + tk.MustExec("create table t (c int auto_increment primary key, d int)") + tk.MustQuery("select auto_increment from information_schema.tables where table_name='t'").Check( + testkit.Rows("1")) + tk.MustExec("insert into t(c, d) values(1, 1)") + tk.MustQuery("select auto_increment from information_schema.tables where table_name='t'").Check( + testkit.Rows("30002")) + tk.MustExec("create user xxx") tk.MustExec("flush privileges")