Re: Clarify the ordering guarantees in combining queries (or lack thereof) - Mailing list pgsql-docs

From David G. Johnston
Subject Re: Clarify the ordering guarantees in combining queries (or lack thereof)
Date
Msg-id CAKFQuwbzdxypF0Gb9N-3eQsnrgy5r6pdWF2a6XnZWf9L2-EbYA@mail.gmail.com
Whole thread Raw
In response to Re: Clarify the ordering guarantees in combining queries (or lack thereof)  (Shay Rojansky <roji@roji.org>)
List pgsql-docs
On Thursday, July 14, 2022, Shay Rojansky <roji@roji.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.

pgsql-docs by date:

Previous
From: Tom Lane
Date:
Subject: Re: Clarify the ordering guarantees in combining queries (or lack thereof)
Next
From: PG Doc comments form
Date:
Subject: pg_advisory_unlock(null)