Re: Add proper planner support for ORDER BY / DISTINCT aggregates

From: Pavel Luzanov <p(dot)luzanov(at)postgrespro(dot)ru>
To: Ronan Dunklau <ronan(dot)dunklau(at)aiven(dot)io>, David Rowley <dgrowleyml(at)gmail(dot)com>, Justin Pryzby <pryzby(at)telsasoft(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Richard Guo <guofenglinux(at)gmail(dot)com>, pgsql-hackers(at)lists(dot)postgresql(dot)org, Ranier Vilela <ranier(dot)vf(at)gmail(dot)com>
Subject: Re: Add proper planner support for ORDER BY / DISTINCT aggregates
Date: 2022-11-07 20:37:55
Message-ID: 017612dc-3bce-0d08-096e-db8605df9dbe@postgrespro.ru
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 07.11.2022 20:30, Ronan Dunklau wrote:
> What I meant here is that disabling seqscans, the planner still chooses a full
> sort over a partial sort. The underlying index is the same, it is just a
> matter of choosing a Sort node over an IncrementalSort node. This, I think, is
> wrong: I can't see how it could be worse to use an incrementalsort in that
> case.

I finally get your point. And I agree with you.

> Maybe the original costing code for incremental sort was a bit too
> pessimistic.

In this query, incremental sorting lost just a little bit in cost:
164468.95 vs 162504.23.

QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------
 GroupAggregate  (cost=155002.98..162504.23 rows=100 width=34) (actual
time=296.591..568.270 rows=100 loops=1)
   Group Key: a
   ->  Sort  (cost=155002.98..157502.98 rows=1000000 width=4) (actual
time=293.810..454.170 rows=1000000 loops=1)
         Sort Key: a, c
         Sort Method: external merge  Disk: 15560kB
         ->  Index Scan using t_a_b_idx on t (cost=0.42..41670.64
rows=1000000 width=4) (actual time=0.021..156.441 rows=1000000 loops=1)
 Settings: enable_seqscan = 'off'
 Planning Time: 0.074 ms
 Execution Time: 569.957 ms
(9 rows)

set enable_sort=off;
SET
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------
 GroupAggregate  (cost=1457.58..164468.95 rows=100 width=34) (actual
time=6.623..408.833 rows=100 loops=1)
   Group Key: a
   ->  Incremental Sort  (cost=1457.58..159467.70 rows=1000000 width=4)
(actual time=2.652..298.530 rows=1000000 loops=1)
         Sort Key: a, c
         Presorted Key: a
         Full-sort Groups: 100  Sort Method: quicksort  Average Memory:
27kB  Peak Memory: 27kB
         Pre-sorted Groups: 100  Sort Method: quicksort  Average
Memory: 697kB  Peak Memory: 697kB
         ->  Index Scan using t_a_b_idx on t (cost=0.42..41670.64
rows=1000000 width=4) (actual time=0.011..155.260 rows=1000000 loops=1)
 Settings: enable_seqscan = 'off', enable_sort = 'off'
 Planning Time: 0.044 ms
 Execution Time: 408.867 ms

--
Pavel Luzanov
Postgres Professional: https://postgrespro.com
The Russian Postgres Company

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Fabien COELHO 2022-11-07 20:46:22 Re: [PATCH] pgbench: add multiconnect option
Previous Message Corey Huinker 2022-11-07 20:27:40 Re: psql: Add command to use extended query protocol