From b8a2b9e5394b28268f4be537a7c9f8f7bdbe0ce0 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Thu, 30 May 2024 15:38:52 +0800 Subject: [PATCH] statstics,ddl: fix FK table forgets to send CreateTable event (#53654) (#53662) close pingcap/tidb#53652 --- pkg/ddl/table.go | 5 +++++ pkg/statistics/handle/ddl/ddl_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/pkg/ddl/table.go b/pkg/ddl/table.go index 63523481509dd..d602117acdb48 100644 --- a/pkg/ddl/table.go +++ b/pkg/ddl/table.go @@ -217,6 +217,11 @@ func createTableWithForeignKeys(d *ddlCtx, t *meta.Meta, job *model.Job, tbInfo return ver, errors.Trace(err) } job.FinishTableJob(model.JobStateDone, model.StatePublic, ver, tbInfo) + createTableEvent := statsutil.NewCreateTableEvent( + job.SchemaID, + tbInfo, + ) + asyncNotifyEvent(d, createTableEvent) return ver, nil default: return ver, errors.Trace(dbterror.ErrInvalidDDLJob.GenWithStackByArgs("table", tbInfo.State)) diff --git a/pkg/statistics/handle/ddl/ddl_test.go b/pkg/statistics/handle/ddl/ddl_test.go index 6bf394669f9c9..3e120a15f4575 100644 --- a/pkg/statistics/handle/ddl/ddl_test.go +++ b/pkg/statistics/handle/ddl/ddl_test.go @@ -86,6 +86,30 @@ func TestDDLTable(t *testing.T) { require.Nil(t, h.Update(is)) statsTbl = h.GetTableStats(tableInfo) require.False(t, statsTbl.Pseudo) + + // For FK table's CreateTable Event + // https://github.com/pingcap/tidb/issues/53652 + testKit.MustExec("create table t_parent (id int primary key)") + is = do.InfoSchema() + tbl, err = is.TableByName(model.NewCIStr("test"), model.NewCIStr("t_parent")) + require.NoError(t, err) + tableInfo = tbl.Meta() + err = h.HandleDDLEvent(<-h.DDLEventCh()) + require.NoError(t, err) + require.Nil(t, h.Update(is)) + statsTbl = h.GetTableStats(tableInfo) + require.False(t, statsTbl.Pseudo) + + testKit.MustExec("create table t_child (id int primary key, pid int, foreign key (pid) references t_parent(id) on delete cascade on update cascade);") + is = do.InfoSchema() + tbl, err = is.TableByName(model.NewCIStr("test"), model.NewCIStr("t_child")) + require.NoError(t, err) + tableInfo = tbl.Meta() + err = h.HandleDDLEvent(<-h.DDLEventCh()) + require.NoError(t, err) + require.Nil(t, h.Update(is)) + statsTbl = h.GetTableStats(tableInfo) + require.False(t, statsTbl.Pseudo) } func TestCreateASystemTable(t *testing.T) {