Conversation
When ANALYZE builds statistics for a partitioned table, it adds up the per-segment ndistinct of all partitions. If the same values appear in every partition, they are counted once per partition, so the result is too big: 8 partitions give 8 times the real value. ORCA uses this number to estimate how many rows a partial aggregate returns. With the inflated number it thinks the partial aggregate removes almost no rows, and picks a one-stage aggregate that sends all rows through the motion. A segment cannot have more distinct values than the whole table, so cap the value at the table's ndistinct times the number of segments.
Alena0704
marked this pull request as draft
September 17, 2026 14:14
Alena0704
marked this pull request as ready for review
September 17, 2026 20:00
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.
ORCA does not use two-stage aggregation on partitioned tables
ANALYZE adds up the per-segment ndistinct of all partitions to get the value
for the parent table (
STATISTIC_KIND_NDV_BY_SEGMENTS). When the same valuesare in every partition, they are counted once per partition, so the value is
too big.
ORCA uses this value to guess how many rows are left after the partial
aggregate. With a value that is too big, ORCA thinks the partial aggregate
does not help and sends all rows through the motion. The more partitions and
grouping columns, the bigger the error.
A segment cannot have more distinct values than the whole table, so
cap the value at the table's ndistinct times the number of segments.
Reproduction (3 segments, ORCA):
Before (
REL_2_STABLE): one-stage plan, all 7M rows go through the motion.After: two-stage plan, only 660 rows go through the motion.
Type of Change
Breaking Changes
Test Plan
make installcheckmake -C src/test installcheck-cbdb-parallelImpact
Performance:
User-facing changes:
Dependencies:
Checklist
Additional Context
CI Skip Instructions