Details
-
Task
-
Status: Open (View Workflow)
-
Major
-
Resolution: Unresolved
-
13.2
-
None
-
None
-
Q4/2026 Server Development
Description
Initially, no join buffers in worker thread
setup_worker_jointabs() stripped the cache from every worker JOIN_TAB and joined one row at a time. To keep results correct, the worker also received the buffer's scan filter (cache_select->cond) along with select_cond.
- The worker then ran a different plan from the one costed. BNL, BNLH, BKA and BKAH are chosen because they scan the inner table once per full buffer. Row by row, the worker scanned it once per driving row.
With a full-scan inner table that can be orders of magnitude more work, enough to make the parallel plan lose to the serial one it was meant to beat. - So the gate refused the plan, "as the prototype did". It checks tab->cache || tab->use_join_cache in can_run_query_in_workers(), and the optimizer trace names the table. The row-by-row handling was removed at the same time.
Parallel query: run join buffers (BNL/BNLH) in the workers
Why it's needed
Without join buffers in the workers, a parallel plan had two choices for a table read through a join buffer, and both were bad:
- Strip the buffer. Originally the worker removed the buffer from its copy of the plan and joined one row at a time. That rescans the inner table once per driving row instead of once per full buffer. For
SELECT * FROM s1 JOIN s2 ON (1) (1000 × 2000 rows) that cost an order of magnitude. - Refuse the plan. Later, the parallel gate refused any plan containing a join buffer, and the costing avoided choosing one under a parallel driver. That kept the workers out of every query where a buffer is the right join method:
- cross joins;
- joins with no usable index;
- small dimension tables scanned under the divided driving table, such as ship_mode in TPC-DS Q66.
Approach
Each worker builds and runs its own buffer, of the same kind as the serial plan's, on its own copies of the tables.
- Nothing is shared between workers. A worker with 1/N of the driving rows fills its buffer 1/N as often, so the inner table is scanned about as many times in total as in the serial plan. At most each worker adds one extra scan for its last partly filled buffer, and the workers' scans run concurrently.
- Buffer size: each worker's buffer is the session's join_buffer_size, so total buffer memory is up to N × join_buffer_size.
- Hash joins (BNLH) are supported. The hash key descriptor is cloned onto the worker's own fields.
- BKA/BKAH are not supported, because they read the inner table through multi-range read in the handler. The gate refuses them. A plan chosen for the workers is capped at join_cache_level 4, so the costing never picks a plan the gate would refuse.
Result
On the cross join above with 4 workers, ANALYZE shows s2 scanned twice in total (once per worker that received rows). The row-at-a-time version scanned it 1000 times. TPC-H SF1 Q3, Q5, Q10 and Q12 keep the same plans and timings.