Skip to content

Commit 6d76d83

Browse files
craig[bot]rytaft
andcommitted
Merge #144635
144635: sql: disable buffered writes if ReturnRawMVCCValues needed r=rytaft a=rytaft Buffered writes don't yet support the `ReturnRawMVCCValues` option of Gets, Scans, and ReverseScans, which is needed to produce certain system columns. Therefore, this commit disables buffered writes if those system columns are requested when scanning data. Fixes #144273 Release note: None Co-authored-by: Rebecca Taft <[email protected]>
2 parents e35bddb + 31ab5c3 commit 6d76d83

File tree

6 files changed

+91
-0
lines changed

6 files changed

+91
-0
lines changed

pkg/sql/colfetcher/cfetcher.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/cockroachdb/apd/v3"
1919
"github.com/cockroachdb/cockroach/pkg/col/coldata"
2020
"github.com/cockroachdb/cockroach/pkg/keys"
21+
"github.com/cockroachdb/cockroach/pkg/kv"
2122
"github.com/cockroachdb/cockroach/pkg/roachpb"
2223
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catenumpb"
2324
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb"
@@ -202,6 +203,8 @@ type cFetcherArgs struct {
202203
// the last one returned on the NextBatch calls if the caller wishes to keep
203204
// multiple batches at the same time.
204205
alwaysReallocate bool
206+
// Txn is the txn for the fetch. It might be nil.
207+
txn *kv.Txn
205208
}
206209

207210
// noOutputColumn is a sentinel value to denote that a system column is not
@@ -440,6 +443,14 @@ func (cf *cFetcher) Init(
440443
}
441444
}
442445

446+
// Disable buffered writes if any system columns are needed that require
447+
// MVCC decoding.
448+
if cf.mvccDecodeStrategy == storage.MVCCDecodingRequired {
449+
if cf.txn != nil && cf.txn.BufferedWritesEnabled() {
450+
cf.txn.SetBufferedWritesEnabled(false /* enabled */)
451+
}
452+
}
453+
443454
fullColumns := table.spec.KeyFullColumns()
444455

445456
nIndexCols := len(fullColumns)

pkg/sql/colfetcher/cfetcher_wrapper.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ func newCFetcherWrapper(
238238
true, /* singleUse */
239239
collectStats,
240240
alwaysReallocate,
241+
nil, /* txn */
241242
}
242243

243244
// This memory monitor is not connected to the memory accounting system

pkg/sql/colfetcher/colbatch_scan.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ func NewColBatchScan(
347347
true, /* singleUse */
348348
shouldCollectStats,
349349
false, /* alwaysReallocate */
350+
flowCtx.Txn,
350351
}
351352
if err = fetcher.Init(fetcherAllocator, kvFetcher, tableArgs); err != nil {
352353
fetcher.Release()

pkg/sql/colfetcher/index_join.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,7 @@ func NewColIndexJoin(
602602
false, /* singleUse */
603603
shouldCollectStats,
604604
false, /* alwaysReallocate */
605+
txn,
605606
}
606607
if err = fetcher.Init(
607608
fetcherAllocator, kvFetcher, tableArgs,

pkg/sql/logictest/testdata/logic_test/buffered_writes

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,70 @@ SELECT * FROM t4
252252
2 200
253253
3 300
254254

255+
subtest regression
256+
255257
# Regression test for #144274.
256258
statement ok
257259
EXPLAIN CREATE DATABASE foo
258260

259261
statement ok
260262
EXPLAIN ANALYZE CREATE DATABASE foo
263+
264+
# Regression test for #144273.
265+
statement ok
266+
CREATE TABLE t144273 (
267+
k INT PRIMARY KEY,
268+
a INT,
269+
b INT,
270+
INDEX (a),
271+
INDEX (b),
272+
FAMILY (k, a, b)
273+
)
274+
275+
statement ok
276+
PREPARE p AS UPDATE t144273 t1 SET a = t2.a + 1, b = t2.b + 1 FROM t144273 t2 WHERE t1.k = t2.a AND t1.k = $1
277+
278+
statement ok
279+
SET vectorize = on
280+
281+
statement ok
282+
INSERT INTO t144273 VALUES (1, 1, 1);
283+
284+
# The table IDs in the kv trace below are different with the legacy schema
285+
# changer, so disable that configuration.
286+
skipif config local-legacy-schema-changer
287+
query T kvtrace
288+
EXECUTE p(1)
289+
----
290+
Scan /Table/114/2/{1-2}
291+
Scan /Table/114/1/1/0
292+
Scan /Table/114/1/1/0
293+
Put (locking) /Table/114/1/1/0 -> /TUPLE/2:2:Int/2/1:3:Int/2
294+
Del /Table/114/2/1/1/0
295+
Put /Table/114/2/2/1/0 -> /BYTES/
296+
Del /Table/114/3/1/1/0
297+
Put /Table/114/3/2/1/0 -> /BYTES/
298+
299+
statement ok
300+
SET vectorize = off
301+
302+
statement ok
303+
INSERT INTO t144273 VALUES (2, 2, 2);
304+
305+
# The table IDs in the kv trace below are different with the legacy schema
306+
# changer, so disable that configuration.
307+
skipif config local-legacy-schema-changer
308+
query T kvtrace
309+
EXECUTE p(2)
310+
----
311+
Scan /Table/114/2/{2-3}
312+
Scan /Table/114/1/1/0, /Table/114/1/2/0
313+
Scan /Table/114/1/2/0
314+
Put (locking) /Table/114/1/2/0 -> /TUPLE/2:2:Int/3/1:3:Int/3
315+
Del /Table/114/2/2/2/0
316+
Put /Table/114/2/3/2/0 -> /BYTES/
317+
Del /Table/114/3/2/2/0
318+
Put /Table/114/3/3/2/0 -> /BYTES/
319+
320+
statement ok
321+
RESET vectorize

pkg/sql/row/fetcher.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,14 @@ func (rf *Fetcher) Init(ctx context.Context, args FetcherInitArgs) error {
409409
}
410410
}
411411

412+
// Disable buffered writes if any system columns are needed that require
413+
// MVCC decoding.
414+
if rf.mvccDecodeStrategy == storage.MVCCDecodingRequired {
415+
if rf.args.Txn != nil && rf.args.Txn.BufferedWritesEnabled() {
416+
rf.args.Txn.SetBufferedWritesEnabled(false /* enabled */)
417+
}
418+
}
419+
412420
if len(args.Spec.FetchedColumns) > 0 {
413421
table.neededValueColsByIdx.AddRange(0, len(args.Spec.FetchedColumns)-1)
414422
}
@@ -528,6 +536,14 @@ func (rf *Fetcher) SetTxn(txn *kv.Txn) error {
528536
// setTxnAndSendFn peeks inside of the KVFetcher to update the underlying
529537
// txnKVFetcher with the new txn and sendFn.
530538
func (rf *Fetcher) setTxnAndSendFn(txn *kv.Txn, sendFn sendFunc) error {
539+
// Disable buffered writes if any system columns are needed that require
540+
// MVCC decoding.
541+
if rf.mvccDecodeStrategy == storage.MVCCDecodingRequired {
542+
if txn != nil && txn.BufferedWritesEnabled() {
543+
txn.SetBufferedWritesEnabled(false /* enabled */)
544+
}
545+
}
546+
531547
f, ok := rf.kvFetcher.KVBatchFetcher.(*txnKVFetcher)
532548
if !ok {
533549
return errors.AssertionFailedf(

0 commit comments

Comments
 (0)