Re: Expand applicability of aggregate's sortop optimization - Mailing list pgsql-hackers

From David Rowley
Subject Re: Expand applicability of aggregate's sortop optimization
Date
Msg-id CAApHDvoz_8sVQ_uqd9nXw2jjHAUB=Whwb5chSExeo+SdktrwzA@mail.gmail.com
Whole thread Raw
In response to Expand applicability of aggregate's sortop optimization  (Matthias van de Meent <boekewurm+postgres@gmail.com>)
Responses Re: Expand applicability of aggregate's sortop optimization
List pgsql-hackers
On Wed, 8 May 2024 at 22:13, Matthias van de Meent
<boekewurm+postgres@gmail.com> wrote:
> As you may know, aggregates like SELECT MIN(unique1) FROM tenk1; are
> rewritten as SELECT unique1 FROM tenk1 ORDER BY unique1 USING < LIMIT
> 1; by using the optional sortop field in the aggregator.
> However, this optimization is disabled for clauses that in itself have
> an ORDER BY clause such as `MIN(unique1 ORDER BY <anything>), because
> <anything> can cause reordering of distinguisable values like 1.0 and
> 1.00, which then causes measurable differences in the output. In the
> general case, that's a good reason to not apply this optimization, but
> in some cases, we could still apply the index optimization.

I wonder if we should also consider as an alternative to this to just
have an aggregate support function, similar to
SupportRequestOptimizeWindowClause that just nullifies the aggorder /
aggdistinct fields for Min/Max aggregates on types where there's no
possible difference in output when calling the transition function on
rows in a different order.

Would that apply in enough cases for you?

I think it would rule out Min(numeric) and Max(numeric). We were
careful not to affect the number of decimal places in the numeric
output when using the moving aggregate inverse transition
infrastructure for WindowFuncs, so I agree we should maintain an
ability to control the aggregate transition order for numeric. (See
do_numeric_discard's maxScale if check)

I don't think floating point types have the same issues here. At least
+1.0 is greater than -1.0.

Are there any strange collation rules that would cause issues if we
did this with the text types?

David



pgsql-hackers by date:

Previous
From: Michael Paquier
Date:
Subject: Re: add --no-sync to pg_upgrade's calls to pg_dump and pg_dumpall
Next
From: Michael Paquier
Date:
Subject: Re: Weird test mixup