Skip to content

[adapters] size the datafusion sort reservation from the pool - #6870

Open
ryzhyk wants to merge 1 commit into
mainfrom
fix-datafusion-sort-reservation
Open

[adapters] size the datafusion sort reservation from the pool#6870
ryzhyk wants to merge 1 commit into
mainfrom
fix-datafusion-sort-reservation

Conversation

@ryzhyk

@ryzhyk ryzhyk commented Aug 15, 2026

Copy link
Copy Markdown
Contributor

We were incorrectly configuring datafusion's sort_spill_reservation_bytes. We unconditionally configured 64MiB per worker. It turns out that datafusion insists on reserving the configured number of bytes unconditionally before processing any data, even if it's only sorting a few records. If this reservation (64MiB x the number of workers) exceeds the datafusion memory pool size, it fails. Even if it fits, it can starve other datafusion operators and cause them to fail.

The new policy we implement is to allocate 25% of the memory pool to spill-to-disk sorting. This allocation is split across all partitions. If the resulting per-partition allocation is too small, then we fall back to slower single-threaded full sorting.

The next issue we fix is that we estimate datafusion partition count as the local worker count as opposed to the total number of workers in a multihost config.

This still leaves an important gap. The 25% memory pool allocation is per datafusion session. Multiple sessions started by multiple connector can still oversubscribe the pool.

Describe Manual Test Plan

Checklist

  • Unit tests added/updated
  • Integration tests added/updated
  • Documentation updated
  • Changelog updated

Breaking Changes?

Mark if you think the answer is yes for any of these components:

Describe Incompatible Changes

We were incorrectly configuring datafusion's `sort_spill_reservation_bytes`.
We unconditionally configured 64MiB per worker. It turns out that datafusion
insists on reserving the configured number of bytes unconditionally before
processing any data, even if it's only sorting a few records. If this
reservation (64MiB x the number of workers) exceeds the datafusion memory pool
size, it fails. Even if it fits, it can starve other datafusion operators and
cause them to fail.

The new policy we implement is to allocate 25% of the memory pool to
spill-to-disk sorting. This allocation is split across all partitions. If the
resulting per-partition allocation is too small, then we fall back to slower
single-threaded full sorting.

The next issue we fix is that we estimate datafusion partition count as the
*local* worker count as opposed to the total number of workers in a multihost
config.

This still leaves an important gap. The 25% memory pool allocation is per
datafusion session. Multiple sessions started by multiple connector can still
oversubscribe the pool.

Signed-off-by: Leonid Ryzhyk <ryzhyk@gmail.com>
@ryzhyk
ryzhyk requested a review from swanandx August 15, 2026 21:22
@ryzhyk ryzhyk added connectors Issues related to the adapters/connectors crate adhoc Issue related to ad hoc query processing labels Aug 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

adhoc Issue related to ad hoc query processing connectors Issues related to the adapters/connectors crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant