@@ -26,6 +26,7 @@ import (
26
26
"github.com/pingcap/tidb/parser/format"
27
27
"github.com/pingcap/tidb/parser/model"
28
28
"github.com/pingcap/tidb/sessionctx"
29
+ "github.com/pingcap/tidb/sessiontxn"
29
30
"github.com/pingcap/tidb/types"
30
31
"github.com/pingcap/tidb/util/dbterror"
31
32
)
@@ -83,11 +84,15 @@ func onTTLInfoChange(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, err er
83
84
return ver , nil
84
85
}
85
86
86
- func checkTTLInfoValid (ctx sessionctx.Context , tblInfo * model.TableInfo ) error {
87
+ func checkTTLInfoValid (ctx sessionctx.Context , schema model. CIStr , tblInfo * model.TableInfo ) error {
87
88
if err := checkTTLIntervalExpr (ctx , tblInfo .TTLInfo ); err != nil {
88
89
return err
89
90
}
90
91
92
+ if err := checkTTLTableSuitable (ctx , schema , tblInfo ); err != nil {
93
+ return err
94
+ }
95
+
91
96
return checkTTLInfoColumnType (tblInfo )
92
97
}
93
98
@@ -119,6 +124,22 @@ func checkTTLInfoColumnType(tblInfo *model.TableInfo) error {
119
124
return nil
120
125
}
121
126
127
+ // checkTTLTableSuitable returns whether this table is suitable to be a TTL table
128
+ // A temporary table or a parent table referenced by a foreign key cannot be TTL table
129
+ func checkTTLTableSuitable (ctx sessionctx.Context , schema model.CIStr , tblInfo * model.TableInfo ) error {
130
+ if tblInfo .TempTableType != model .TempTableNone {
131
+ return dbterror .ErrTempTableNotAllowedWithTTL
132
+ }
133
+
134
+ // checks even when the foreign key check is not enabled, to keep safe
135
+ is := sessiontxn .GetTxnManager (ctx ).GetTxnInfoSchema ()
136
+ if referredFK := checkTableHasForeignKeyReferred (is , schema .L , tblInfo .Name .L , nil , true ); referredFK != nil {
137
+ return dbterror .ErrUnsupportedTTLReferencedByFK
138
+ }
139
+
140
+ return nil
141
+ }
142
+
122
143
func checkDropColumnWithTTLConfig (tblInfo * model.TableInfo , colName string ) error {
123
144
if tblInfo .TTLInfo != nil {
124
145
if tblInfo .TTLInfo .ColumnName .L == colName {
0 commit comments