Re: [PATCH] Remove redundant ORDER BY from COUNT aggregates - Mailing list pgsql-hackers

From John Naylor
Subject Re: [PATCH] Remove redundant ORDER BY from COUNT aggregates
Date
Msg-id CANWCAZZzbLFOsaLw-gKpbckLMA7Qhd8ZNO8Wjs0H2juLW8wy+w@mail.gmail.com
Whole thread
Responses Re: Skipping NULL keys when uniqueifying a semijoin's RHS
Re: [PATCH] Remove redundant ORDER BY from COUNT aggregates
List pgsql-hackers
On Wed, Aug 12, 2026 at 11:01 AM Haibo Yan <tristan.yim@gmail.com> wrote:
> I’d like to propose a small optimization for aggregate-local ORDER BY in COUNT.
> Currently, for example:
>
>     SELECT count(a ORDER BY b) FROM t;
>
> is planned as an ordered aggregate, even though the ordering cannot affect the
> result of COUNT.  This may require a Sort, but the impact is broader than just
> the extra sort: having aggorder also prevents partial aggregation and hash
> aggregation, and prevents the aggregate from sharing state with an otherwise
> identical count(a).

The question that comes to mind is, why would someone (or a program
for that matter) write it this way? Self-join elimination cited ORMs
as a motivation, what is it for this case?

--
John Naylor
Amazon Web Services



Attachment

pgsql-hackers by date:

Previous
From: Mats Kindahl
Date:
Subject: Re: pg_rewind does not rewind diverging timelines
Next
From: Pavel Stehule
Date:
Subject: Re: missing possibility to use alternative translated month names in to_char function