Skip to content

Commit 93fae4e

Browse files
rafissclaude
andcommitted
sql/ttl: use AOST timestamp when computing query bounds
Previously, when computing query bounds from spans in the TTL job processor, we used the current timestamp for the KV Scan/ReverseScan operations. This could cause contention with foreground traffic. This change updates the TTL processor to use the same AOST (As Of System Time) timestamp for computing query bounds that is already used for the SELECT queries. This aligns the bounds computation with the historical time at which the TTL job operates, reducing load on the system and improving overall efficiency. Resolves: #155871 Epic: CRDB-55074 Release note: None 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 798c8dd commit 93fae4e

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

pkg/sql/ttl/ttljob/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ go_library(
4747
"//pkg/sql/types",
4848
"//pkg/util/admission/admissionpb",
4949
"//pkg/util/ctxgroup",
50-
"//pkg/util/hlc",
5150
"//pkg/util/log",
5251
"//pkg/util/metric",
5352
"//pkg/util/metric/aggmetric",

pkg/sql/ttl/ttljob/ttljob_processor.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import (
3434
"github.com/cockroachdb/cockroach/pkg/sql/types"
3535
"github.com/cockroachdb/cockroach/pkg/util/admission/admissionpb"
3636
"github.com/cockroachdb/cockroach/pkg/util/ctxgroup"
37-
"github.com/cockroachdb/cockroach/pkg/util/hlc"
3837
"github.com/cockroachdb/cockroach/pkg/util/log"
3938
"github.com/cockroachdb/cockroach/pkg/util/quotapool"
4039
"github.com/cockroachdb/cockroach/pkg/util/retry"
@@ -298,6 +297,10 @@ func (t *ttlProcessor) work(ctx context.Context, output execinfra.RowReceiver) e
298297
// Iterate over every span to feed work for the goroutine processors.
299298
kvDB := db.KV()
300299
var alloc tree.DatumAlloc
300+
// Compute the AOST timestamp for SpanToQueryBounds. This aligns the bounds
301+
// computation with the historical time used by the SELECT queries, reducing
302+
// contention with foreground traffic.
303+
aostTimestamp := kvDB.Clock().Now().AddDuration(ttlSpec.AOSTDuration)
301304
for i, span := range ttlSpec.Spans {
302305
if bounds, hasRows, err := spanutils.SpanToQueryBounds(
303306
ctx,
@@ -309,7 +312,7 @@ func (t *ttlProcessor) work(ctx context.Context, output execinfra.RowReceiver) e
309312
numFamilies,
310313
span,
311314
&alloc,
312-
hlc.Timestamp{}, // Use current time bounds; TTL only deletes live rows.
315+
aostTimestamp,
313316
); err != nil {
314317
return errors.Wrapf(err, "SpanToQueryBounds error index=%d span=%s", i, span)
315318
} else if hasRows {

0 commit comments

Comments
 (0)