Open
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## sortedMerge #8545 +/- ##
==============================================
Coverage ? 44.57%
==============================================
Files ? 287
Lines ? 63183
Branches ? 7793
==============================================
Hits ? 28164
Misses ? 32534
Partials ? 2485 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Streaming Sorted Merge Adapter
Summary
Adds a streaming k-way merge adapter controlled by
citus.enable_streaming_sorted_merge(bool, defaultoff, experimental). When enabled alongsidecitus.enable_sorted_merge, the coordinator returns globally-sorted tuples directly from the binary heap instead of eagerly materializing all results into a tuplestore first. This reduces peak memory and improves time-to-first-tuple, especially forLIMITqueries.Changes
SortedMergeAdapter(new opaque type insorted_merge.c)CreateSortedMergeAdapter()SortedMergeAdapterNext()SortedMergeAdapterRescan()CitusReScan)FreeSortedMergeAdapter()CitusEndScan)MergePerTaskStoresIntoFinalStore()is refactored to use the adapter internally (drain + free), eliminating duplicated merge logic.Executor
adaptive_executor.c: Creates aSortedMergeAdapterwith store ownership when streaming is active; falls back to eager merge otherwise.multi_executor.c: NewFetchNextScanTuple()dispatches between adapter and tuplestore. Includes a defensive error for backward scan requests that bypass the Material node (should never fire in practice — see below).citus_custom_scan.c: Cleanup inCitusEndScan(); rescan inCitusReScan().CitusScanState: NewmergeAdapterfield.Backward scan & SCROLL cursors
The streaming adapter is forward-only by design. Two planner-level changes ensure SCROLL cursors still work correctly using standard PostgreSQL APIs:
Drop
CUSTOMPATH_SUPPORT_BACKWARD_SCAN: InFinalizePlan(), when streaming sorted merge is active, theCustomScannode's flags no longer includeCUSTOMPATH_SUPPORT_BACKWARD_SCAN(extensible.h:0x0001). This tells PostgreSQL'sExecSupportsBackwardScan()that our node cannot satisfy backward fetches.Insert a Material node for SCROLL cursors: PostgreSQL's
standard_planner()normally callsmaterialize_finished_plan()to wrap non-backward-scannable plans in a Material node whenCURSOR_OPT_SCROLLis set. However, Citus replaces the entire plan tree afterstandard_planner()returns (viaFinalizePlan()), discarding any Material node it inserted. We therefore re-perform the same check inCreateDistributedPlannedStmt():The Material node buffers tuples and provides bidirectional access. The streaming adapter only ever sees forward requests. Test case
G3bverifies thatFETCH BACKWARDon aSCROLLcursor produces correct results with streaming mode active.Tests
multi_orderby_pushdown_streaming.sql(new): Runs the full sorted-merge test suite twice — eager merge, then streaming — verifying identical results. IncludesG3bfor SCROLL cursor backward scan.setup_multi_orderby_pushdown.sql(new): Extracted shared table setup.Testing
check-multi: 193/193 passcheck-style: pass