Re: BUG #18126: Ordered set aggregate: result does not depend on sort order

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: wollhofen(at)unisoftwareplus(dot)com
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #18126: Ordered set aggregate: result does not depend on sort order
Date: 2023-09-21 15:01:30
Message-ID: 699056.1695308490@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

PG Bug reporting form <noreply(at)postgresql(dot)org> writes:
> Probably related to bug report #18125.
> Using a custom ordered set aggregate, the sort order in the ORDER BY after
> the WITHIN GROUP does not show an influence on the results, as demonstrated
> in the script below.

I think the problem in both of these bugs is that you're expecting
the system to sort the input for you. As per [1], this is not the
case for ordered-set aggregates:

Unlike the case for normal aggregates, the sorting of input rows for
an ordered-set aggregate is not done behind the scenes, but is the
responsibility of the aggregate's support functions. The typical
implementation approach is to keep a reference to a “tuplesort” object
in the aggregate's state value, feed the incoming rows into that
object, and then complete the sorting and read out the data in the
final function. This design allows the final function to perform
special operations such as injecting additional “hypothetical” rows
into the data to be sorted. While normal aggregates can often be
implemented with support functions written in PL/pgSQL or another PL
language, ordered-set aggregates generally have to be written in C,
since their state values aren't definable as any SQL data type.

Perhaps it would be useful to have some kind of shim whereby a
"dumb" transition function could be the basis of an ordered-set
aggregate. But I'm not quite seeing why that would be an
improvement over a traditional aggregate that you use with an
ORDER BY option.

regards, tom lane

[1] https://www.postgresql.org/docs/current/xaggr.html#XAGGR-ORDERED-SET-AGGREGATES

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2023-09-21 16:16:56 Re: 回复:Re: BUG #18118: bug report for COMMIT AND CHAIN feature
Previous Message Dean Rasheed 2023-09-21 11:49:46 Re: BUG #18103: bugs of concurrent merge into when use different join plan