Re: Reproducing incorrect order with order by in a subquery

From: Ruslan Zakirov <ruslan(dot)zakirov(at)gmail(dot)com>
To: Thomas Kellerer <shammat(at)gmx(dot)net>
Cc: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Reproducing incorrect order with order by in a subquery
Date: 2023-06-13 14:58:25
Message-ID: CAMOxC8tvSx1Dduu5CvBTZ3eZdNMyE9=S2PSiypWKafVaF=-9nQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Jun 13, 2023 at 1:26 PM Thomas Kellerer <shammat(at)gmx(dot)net> wrote:

> 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
>

I know how to fix the problem and I know that ORDER BY should be in the
outermost select.

However, I want to write a test case that shows that the old code is wrong,
but can not create
minimal set of tables to reproduce it. With this I'm looking for help.

--
Best regards, Ruslan.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2023-06-13 15:06:53 Re: Reproducing incorrect order with order by in a subquery
Previous Message Christophe Pettus 2023-06-13 14:54:21 Re: SOC II Type 2 report.