Re: Clarify the ordering guarantees in combining queries (or lack thereof)

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Shay Rojansky <roji(at)roji(dot)org>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-docs <pgsql-docs(at)postgresql(dot)org>
Subject: Re: Clarify the ordering guarantees in combining queries (or lack thereof)
Date: 2022-07-14 12:28:44
Message-ID: CAKFQuwbzdxypF0Gb9N-3eQsnrgy5r6pdWF2a6XnZWf9L2-EbYA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-docs

On Thursday, July 14, 2022, Shay Rojansky <roji(at)roji(dot)org> wrote:

>
> If there's a guarantee that UNION ALL preserves ordering - as Tom seems to
> indicate in the thread quoted above - then the above works. If there's no
> such guarantee, then AFAIK the above can't be rewritten; putting the ORDER
> BY outside - on the results of the UNION ALL - would order all results
> rather than preserving each resultset's ordering.
>
>
Yes, an order by outside the union will sort the union results as a whole.
You can still write an order by and the union all so you get any
conceivable ordering, though it may possibly require putting the union into
a subquery depending on the order and output column combination desired.

David J.

In response to

Browse pgsql-docs by date

  From Date Subject
Next Message PG Doc comments form 2022-07-14 12:47:18 pg_advisory_unlock(null)
Previous Message Tom Lane 2022-07-14 12:01:48 Re: Clarify the ordering guarantees in combining queries (or lack thereof)