Re: Add proper planner support for ORDER BY / DISTINCT aggregates
Richard Guo <guofenglinux@gmail.com>
From: Richard Guo <guofenglinux@gmail.com>
To: David Rowley <dgrowleyml@gmail.com>
Cc: Dean Rasheed <dean.a.rasheed@gmail.com>,
Ronan Dunklau <ronan.dunklau@aiven.io>, Justin Pryzby <pryzby@telsasoft.com>,
Pavel Luzanov <p.luzanov@postgrespro.ru>, Tom Lane <tgl@sss.pgh.pa.us>, pgsql-hackers@lists.postgresql.org, Ranier Vilela <ranier.vf@gmail.com>
Date: 2023-01-11T08:47:05Z
Lists: pgsql-hackers
On Wed, Jan 11, 2023 at 12:13 PM David Rowley <dgrowleyml@gmail.com> wrote:
> postgres=# set enable_presorted_aggregate=0;
> SET
> postgres=# select string_agg(random()::text, ',' order by random())
> from generate_series(1,3);
> string_agg
> -----------------------------------------------------------
> 0.8659110018246505,0.15612649559563474,0.2022878955613403
> (1 row)
>
> I'd have expected those random numbers to be concatenated in ascending
> order.
select string_agg(
random()::text, -- position 1
','
order by random() -- position 2
)
from generate_series(1,3);
I traced this query a bit and found that when executing the aggregation
the random() function in the aggregate expression (position 1) and in
the order by clause (position 2) are calculated separately. And the
sorting is performed based on the function results from the order by
clause. In the final output, what we see is the function results from
the aggregate expression. Thus we'll notice the output is not sorted.
I'm not sure if this is expected or broken though.
BTW, if we explicitly add ::text for random() in the order by clause, as
select string_agg(
random()::text,
','
order by random()::text
)
from generate_series(1,3);
The random() function will be calculated only once for each tuple, and
we can get a sorted output.
Thanks
Richard
Commits
-
Don't presort ORDER BY/DISTINCT Aggrefs with volatile functions
- da5800d5fa63 16.0 landed
-
Add enable_presorted_aggregate GUC
- 3226f47282a0 16.0 landed
-
Remove pessimistic cost penalization from Incremental Sort
- 4a29eabd1d91 16.0 landed
-
Fix hypothetical problem passing the wrong GROUP BY pathkeys
- af7d270dd3c7 16.0 landed
-
Remove unused fields from ExprEvalStep
- 9fc1776dda9f 16.0 landed
-
Improve performance of ORDER BY / DISTINCT aggregates
- 1349d2790bf4 16.0 landed
-
Refactor function parse_subscription_options.
- 8aafb0261675 15.0 cited