pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Federico <cfederico87@gmail.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Order by in a sub query when aggregating the main query
Date: Sat, 24 Sep 2022 18:20:35 -0400
Message-ID: <2617336.1664058035@sss.pgh.pa.us> (raw)
In-Reply-To: <CAN19dycxr2f+GtKLR4X3OMepFzRGaA-uM6dNS6EqaNUEQTScww@mail.gmail.com>
References: <CAN19dycxr2f+GtKLR4X3OMepFzRGaA-uM6dNS6EqaNUEQTScww@mail.gmail.com>

Federico <cfederico87@gmail.com> writes:
> A basic example of the type of query in question is the following (see
> below for the actual query):

>   select w, array_agg(x)
>   from (
>     select v, v / 10 as w
>     from pg_catalog.generate_series(25, 0, -1) as t(v)
>     order by v
>   ) as t(x)
>   group by w

> This query will return an ordered array as specified by the order by
> clause.in the subquery.
> Can this behaviour be relied upon?

No, not really.  It might always work given a particular set of
circumstances.  As long as the planner chooses to do the outer
query's grouped aggregation as a HashAgg, there'd be no reason
for it to reshuffle the subquery output before feeding that to
array_agg.  However, if it decided that sort-group-and-aggregate
was better, it'd insert a sort by w above the subquery, and then
you'd lose any certainty of the ordering by v continuing to hold.
(Maybe the sort by w would be stable for equal keys, but that's
not guaranteed.)

What you really ought to do is write

  select w, array_agg(x order by x)
  from ...

to be in the clear per SQL standard.  I think that right now that'd
incur additional sorting overhead, which is annoying.  But work is
ongoing to recognize when the input is already correctly sorted
for an aggregate, so it should get better in PG 16 or so.

			regards, tom lane





view thread (4+ messages)  latest in thread

Message-ID: <2617336.1664058035@sss.pgh.pa.us>
Permalink:  ../2617336.1664058035@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/2617336.1664058035@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, cfederico87@gmail.com
  Subject: Re: Order by in a sub query when aggregating the main query
  In-Reply-To: <2617336.1664058035@sss.pgh.pa.us>
  References: <CAN19dycxr2f+GtKLR4X3OMepFzRGaA-uM6dNS6EqaNUEQTScww@mail.gmail.com> <2617336.1664058035@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox