Show estimated number of groups for IncrementalSort in EXPLAIN
Hi hackers,
In [0]/messages/by-id/6642af90-561c-4f0c-9d5b-7e288e6e7f84@gmail.com, a question was raised whether we should expose IncrementalSort
group estimation in EXPLAIN, as we did for Memoize. Knowing the
estimated number of groups helps understand why the planner chose
IncrementalSort whether a bad estimate is to blame for a sub-optimal plan.
Example of EXPLAIN:
```
CREATE TABLE t (a int, b int, c int);
CREATE INDEX ON t (a);
INSERT INTO t SELECT i % 100, (random() * 1000)::int, (random() *
1000)::int FROM generate_series(1, 100000) i;
ANALYZE t;
SET enable_seqscan = off;
EXPLAIN ANALYZE SELECT * FROM t ORDER BY a, b;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------
Incremental Sort (cost=90.80..10302.90 rows=100000 width=12) (actual
time=6.715..29.592 rows=100000.00 loops=1)
Sort Key: a, b
Presorted Key: a
*Estimated Groups: 100*
Full-sort Groups: 100 Sort Method: quicksort Average Memory: 27kB
Peak Memory: 27kB
Pre-sorted Groups: 100 Sort Method: quicksort Average Memory:
56kB Peak Memory: 56kB
Buffers: shared hit=54201 dirtied=1 written=1
-> Index Scan using t_a_idx on t (cost=0.29..4068.01 rows=100000
width=12) (actual time=0.346..20.403 rows=100000.00 loops=1)
Index Searches: 1
Buffers: shared hit=54201 dirtied=1 written=1
Planning:
Buffers: shared hit=21
Planning Time: 0.411 ms
Execution Time: 30.530 ms
(14 rows)
```
Thoughts?
[0]: /messages/by-id/6642af90-561c-4f0c-9d5b-7e288e6e7f84@gmail.com
/messages/by-id/6642af90-561c-4f0c-9d5b-7e288e6e7f84@gmail.com
--
Best regards,
Ilia Evdokimov,
Tantor Labs LLC,
https://tantorlabs.com/
Attachments:
v1-0001-Show-estimated-number-of-groups-for-IncrementalSo.patchtext/x-patch; charset=UTF-8; name=v1-0001-Show-estimated-number-of-groups-for-IncrementalSo.patchDownload+26-8
Hi, thanks for working on this.
On 10/06/26 17:50, Ilia Evdokimov wrote:
*Estimated Groups: 100*
I think it's useful to know the estimated number of groups to understand
why the planner chooses IncrementalSort. The patch looks clean.
There are no tests for this change, although it should be safe, you might
want to consider adding one to `src/test/regress/sql/incremental_sort.sql`.
Best regards,
Enrique.
On 6/17/26 01:51, Enrique Sánchez wrote:
I think it's useful to know the estimated number of groups to
understand why the planner chooses IncrementalSort. The patch looks clean.There are no tests for this change, although it should be safe, you
might want to consider adding one to
`src/test/regress/sql/incremental_sort.sql`.
Memoize has similar planner estimation output in EXPLAIN, but has no
regression tests for it either. Besides, in `inremental_sort` test all
EXPLAIN calls use COSTS OFF, so the new line would not appear there at
all. To be honest, I don't see any reason to display it in regression tests.
--
Best regards,
Ilia Evdokimov,
Tantor Labs LLC,
https://tantorlabs.com/
On Thu, 11 Jun 2026 at 03:50, Ilia Evdokimov
<ilya.evdokimov@tantorlabs.com> wrote:
In [0], a question was raised whether we should expose IncrementalSort group estimation in EXPLAIN, as we did for Memoize. Knowing the estimated number of groups helps understand why the planner chose IncrementalSort whether a bad estimate is to blame for a sub-optimal plan.
[0]: /messages/by-id/6642af90-561c-4f0c-9d5b-7e288e6e7f84@gmail.com
I think it makes sense to have this information in EXPLAIN.
A couple of things about the patch:
1. I think the new est_input_groups field should be Cardinality rather
than double. Various other Path types and Plan nodes call this
"numGroups". I don't see any reason to deviate from that.
2. The header comment for cost_incremental_sort needs to be updated to
mention that p_input_groups may be passed as non-NULL to provide the
caller with the estimated number of sort groups.
David
On 6/23/26 01:39, David Rowley wrote:
A couple of things about the patch:
1. I think the new est_input_groups field should be Cardinality rather
than double. Various other Path types and Plan nodes call this
"numGroups". I don't see any reason to deviate from that.
2. The header comment for cost_incremental_sort needs to be updated to
mention that p_input_groups may be passed as non-NULL to provide the
caller with the estimated number of sort groups.
Fixed in v2-patch.
--
Best regards,
Ilia Evdokimov,
Tantor Labs LLC,
https://tantorlabs.com/
Hi all,
On Tue, Jun 23, 2026 at 3:22 PM Ilia Evdokimov
<ilya.evdokimov@tantorlabs.com> wrote:
Hi hackers,
In [0], a question was raised whether we should expose IncrementalSort group estimation in EXPLAIN, as we did for Memoize. Knowing the estimated number of groups helps understand why the planner chose IncrementalSort whether a bad estimate is to blame for a sub-optimal plan.
Example of EXPLAIN:
```
CREATE TABLE t (a int, b int, c int);
CREATE INDEX ON t (a);
INSERT INTO t SELECT i % 100, (random() * 1000)::int, (random() * 1000)::int FROM generate_series(1, 100000) i;
ANALYZE t;
SET enable_seqscan = off;EXPLAIN ANALYZE SELECT * FROM t ORDER BY a, b;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------
Incremental Sort (cost=90.80..10302.90 rows=100000 width=12) (actual time=6.715..29.592 rows=100000.00 loops=1)
Sort Key: a, b
Presorted Key: a
Estimated Groups: 100
Full-sort Groups: 100 Sort Method: quicksort Average Memory: 27kB Peak Memory: 27kB
Pre-sorted Groups: 100 Sort Method: quicksort Average Memory: 56kB Peak Memory: 56kB
Buffers: shared hit=54201 dirtied=1 written=1
-> Index Scan using t_a_idx on t (cost=0.29..4068.01 rows=100000 width=12) (actual time=0.346..20.403 rows=100000.00 loops=1)
Index Searches: 1
Buffers: shared hit=54201 dirtied=1 written=1
Planning:
Buffers: shared hit=21
Planning Time: 0.411 ms
Execution Time: 30.530 ms
(14 rows)
```Thoughts?
[0]: /messages/by-id/6642af90-561c-4f0c-9d5b-7e288e6e7f84@gmail.com
Thank you for the patch and the idea of exposing the estimated number
of groups for Incremental Sort in EXPLAIN makes sense to me. I applied
the patch on current master and verified the new output using a simple
Incremental Sort plan:
Incremental Sort (cost=86.88..9911.06 rows=100000 width=8)
Sort Key: a, b
Presorted Key: a
Estimated Groups: 100
I also verified that the field is displayed in both EXPLAIN and
EXPLAIN ANALYZE output, the field is omitted when EXPLAIN is executed
with COSTS OFF, displayed value matches the planner's estimate used
for costing, the output formatting looks correct and the new line
appears in an appropriate location between "Presorted Key" and the
execution statistics shown by EXPLAIN ANALYZE. Additionally, I tested
a parallel plan and confirmed that the estimate is displayed correctly
when Incremental Sort appears below a Gather Merge node:
Gather Merge (cost=4885.06..6358.53 rows=100000 width=8)
Workers Planned: 4
-> Sort (cost=4885.00..4947.50 rows=25000 width=8)
Sort Key: pt.a, pt.b
-> Parallel Append (cost=0.29..3058.80 rows=25000 width=8)
-> Parallel Index Scan using pt_p1_a_idx on pt_p1 pt_1
(cost=0.29..1467.02 rows=12500 width=8)
-> Parallel Index Scan using pt_p2_a_idx on pt_p2 pt_2
(cost=0.29..1466.78 rows=12500 width=8)
I also tested a partitioned table setup and verified that the estimate
is displayed correctly for Incremental Sort plans built on top of
partitioned relations:
Incremental Sort (cost=92.24..10418.69 rows=100000 width=8)
Sort Key: pt.a, pt.b
Presorted Key: pt.a
Estimated Groups: 100
-> Append (cost=0.58..4183.80 rows=100000 width=8)
-> Index Scan using pt_p1_a_idx on pt_p1 pt_1 (cost=0.29..1842.02
rows=50000 width=8)
-> Index Scan using pt_p2_a_idx on pt_p2 pt_2
(cost=0.29..1841.78 rows=50000 width=8)
(7 rows)
During testing I observed that the estimated number of groups differed
significantly which demonstrated the usefulness of exposing this
planner estimate for diagnosing cardinality estimation issues
affecting Incremental Sort costing. So far no issue with the patch and
the patch looks good to me. Looking forward to more feedback.
Regards,
Solai
I noticed that doc contains a query with EXPLAIN output for an
Incremental Sort plan, and we should add new line with Estimated Groups.
I added v3-patch with it.
--
Best regards,
Ilia Evdokimov,
Tantor Labs LLC,
https://tantorlabs.com/
On Wed, 29 Jul 2026 at 22:14, Ilia Evdokimov
<ilya.evdokimov@tantorlabs.com> wrote:
I noticed that doc contains a query with EXPLAIN output for an
Incremental Sort plan, and we should add new line with Estimated Groups.
I added v3-patch with it.
I did some very minor adjustments to align some parameter names so
they used the same naming convention as the other parameters, and also
some alignment of comments for the new numGroups fields to make the
comments the same as the other places that have a field by that name
and for the same purpose.
Now pushed.
David
Great!
Thank you for pushing.
Regards,
Solai
Show quoted text
On Tue, Aug 11, 2026 at 7:07 AM David Rowley <dgrowleyml@gmail.com> wrote:
On Wed, 29 Jul 2026 at 22:14, Ilia Evdokimov
<ilya.evdokimov@tantorlabs.com> wrote:I noticed that doc contains a query with EXPLAIN output for an
Incremental Sort plan, and we should add new line with Estimated Groups.
I added v3-patch with it.I did some very minor adjustments to align some parameter names so
they used the same naming convention as the other parameters, and also
some alignment of comments for the new numGroups fields to make the
comments the same as the other places that have a field by that name
and for the same purpose.Now pushed.
David