pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedOrder by in a sub query when aggregating the main query
4+ messages / 2 participants
[nested] [flat]
* Order by in a sub query when aggregating the main query
@ 2022-09-24 21:38 Federico <cfederico87@gmail.com>
2022-09-24 22:20 ` Re: Order by in a sub query when aggregating the main query Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 4+ messages in thread
From: Federico @ 2022-09-24 21:38 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Hi all,
I have a question related to the order by clause used in a subquery of
a main query that uses one or more aggregation functions with a group
by.
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?
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Order by in a sub query when aggregating the main query
2022-09-24 21:38 Order by in a sub query when aggregating the main query Federico <cfederico87@gmail.com>
@ 2022-09-24 22:20 ` Tom Lane <tgl@sss.pgh.pa.us>
2022-09-24 22:30 ` Re: Order by in a sub query when aggregating the main query Federico <cfederico87@gmail.com>
0 siblings, 1 reply; 4+ messages in thread
From: Tom Lane @ 2022-09-24 22:20 UTC (permalink / raw)
To: Federico <cfederico87@gmail.com>; +Cc: pgsql-general@lists.postgresql.org
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
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Order by in a sub query when aggregating the main query
2022-09-24 21:38 Order by in a sub query when aggregating the main query Federico <cfederico87@gmail.com>
2022-09-24 22:20 ` Re: Order by in a sub query when aggregating the main query Tom Lane <tgl@sss.pgh.pa.us>
@ 2022-09-24 22:30 ` Federico <cfederico87@gmail.com>
2022-09-27 21:11 ` Re: Order by in a sub query when aggregating the main query Federico <cfederico87@gmail.com>
0 siblings, 1 reply; 4+ messages in thread
From: Federico @ 2022-09-24 22:30 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-general@lists.postgresql.org
Understood, thanks for the explanation.
I'll work on updating the queries used by sqlalchemy to do array_agg(x
order by x) instead of the order by in the subquery.
> 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.
Nice to know, hopefully it's too bad for this use case
Thanks, Federico Caselli
On Sun, 25 Sept 2022 at 00:20, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>
> 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
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Order by in a sub query when aggregating the main query
2022-09-24 21:38 Order by in a sub query when aggregating the main query Federico <cfederico87@gmail.com>
2022-09-24 22:20 ` Re: Order by in a sub query when aggregating the main query Tom Lane <tgl@sss.pgh.pa.us>
2022-09-24 22:30 ` Re: Order by in a sub query when aggregating the main query Federico <cfederico87@gmail.com>
@ 2022-09-27 21:11 ` Federico <cfederico87@gmail.com>
0 siblings, 0 replies; 4+ messages in thread
From: Federico @ 2022-09-27 21:11 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-general@lists.postgresql.org
I've changed the code to use order by in the aggregate and it seems
there are no noticeable changes in the query performance.
Thanks for the help.
Best,
Federico Caselli
On Sun, 25 Sept 2022 at 00:30, Federico <cfederico87@gmail.com> wrote:
>
> Understood, thanks for the explanation.
> I'll work on updating the queries used by sqlalchemy to do array_agg(x
> order by x) instead of the order by in the subquery.
>
> > 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.
>
> Nice to know, hopefully it's too bad for this use case
>
> Thanks, Federico Caselli
>
> On Sun, 25 Sept 2022 at 00:20, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> >
> > 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
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2022-09-27 21:11 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-09-24 21:38 Order by in a sub query when aggregating the main query Federico <cfederico87@gmail.com>
2022-09-24 22:20 ` Tom Lane <tgl@sss.pgh.pa.us>
2022-09-24 22:30 ` Federico <cfederico87@gmail.com>
2022-09-27 21:11 ` Federico <cfederico87@gmail.com>
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