Skip to content

to 3.0: fix(dedupjoin): return ErrTxnNeedRetry in checkSnapshotAdvancedDuplicates to fix false-positive duplicate entry (#23943) - #23952

Merged
4 commits merged into
matrixorigin:3.0-devfrom
gouhongshen:fix/dedupjoin-false-positive-23943
Mar 25, 2026
Merged

4 commits merged into
matrixorigin:3.0-devfrom
gouhongshen:fix/dedupjoin-false-positive-23943

Conversation

@gouhongshen

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #23943

What this PR does / why we need it:

checkSnapshotAdvancedDuplicates is called after the Lock operator advances the statement snapshot from T₀ to T₁. It uses PrimaryKeysMayBeUpserted to check whether any of the keys being inserted were modified in [T₀, T₁] by a concurrent transaction.

Root cause of the false positive: PrimaryKeysMayBeUpserted is probabilistic — it uses bloom filters and internally calls GetChangedObjsBetween(from.Next(), MaxTs()), which ignores the to argument and effectively scans [T₀, +∞). In sysbench read-write workloads, concurrent DELETE+INSERT on unrelated keys caused the check to return true even when no real duplicate existed, surfacing spurious Duplicate entry errors to clients.

Fix:

  1. Return ErrTxnNeedRetry instead of ErrDuplicateEntry. ErrTxnNeedRetry is caught by the statement retry loop in compile2.go, which rolls back the workspace and re-executes at the latest snapshot. On retry, the probe side re-scans accurately and returns a real ErrDuplicateEntry only if a true duplicate exists.

  2. Call proc.SetStmtSnapshotTS(currentTS) before returning the error. prepareRetry preserves StmtSnapshotTS and carries it into the next attempt as InitialSnapshotTS. Without advancing it to T₁, every retry would re-check the same [T₀, +∞) window, potentially livelocking indefinitely. After the advance, if Lock does not push the snapshot further on retry, T₁ < T₁ is false and the fallback check is skipped entirely.

Test coverage

  • Updated TestCheckSnapshotAdvancedDuplicates_ReturnsDuplicate: asserts ErrTxnNeedRetry and that StmtSnapshotTS is advanced to currentTS.
  • Added TestCheckSnapshotAdvancedDuplicates_FalsePositive: regression test for the sysbench false-positive scenario — build keys [10, 20], concurrent txn modifies key 10, bloom filter fires — asserts ErrTxnNeedRetry (not ErrDuplicateEntry) and StmtSnapshotTS advancement.

Does this PR introduce a user-facing change?

fix: remove spurious Duplicate entry errors during concurrent snapshot-restore INSERT operations (issue #23943)

gouhongshen and others added 2 commits March 25, 2026 11:31
…n checkSnapshotAdvancedDuplicates

PrimaryKeysMayBeUpserted is a probabilistic check (bloom filter /
range overlap) that can produce false positives.  Returning
ErrDuplicateEntry directly causes spurious client-visible errors in
workloads like sysbench read-write (issue matrixorigin#23943).

Change the error to ErrTxnNeedRetry so the statement is transparently
retried.  On retry the probe side re-scans at the advanced snapshot
and accurately detects real duplicate entries if they exist.

Also add a regression test ported from repro.go that asserts the
false-positive scenario now returns ErrTxnNeedRetry instead of
ErrDuplicateEntry.

Co-authored-by: Copilot <[email protected]>
…ent livelock

prepareRetry preserves the existing StmtSnapshotTS and carries it into
the retried statement as InitialSnapshotTS.  Without this change, every
retry re-checks the same [T₀, +∞) window (PrimaryKeysMayBeUpserted
ignores its 'to' argument) and a false positive that triggered the first
retry can trigger every subsequent retry, creating an unbounded loop.

Fix: call proc.SetStmtSnapshotTS(currentTS) before returning
ErrTxnNeedRetry.  On retry, InitialSnapshotTS starts at T₁.  If Lock
does not push the snapshot further, T₁ < T₁ is false and the fallback
check is skipped entirely, converging in one retry.  If there is a
genuine further advancement to T₂, only the new [T₁, T₂] window is
re-evaluated.

Update both regression tests to assert that StmtSnapshotTS is advanced
to currentTS after the retryable error is returned.

Co-authored-by: Copilot <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes false-positive Duplicate entry errors emitted by dedupjoin when statement snapshots advance and the probabilistic PrimaryKeysMayBeUpserted check reports potential conflicts outside the intended time window, by switching the fallback path to a retryable error and ensuring the next retry uses the advanced snapshot.

Changes:

  • In checkSnapshotAdvancedDuplicates, return ErrTxnNeedRetry (instead of ErrDuplicateEntry) when the range check reports a potential duplicate.
  • Advance proc.StmtSnapshotTS to the current snapshot before returning, so statement retries don’t re-check the same snapshot window.
  • Update/add unit tests to assert the retryable error and snapshot advancement, including a regression test for the reported false-positive scenario.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
pkg/sql/colexec/dedupjoin/join.go Switch snapshot-advanced duplicate fallback from DuplicateEntry to TxnNeedRetry and advance StmtSnapshotTS to prevent retry livelock.
pkg/sql/colexec/dedupjoin/join_test.go Update expectations for retry behavior and add a regression test asserting StmtSnapshotTS advancement.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/sql/colexec/dedupjoin/join_test.go Resolved
Comment thread pkg/sql/colexec/dedupjoin/join_test.go Resolved
…tamps

- Rename TestCheckSnapshotAdvancedDuplicates_ReturnsDuplicate to
  TestCheckSnapshotAdvancedDuplicates_TriggersRetry to reflect that
  the function now returns ErrTxnNeedRetry.
- Replace hardcoded types.BuildTS(1, 0) / types.BuildTS(2, 0) with
  fromTS/toTS derived from initialTS and currentTS variables.

Co-authored-by: Copilot <[email protected]>
@gouhongshen gouhongshen changed the title fix(dedupjoin): return ErrTxnNeedRetry in checkSnapshotAdvancedDuplicates to fix false-positive duplicate entry (#23943) to 3.0: fix(dedupjoin): return ErrTxnNeedRetry in checkSnapshotAdvancedDuplicates to fix false-positive duplicate entry (#23943) Mar 25, 2026
@ghost
ghost merged commit 5e9f1d9 into matrixorigin:3.0-dev Mar 25, 2026
41 of 42 checks passed
This pull request was closed.

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working size/S Denotes a PR that changes [10,99] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants