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

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: David Rowley <dgrowleyml(at)gmail(dot)com>
Cc: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>, Ronan Dunklau <ronan(dot)dunklau(at)aiven(dot)io>, Justin Pryzby <pryzby(at)telsasoft(dot)com>, Pavel Luzanov <p(dot)luzanov(at)postgrespro(dot)ru>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, 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: 2023-01-11 08:47:05
Message-ID: CAMbWs4-q4zD96d_2HdZ8gf=g0seiBk2UYFOMuq9mG5-r82dKjQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, Jan 11, 2023 at 12:13 PM David Rowley <dgrowleyml(at)gmail(dot)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

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message shiy.fnst@fujitsu.com 2023-01-11 08:56:28 RE: Allow logical replication to copy tables in binary format
Previous Message Melih Mutlu 2023-01-11 08:31:12 Re: [PATCH] Reuse Workers and Replication Slots during Logical Replication