Skip to content

fix: narrow pessimistic insert table lock window - #24231

Merged
mergify[bot] merged 4 commits into
matrixorigin:mainfrom
LeftHandCold:backport/pr24201-main
Apr 29, 2026
Merged

mergify[bot] merged 4 commits into
matrixorigin:mainfrom
LeftHandCold:backport/pr24201-main

Conversation

@LeftHandCold

Copy link
Copy Markdown
Collaborator

What type of PR is this?

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

Which issue(s) this PR fixes:

fixes #24200

What this PR does / why we need it:

This backports #24201 to main.

For pessimistic INSERT ... SELECT ... statements that escalate to table locks, Compile.compileLock was still keeping the target table on the pre-pipeline lock path. That can make a large source scan hold the target-table lock before the write side starts running and block later writers for too long.

This backport keeps the same table-lock semantics, but lets LockOp acquire the table lock when the first batch reaches the target pipeline. It also preserves EOF-time table locking for the zero-row path so inserts that produce no rows still keep the intended table-lock behavior.

Also included:

  • a compile unit test for the pre-pipeline lock routing
  • a lockop unit test for the zero-row EOF table-lock path

Validation

  • go test ./pkg/sql/compile ./pkg/sql/colexec/lockop -count=1 (blocked in this Darwin environment because the repository's current CGO dependencies require missing headers such as usearch.h / xxhash.h; the backport was kept aligned with the merged 3.0-dev fix and will rely on CI for full package validation)

Copilot AI review requested due to automatic review settings April 28, 2026 03:25
@matrix-meow matrix-meow added the size/S Denotes a PR that changes [10,99] lines label Apr 28, 2026
@mergify mergify Bot added the kind/bug Something isn't working label Apr 28, 2026
@LeftHandCold LeftHandCold mentioned this pull request Apr 28, 2026
1 of 7 tasks

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

This PR backports a fix to reduce how long pessimistic INSERT ... SELECT ... can hold an escalated table lock by avoiding pre-pipeline table locking for Query_INSERT, deferring acquisition to LockOp while preserving the “lock even on zero rows” semantics via EOF-time table locking.

Changes:

  • Add Compile.shouldPrePipelineLockTable() and route compileLock to skip pre-pipeline table locks for INSERT, setting LockTableAtTheEnd instead.
  • Add a compile unit test covering the pre-pipeline lock routing behavior.
  • Add a lockop unit test covering EOF-time table locking when no rows are produced.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
pkg/sql/compile/compile.go Introduces INSERT-specific logic to defer table lock acquisition to runtime LockOp (plus EOF fallback) rather than pre-pipeline.
pkg/sql/compile/compile_test.go Adds a focused unit test for shouldPrePipelineLockTable behavior across statement types.
pkg/sql/colexec/lockop/lock_op_test.go Adds a unit test asserting EOF-time table locking when the child produces no rows.

Comment thread pkg/sql/colexec/lockop/lock_op_test.go Resolved
@mergify mergify Bot removed the dequeued label Apr 29, 2026
@matrix-meow matrix-meow added size/M Denotes a PR that changes [100,499] lines and removed size/S Denotes a PR that changes [10,99] lines labels Apr 29, 2026
mergify Bot pushed a commit that referenced this pull request Apr 29, 2026
This backports #24226 to `main` on top of #24231.

`#24231` moves regular `Query_INSERT` table locking off the pre-pipeline path. `LOAD DATA` is also planned as `Query_INSERT` and always carries a table-locking `LockOp`, so it needs a narrow exception to stay on the pre-pipeline lock path and avoid retrying the same whole-table lock on every non-empty batch.

This stacked backport keeps the `#24231` behavior for regular INSERT statements, but restores pre-pipeline table locking for `LOAD DATA` only. It also makes `Compile.lockTable()` lock all pre-pipeline targets instead of returning after the first one.

Also included:
- coverage that distinguishes regular INSERT from `LOAD DATA`
- coverage that locks all pre-pipeline targets

Stacking note:
- GitHub cannot base an upstream PR on a fork branch, so this PR currently includes the `#24231` backport in its visible diff as well. After `#24231` lands, this branch should be rebased so reviewers only see the incremental `#24226` change.

Approved by: @aunjgr, @ouyuanning
LeftHandCold and others added 4 commits April 29, 2026 15:31
Replace the flaky wall-clock assertion with a deterministic check that sender only invokes the underlying RPC client once when the backend retry budget is disabled.

Co-authored-by: Copilot <[email protected]>
Comment out the compileAttachedScope function.
@mergify

mergify Bot commented Apr 29, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-04-29 08:54 UTC · Rule: main
  • ✅ Checks skipped · PR is already up-to-date
  • ✅ Merged — 2026-04-29 08:54 UTC · at 0d04426a3f72e474352277440b4c0d8a03820735 · squash

This pull request spent 24 seconds in the queue, including 2 seconds running CI.

Required conditions to merge
  • #approved-reviews-by >= 1 [🛡 GitHub branch protection]
  • #changes-requested-reviews-by = 0 [🛡 GitHub branch protection]
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • branch-protection-review-decision = APPROVED [🛡 GitHub branch protection]
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-neutral = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-skipped = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / SCA Test on Ubuntu/x86
    • check-neutral = Matrixone CI / SCA Test on Ubuntu/x86
    • check-skipped = Matrixone CI / SCA Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH)
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
    • check-neutral = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
    • check-skipped = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Utils CI / Coverage
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage

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.

[Bug]: pessimistic INSERT ... SELECT holds PK table lock too early on purchase_order_detail

6 participants