Skip to content

fix(sql): preserve locking read retries across LIMIT - #30268

Merged
XuPeng-SH merged 6 commits into
matrixorigin:mainfrom
aptend:fix/30245-locking-read-limit-retry
Oct 15, 2026
Merged

XuPeng-SH merged 6 commits into
matrixorigin:mainfrom
aptend:fix/30245-locking-read-limit-retry

Conversation

@aptend

@aptend aptend commented Oct 15, 2026 •

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:

Fixes #30245

What this PR does / why we need it:

SELECT ... LIMIT 1 FOR UPDATE can return an obsolete row after waiting for a concurrent update. LockOp saved the required retry error until child EOF, but LIMIT could stop pulling input before that error was returned.

Once a retry is needed, LockOp now consumes the remaining input and acquires its locks without forwarding obsolete batches, then reports the retry error. This preserves the existing collect-all-locks retry policy and keeps uncontended execution streaming. Child errors and cancellation terminate draining; a later definition change retains its plan-rebuild error priority.

Validation:

  • The new LIMIT regression fails against the original production code. The complete LockOp package passes normally and with -race; the new operator test and six affected existing tests each pass 100 race repetitions.
  • TestIssue30245LockingReadRetriesBeforeLimit passes normally and with -race -count=3, covering 11 scenarios including prepared reuse, implicit session limits, FOR SHARE, cross-CN lock ownership, rollback, RR conflicts, and UPDATE after waiting.
  • locking_read_limit_retry.sql passes normal mo-tester comparison twice on the same fresh instance: 31/31 statements each run, with teardown verified before reuse.
  • Scoped go vet, golangci-lint v2.14.0, gofmt, and diff checks pass.
  • After syncing main's revert: restore fulltext planner before #30189 #30285 revert, the planner package passes with -short -covermode=set (15,404 named passes, 2 existing skips). Covered-fulltext regressions also pass with -race and with CI's matrixone_test coverage tag. The complete LockOp race suite and the 11-scenario SQL race regression pass again.

Drain the remaining lock input after a retry becomes necessary, acquiring its locks without forwarding obsolete batches. This keeps batched lock acquisition while preventing LIMIT from suppressing the pending retry error.

Add operator, SQL protocol, and distributed regressions for limited locking reads, prepared reuse, session row limits, cancellation, and remote lock owners.
@mergify mergify Bot added the kind/bug Something isn't working label Oct 15, 2026
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@XuPeng-SH

XuPeng-SH commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: 886048dc196d2553aa43ae4cf6e1465201570ef9

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

mo-bot review · mo-bot-local · review

Commit: 886048dc196d2553aa43ae4cf6e1465201570ef9

Assessment: approve

Reviewed the complete six-file delta and affected callers. The fix keeps retry ownership in LockOp, prevents LIMIT from hiding pending retries, and preserves uncontended streaming and cleanup contracts. No actionable defect remains.

Model: gpt-6-astra / xhigh

Session: 01a13dcf-1517-7f90-b3f9-c5f9f9e7031b · slot 0 · generation 1

Validation:

  • Complete LockOp normal and race suites each passed 196 named tests without failures or skips.

  • All seven affected operator tests passed 100 race repetitions each. The original production code failed the new LIMIT regression as expected.

  • The two-CN SQL regression passed all 11 scenarios normally, with race detection, and in four repeated race runs.

  • Reconciled 17 command/source/exit/log records; verified preflight, native provenance, formatting, diff checks and unchanged source. Evidence: /home/xupeng/.local/state/mo-bot/jobs/0cbc8a1bb3beeac39bd9e008/attempt-1/evidence-audit.json.

  • Frozen candidate CI reports successful UT, SCA, coverage, shared build and both BVT groups. Reviewed the added SQL/result pair and its group selection.

Limitations:

  • Individual mo-tester logs and the linked issue body were not supplied. BVT success relies on the frozen CI snapshot, supplemented by locally executed public SQL regressions.

  • No performance benchmark was run. Uncontended streaming is tested; conflict-only draining deliberately completes the existing lock-acquisition policy.

@mergify mergify Bot added the queued label Oct 15, 2026
@mergify

mergify Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-10-15 13:27 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-10-15 14:11 UTC · at b715e4d778f6d1d09654f36e8bfbc8df96e0106c

This pull request spent 44 minutes 17 seconds in the queue, with no time running CI.

Reason

Pull request #30268 has been dequeued

Pull request from fork cannot be queued. This pull request comes from a fork, and Mergify needs the author's permission to update its branch.

The author needs to enable "Allow edits from maintainers" on this pull request.

Hint

You should look at the reason for the failure and decide if the pull request needs to be fixed or if you want to requeue it.
If you do update this pull request, it will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue instead, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

Waiting for any of
  • check-neutral = CI Required
  • check-skipped = CI Required
  • check-success = CI Required
All conditions

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request

@XuPeng-SH
XuPeng-SH merged commit 493d8b8 into matrixorigin:main Oct 15, 2026
@mergify mergify Bot added dequeued and removed queued labels Oct 15, 2026
XuPeng-SH pushed a commit that referenced this pull request Oct 16, 2026
## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

Fixes #30245 (4.2 release line).

## What this PR does / why we need it:

Backport #30268 (main commit `493d8b8181b7d74bed91e360874a3dd35223f276`)
to `4.2-dev`.

A locking read with `LIMIT 1` can return an obsolete row after waiting
for a concurrent update because LIMIT stops pulling before LockOp
reports its pending retry. The backport drains and locks the remaining
input without forwarding obsolete batches, then returns the retry.
Uncontended execution remains streaming. The production function is
identical to the merged main fix; no other production code changes.

Release compatibility adaptations are limited to tests: add the
timestamp-waiter and actual-waiter probes absent on this branch, update
the three existing retry-timing expectations, and retain 4.2's
committed-row result for the RR session cases. The unchanged release
source passes both no-LIMIT controls but fails both LIMIT controls with
old value `0` instead of `1`; this backport passes all 11 SQL scenarios.
Main-only definition-fence tests are not introduced.

Validation on Linux amd64 / Go 1.26.4, based on
`9adce306ff4efa9754fd8d9cce051386a1eb0190`:

- Built task-owned native libraries with `make -j4 cgo` and used the
repository CGo test wrapper.
- Complete LockOp normal and race suites: 134 named passes each.
Adaptive focused race repetitions: new operator root 66, the two
conflict roots 100 each, and the row-version root 81; all passed.
- Operator LIMIT regression fails against the exact original release
production source. Public SQL negative controls independently confirm RC
and RR session no-LIMIT reads return `1`, while LIMIT reads return `0`
before the fix.
- SQL regression: all 11 scenarios pass normally. All 11 scenarios also
pass with race detection and in the separate adaptive race run (measured
17.13 s, 30 s budget, count=1).
- Distributed `locking_read_limit_retry.sql`: normal mo-tester
comparison twice on the same fresh instance, 31/31 statements each;
database removal verified between runs and after the second run; fixture
stopped.
- Incremental configured golangci-lint v2.6.2, `go vet`, gofmt, and diff
checks passed for the affected packages.

Tradeoff: a pending retry finishes acquiring the remaining input locks,
so a conflicting limited query can process additional input. This
preserves the existing lock-acquisition policy. Full release CI remains
pending; this PR does not itself publish a new 4.2.x release.

This branch was previously deployed

1 inactive deployment
ci — b715e4d7 Deployed Oct 15, 2026 by mergify[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dequeued kind/bug Something isn't working size/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: SELECT ... LIMIT 1 FOR UPDATE 等锁结束后读到持锁事务提交前的旧值

3 participants