Repository navigation
Description
Is there an existing issue for the same bug?
- I have checked the existing issues. Exact searches for
cloneBatchWithAllocationand single-run MergeOrder copying found no owner. [Bug]: MergeOrder leaks native batches after in-memory merge on v4.1.4-hotfix-20260831 #30633 concerns native-batch leaks after in-memory merge; [Performance]: avoid redundant destination NULL prefix scans in string-vector batch appends #31054 concerns destination NULL-prefix scans in string-vector append. Neither covers this clone-before-single-run-send path.
Branch Name
main
Commit ID
Other Environment Information
- Isolated single-node runtime; every formal run returned
git_version()='98b20975'andSELECT 42=42. GOMAXPROCS=4,GOMEMLIMIT=4GiB,max_dop=1; arms ran serially in owned disposable schemas.- N=8,192 and N=16,384; 300 full-result-checked statements per arm under a 20-second symbolized CPU profile at each scale.
- Exact ordered N-row sequences, executed physical structure, caller-specific pprof, and schema cleanup passed at both scales.
- This is a structural executor-path and sampled-CPU report, not a wall-clock-speedup claim.
Actual Behavior
When MergeOrder receives one sorted run, it first clones the complete input batch at order.go:655. It later reaches the single-batch branch whose comment says only one batch, no need to sort. just send it, and sends the cloned batch without performing an in-memory merge. For a wide payload, this adds a complete second executor-owned copy even though no merge output construction is needed.
The target uses a variable 1,280-byte-per-row payload and has one no-spill order N→N → merge order N→N pipeline. The exact control returns the same complete ordered sequence without a Sort operator. There is no LIMIT in either query, so this is not a LIMIT whole-batch copy finding.
Direct caller-specific CPU attribution:
| N | MergeOrder.Call:655 |
Batch.CloneTo:1244 through mergeorder.cloneBatchWithAllocation |
Control clone helper |
|---|---|---|---|
| 8,192 | 0.46s cumulative | 0.46s cumulative | absent |
| 16,384 | 0.93s cumulative | 0.93s cumulative | absent |
The peek chain is MergeOrder.Call → mergeorder.cloneBatchWithAllocation → Batch.CloneTo; Order's own input-retention copies are not counted as MergeOrder copy evidence.
Expected Behavior
For a single resident sorted run that will be sent unchanged, MergeOrder should use an ownership-safe forwarding path rather than cloning every vector before the single-batch send. Any change must preserve input/output ownership, the resident allocation view, order-key cleanup, memory accounting, reset/reuse, spill transitions, and error-path cleanup. Multi-run merge and spill behavior are outside this request.
Steps to Reproduce
Use the following helper with N=8,192 and then N=16,384:
def mergecopy_query(arm, n):
payload = "REPEAT(LPAD(CAST(result AS CHAR),5,'0'),256)"
order = " ORDER BY result" if arm == "target" else ""
return (
"SELECT q.n,LENGTH(q.p) AS p_len FROM ("
"SELECT result AS n," + payload + " AS p "
"FROM generate_series(1," + str(n) + ") g" + order + ") q"
)With SET max_dop=1, both arms must return exactly rows i=1..N, each as (i,1280). Compare the complete normalized row sequence, not only COUNT/SUM. Inspect EXPLAIN, EXPLAIN VERBOSE, and EXPLAIN PHYPLAN ANALYZE; require target order N→N → merge order N→N, no control Sort/MergeOrder, no LIMIT, and zero spill.
Repeat each arm 300 times with full-result checks under separate 20-second CPU profiles:
go tool pprof -list='mergeorder\.\(\*MergeOrder\)\.Call' \
./mo-service mergeorder-target.cpu.pprof
go tool pprof -list='mergeorder\.cloneBatchWithAllocation' \
./mo-service mergeorder-target.cpu.pprof
go tool pprof -list='batch\.\(\*Batch\)\.CloneTo' \
./mo-service mergeorder-target.cpu.pprof
go tool pprof -peek='mergeorder\.cloneBatchWithAllocation' \
./mo-service mergeorder-target.cpu.pprof
go tool pprof -peek='batch\.\(\*Batch\)\.CloneTo' \
./mo-service mergeorder-target.cpu.pprofRelevant source at this commit:
MergeOrder.Calllines 650–659 clones the retained batch.cloneBatchWithAllocationlines 48–63 callssource.CloneTo.normalSendinglines 694–713 sends the sole batch without merging.
Additional information
Evidence includes probe_day12_mergeorder_clone.py and complete plans/profiles/receipts under mergeorder_clone_chain2_n8192/ and mergeorder_clone_chain2_n16384/. One control profile contained a 10ms background MergeOrder.Call sample despite no query MergeOrder node; the gate therefore requires the clone helper and line-655 copy to be absent rather than incorrectly requiring a process-wide Call symbol to disappear.
Activity