@@ -115,10 +115,29 @@ func (e *memtableRetriever) retrieve(ctx context.Context, sctx sessionctx.Contex
115
115
116
116
// Cache the ret full rows in schemataRetriever
117
117
if ! e .initialized {
118
- is := sctx .GetInfoSchema ().(infoschema.InfoSchema )
119
- e .is = is
120
-
121
118
var err error
119
+ // InTxn() should be true in most of the cases.
120
+ // Because the transaction should have been activated in MemTableReaderExec Open().
121
+ // Why not just activate the txn here (sctx.Txn(true)) and do it in Open() instead?
122
+ // Because it could DATA RACE here and in Open() it's safe.
123
+ if sctx .GetSessionVars ().InTxn () {
124
+ e .is , err = domain .GetDomain (sctx ).GetSnapshotInfoSchema (sctx .GetSessionVars ().TxnCtx .StartTS )
125
+ if err != nil {
126
+ return nil , errors .Trace (err )
127
+ }
128
+ } else {
129
+ // When the excutor is built from tidb coprocessor request, the transaction is not valid.
130
+ // Then InTxn() is false.
131
+ //
132
+ // What's the difference between using latest infoschema and using snapshot infoschema?
133
+ // A query *should* use the infoschema of the txn start ts, but it's still safe to use the latest.
134
+ // If now it's 12:00:00, the ts of the latest infoschema might be 11:59:30 or 11:52:12 or anything.
135
+ // Say, default GC interval is 10min, the ts of the latest infoschema is 11:52:12.
136
+ // Then the valid lifetime range on infoschema API become [11:52:12, 12:12:12) using latest infoschema,
137
+ // but it should be [12:00:00, 12:10:00) if using the snapshot infoschema.
138
+ e .is = sctx .GetInfoSchema ().(infoschema.InfoSchema )
139
+ }
140
+
122
141
switch e .table .Name .O {
123
142
case infoschema .TableSchemata :
124
143
err = e .setDataFromSchemata (sctx )
@@ -177,7 +196,7 @@ func (e *memtableRetriever) retrieve(ctx context.Context, sctx sessionctx.Contex
177
196
infoschema .TableClientErrorsSummaryByHost :
178
197
err = e .setDataForClientErrorsSummary (sctx , e .table .Name .O )
179
198
case infoschema .TableAttributes :
180
- err = e .setDataForAttributes (ctx , sctx , is )
199
+ err = e .setDataForAttributes (ctx , sctx , e . is )
181
200
case infoschema .TablePlacementPolicies :
182
201
err = e .setDataFromPlacementPolicies (sctx )
183
202
case infoschema .TableTrxSummary :
0 commit comments