Skip to content

Commit c86c09f

Browse files
committed
backfill: fix a vector index backfill data race
The vector index backfill process uses the KV memory allocated by the backfill reader. This works fine for the first attempt to write a vector index row but, if the transaction writing the row fails and has to retry, there is a chance that the kv still has a reference to IndexEntry when the writer swings around to try again, causing a data race. This patch changes the vector index writer to allocate new memory for the IndexEntry on subsequent attempts to write the value after the first. Informs: #143107 Release note: None
1 parent fa5a923 commit c86c09f

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

pkg/sql/rowexec/indexbackfiller.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,24 @@ func (ib *indexBackfiller) maybeReencodeAndWriteVectorIndexEntry(
206206
tmpEntry.Value.RawBytes = append(tmpEntry.Value.RawBytes[:0], indexEntry.Value.RawBytes...)
207207
tmpEntry.Family = indexEntry.Family
208208

209+
firstAttempt := true
209210
err = ib.flowCtx.Cfg.DB.Txn(ctx, func(ctx context.Context, txn isql.Txn) error {
210-
entry, err := vih.ReEncodeVector(ctx, txn.KV(), *tmpEntry, indexEntry)
211+
// If the first attempt failed, we need to provide a new buffer to KV for
212+
// subsequent attempts because KV might still be using the previous buffer.
213+
var entryBuffer *rowenc.IndexEntry
214+
if firstAttempt {
215+
entryBuffer = indexEntry
216+
firstAttempt = false
217+
} else {
218+
entryBuffer = &rowenc.IndexEntry{
219+
Key: make(roachpb.Key, len(tmpEntry.Key)),
220+
Value: roachpb.Value{
221+
RawBytes: make([]byte, len(tmpEntry.Value.RawBytes)),
222+
},
223+
Family: tmpEntry.Family,
224+
}
225+
}
226+
entry, err := vih.ReEncodeVector(ctx, txn.KV(), *tmpEntry, entryBuffer)
211227
if err != nil {
212228
return err
213229
}

0 commit comments

Comments
 (0)