Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q95bu-0003n1-Pz for pgsql-general@arkaria.postgresql.org; Tue, 13 Jun 2023 15:07:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1q95bt-00084a-Iy for pgsql-general@arkaria.postgresql.org; Tue, 13 Jun 2023 15:07:05 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q95bt-00084I-8X for pgsql-general@lists.postgresql.org; Tue, 13 Jun 2023 15:07:05 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1q95bm-001zWg-Lg for pgsql-general@lists.postgresql.org; Tue, 13 Jun 2023 15:07:04 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 35DF6rmI1635593; Tue, 13 Jun 2023 11:06:53 -0400 From: Tom Lane To: Ruslan Zakirov cc: Thomas Kellerer , pgsql-general@lists.postgresql.org Subject: Re: Reproducing incorrect order with order by in a subquery In-reply-to: References: Comments: In-reply-to Ruslan Zakirov message dated "Tue, 13 Jun 2023 17:58:25 +0300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1635591.1686668813.1@sss.pgh.pa.us> Date: Tue, 13 Jun 2023 11:06:53 -0400 Message-ID: <1635592.1686668813@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Ruslan Zakirov writes: > 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. The ORDER BY in the sub-select will be honored at the output of the sub-select. To have a different ordering at the final output, you need the upper query to do something that would re-order the rows. Joining the sub-select to something else might make that happen, or you could apply DISTINCT or some other non-trivial processing in the upper query. regards, tom lane