Repository navigation
to 3.0: fix(dedupjoin): return ErrTxnNeedRetry in checkSnapshotAdvancedDuplicates to fix false-positive duplicate entry (#23943) - #23952
Merged
4 commits merged intoMar 25, 2026
Conversation
…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]>
There was a problem hiding this comment.
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, returnErrTxnNeedRetry(instead ofErrDuplicateEntry) when the range check reports a potential duplicate. - Advance
proc.StmtSnapshotTSto 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.
…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]>
Closed
1 task done
XuPeng-SH
approved these changes
Mar 25, 2026
This pull request was closed.
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What type of PR is this?
Which issue(s) this PR fixes:
issue #23943
What this PR does / why we need it:
checkSnapshotAdvancedDuplicatesis called after the Lock operator advances the statement snapshot from T₀ to T₁. It usesPrimaryKeysMayBeUpsertedto check whether any of the keys being inserted were modified in [T₀, T₁] by a concurrent transaction.Root cause of the false positive:
PrimaryKeysMayBeUpsertedis probabilistic — it uses bloom filters and internally callsGetChangedObjsBetween(from.Next(), MaxTs()), which ignores thetoargument 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 spuriousDuplicate entryerrors to clients.Fix:
Return
ErrTxnNeedRetryinstead ofErrDuplicateEntry.ErrTxnNeedRetryis caught by the statement retry loop incompile2.go, which rolls back the workspace and re-executes at the latest snapshot. On retry, the probe side re-scans accurately and returns a realErrDuplicateEntryonly if a true duplicate exists.Call
proc.SetStmtSnapshotTS(currentTS)before returning the error.prepareRetrypreservesStmtSnapshotTSand carries it into the next attempt asInitialSnapshotTS. 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
TestCheckSnapshotAdvancedDuplicates_ReturnsDuplicate: assertsErrTxnNeedRetryand thatStmtSnapshotTSis advanced tocurrentTS.TestCheckSnapshotAdvancedDuplicates_FalsePositive: regression test for the sysbench false-positive scenario — build keys [10, 20], concurrent txn modifies key 10, bloom filter fires — assertsErrTxnNeedRetry(notErrDuplicateEntry) andStmtSnapshotTSadvancement.Does this PR introduce a user-facing change?