Skip to content

Commit

Permalink
row: release memory more eagerly in txnKVFetcher
Browse files Browse the repository at this point in the history
Previously, we could leave some dangling references to batch responses
around in the txnKVFetcher when we were fetching more than one batch at
a time. This would cause a delay in reclamation of memory for the
lifetime of a given query.

Release note (bug fix): use less memory in some queries, primarily
lookup joins.
  • Loading branch information
jordanlewis committed May 29, 2021
1 parent 58c7449 commit 1f572c0
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/sql/row/kv_batch_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,26 +525,33 @@ func (f *txnKVFetcher) nextBatch(
) (ok bool, kvs []roachpb.KeyValue, batchResponse []byte, origSpan roachpb.Span, err error) {
if len(f.remainingBatches) > 0 {
batch := f.remainingBatches[0]
f.remainingBatches[0] = nil
f.remainingBatches = f.remainingBatches[1:]
return true, nil, batch, f.origSpan, nil
}
for len(f.responses) > 0 {
reply := f.responses[0].GetInner()
f.responses[0] = roachpb.ResponseUnion{}
f.responses = f.responses[1:]
origSpan := f.requestSpans[0]
f.requestSpans[0] = roachpb.Span{}
f.requestSpans = f.requestSpans[1:]
var batchResp []byte
switch t := reply.(type) {
case *roachpb.ScanResponse:
if len(t.BatchResponses) > 0 {
batchResp = t.BatchResponses[0]
f.remainingBatches = t.BatchResponses[1:]
t.BatchResponses[0] = nil
t.BatchResponses = nil
}
return true, t.Rows, batchResp, origSpan, nil
case *roachpb.ReverseScanResponse:
if len(t.BatchResponses) > 0 {
batchResp = t.BatchResponses[0]
f.remainingBatches = t.BatchResponses[1:]
t.BatchResponses[0] = nil
t.BatchResponses = nil
}
return true, t.Rows, batchResp, origSpan, nil
case *roachpb.GetResponse:
Expand All @@ -571,5 +578,7 @@ func (f *txnKVFetcher) nextBatch(

// close releases the resources of this txnKVFetcher.
func (f *txnKVFetcher) close(ctx context.Context) {
f.responses = nil
f.remainingBatches = nil
f.acc.Close(ctx)
}

0 comments on commit 1f572c0

Please sign in to comment.