From: Thomas Kellerer <shammat@gmx.net>
To: pgsql-general@lists.postgresql.org
Subject: Re: Reproducing incorrect order with order by in a subquery
Date: Tue, 13 Jun 2023 12:26:40 +0200
Message-ID: <dd2f192c-2b90-2bb7-27b4-004af7e7e26a@gmx.net> (raw)
In-Reply-To: <CAMOxC8tBBR8DK3c73zUWSWRzTPP2FHv5+sLFqL2ZWWwLqGmzhw@mail.gmail.com>
References: <CAMOxC8tBBR8DK3c73zUWSWRzTPP2FHv5+sLFqL2ZWWwLqGmzhw@mail.gmail.com>
Ruslan Zakirov schrieb am 13.06.2023 um 09:49:
> For example I have a query:
>
> SELECT main.*, count(*) OVER () FROM (SELECT DISTINCT ... ORDER BY X)
> main;
>
> So the `ORDER BY` clause ended up in a subquery. Most of the time
> ordering works until it doesn't.
>
> Can you help me create a set of test tables with some data to
> reproduce this problem more repeatedly? I just want to write a
> regression test to make sure it doesn't happen again.
Your final/overall query has no ORDER BY, so Postgres is free to return the result in any order it likes.
You will have to add an ORDER BY to the "main" part to get a guaranteed sort order
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: shammat@gmx.net
Subject: Re: Reproducing incorrect order with order by in a subquery
In-Reply-To: <dd2f192c-2b90-2bb7-27b4-004af7e7e26a@gmx.net>
References: <CAMOxC8tBBR8DK3c73zUWSWRzTPP2FHv5+sLFqL2ZWWwLqGmzhw@mail.gmail.com> <dd2f192c-2b90-2bb7-27b4-004af7e7e26a@gmx.net>
* 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