Do not sort batches for Batch sorted merge over unordered chunks if query sort matches compressed sort - #10303
Conversation
|
@pnthao, @akuzm: please review this pull request.
|
757000f to
597d0d0
Compare
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
|
LLM Fuzzer found an issue needs addressed: https://github.com/timescale/timescaledb/actions/runs/29857661986 Will add tests for multikey orderby as we can't use compressed sort for BSM for multikey orderby over unordered chunks. UPDATE - done. |
597d0d0 to
039fa86
Compare
Oh, I know this problem, sounds like this was actually a violation of Postgres protocol for exec nodes. In Postgres, if |
The high-level logic of the SkipScan node is similar to restarting a correlated subquery with LIMIT 1, which is already reflected in the cost model. SkipScan cannot use the normal parameterized index scan, because fundamentally the skip scan needs ordering, not comparison, and there's no way to use comparison predicates to produce index ordering while correctly handling NULLs. So we use a hacky approach where we directly replace the index scan internals, replacing the entire comparison with "null check" clause when needed. After changing the parameters, the child node must be restarted. At the moment we do it in a custom way that depends on the ColumnarScan internals. This does not follow the standard Postgres API `ExecReScan`, not fully resetting ColumnarScan and leading in turn to other problems that were discovered in #10303. Remove the custom code and use the standard `ExecReScan`. Our manual parameterization doesn't seem to conflict with that.
fc8d8bf to
4456d00
Compare
e101650 to
d0f6206
Compare
antekresic
left a comment
There was a problem hiding this comment.
Approving but would love to see tests without segmentby to double check index is used correctly in that setup as well.
There is a test on |
…uery sort matches compressed sort
d0f6206 to
2467570
Compare
When we apply Batch sorted merge to unordered chunks and query sort is matching sort on compressed orderby leading metadata column, we do not need to sort batches before feeding them into Batch sorted merge heap.
Our batches will already be sorted on (orderby leading metadata column) and the only reason we won't be able to use compressed sort is because the chunk is unordered.
Skipping unnecessary sort for Batch sort merge on unordered chunks can improve performance after Direct compress.
Removing unnecessary sort exposed an existing bug in ColumnarScan exec loop where we didn't quit asking for next batch and then trying to get next batch even after all compressed batches were consumed, as there could still be remaining tuples on the heap.
Sort node hid the bug as it would stop fetching batches residing in a sort buffer, but with the regular column scan we just kept fetching batches as we didn't hit the exit condition.
The bug is now fixed.
Part of work on https://github.com/timescale/eng-database/issues/811
Disable-check: force-changelog-file