Skip to content

Commit

Permalink
server: let select for update could be recorded for slow log and stat…
Browse files Browse the repository at this point in the history
…ements (#16743) (#16903)
  • Loading branch information
cfzjywxk authored Apr 28, 2020
1 parent a072269 commit 1490f30
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,7 @@ func (a *recordSet) NewChunk() *chunk.Chunk {

func (a *recordSet) Close() error {
err := a.executor.Close()
// `LowSlowQuery` and `SummaryStmt` must be called before recording `PrevStmt`.
a.stmt.LogSlowQuery(a.txnStartTS, a.lastErr == nil, false)
a.stmt.SummaryStmt()
sessVars := a.stmt.Ctx.GetSessionVars()
pps := types.CloneRow(sessVars.PreparedParams)
sessVars.PrevStmt = FormatSQL(a.stmt.OriginText(), pps)
a.stmt.logAudit()
a.stmt.CloseRecordSet(a.txnStartTS, a.lastErr)
return err
}

Expand Down Expand Up @@ -338,6 +332,7 @@ type chunkRowRecordSet struct {
idx int
fields []*ast.ResultField
e Executor
stmt *ExecStmt
}

func (c *chunkRowRecordSet) Fields() []*ast.ResultField {
Expand All @@ -358,6 +353,7 @@ func (c *chunkRowRecordSet) NewChunk() *chunk.Chunk {
}

func (c *chunkRowRecordSet) Close() error {
c.stmt.CloseRecordSet(c.stmt.Ctx.GetSessionVars().TxnCtx.StartTS, nil)
return nil
}

Expand Down Expand Up @@ -389,7 +385,7 @@ func (a *ExecStmt) runPessimisticSelectForUpdate(ctx context.Context, e Executor
}
if req.NumRows() == 0 {
fields := schema2ResultFields(e.Schema(), a.Ctx.GetSessionVars().CurrentDB)
return &chunkRowRecordSet{rows: rows, fields: fields, e: e}, nil
return &chunkRowRecordSet{rows: rows, fields: fields, e: e, stmt: a}, nil
}
iter := chunk.NewIterator4Chunk(req)
for r := iter.Begin(); r != iter.End(); r = iter.Next() {
Expand Down Expand Up @@ -686,6 +682,17 @@ func FormatSQL(sql string, pps variable.PreparedParams) stringutil.StringerFunc
}
}

// CloseRecordSet will finish the execution of current statement and do some record work
func (a *ExecStmt) CloseRecordSet(txnStartTS uint64, lastErr error) {
// `LowSlowQuery` and `SummaryStmt` must be called before recording `PrevStmt`.
a.LogSlowQuery(txnStartTS, lastErr == nil, false)
a.SummaryStmt()
sessVars := a.Ctx.GetSessionVars()
pps := types.CloneRow(sessVars.PreparedParams)
sessVars.PrevStmt = FormatSQL(a.OriginText(), pps)
a.logAudit()
}

// LogSlowQuery is used to print the slow query in the log files.
func (a *ExecStmt) LogSlowQuery(txnTS uint64, succ bool, hasMoreResults bool) {
sessVars := a.Ctx.GetSessionVars()
Expand Down

0 comments on commit 1490f30

Please sign in to comment.