Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rxjLJ-00BKlI-5K for pgsql-general@arkaria.postgresql.org; Fri, 19 Apr 2024 08:11:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rxjLH-00GxEI-S2 for pgsql-general@arkaria.postgresql.org; Fri, 19 Apr 2024 08:11:31 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rxjLH-00GxDO-G5 for pgsql-general@lists.postgresql.org; Fri, 19 Apr 2024 08:11:31 +0000 Received: from smtp-relay-02-1.dondominio.net ([31.214.176.34]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rxjLB-003aQ7-CO for pgsql-general@lists.postgresql.org; Fri, 19 Apr 2024 08:11:30 +0000 Received: from localhost (smtp-relay-local.scip.local [127.0.0.1]) by smtp-relay.dondominio.com (Postfix) with SMTP id 18B5440610 for ; Fri, 19 Apr 2024 10:11:19 +0200 (CEST) Received: from mail-node.dondominio.com by smtp-relay.dondominio.com (Postfix) with ESMTP id 117A4405FE; Fri, 19 Apr 2024 10:11:18 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=negora.com; s=dddk; t=1713514278; bh=81Nivdk2v/f6kWyR/NtywpT7m2wV0XsCCDx78oludr4=; h=Message-ID:Date:Subject:To:From:Content-Type; b=CDwl/mpvuStnZtb5zTmh2JsEcKvg7Uu29aIc9iBIiQsT8cgZiNl7k4zCW3OC9ncOE K76RHmWn3fHUMCfBagB5b3dexA4OyfJfd+Y/naTS+3EVdj3aPgi71NXel3zsQxOlhY b00ZUeNB++MwmCjmiqQEfHR0jf98WlBKYSJn1/7w= Received: from [10.8.0.6] (167.red-83-55-205.dynamicip.rima-tde.net [83.55.205.167]) (Authenticated sender: public@negora.com) by mail-node.dondominio.com (Postfix) with ESMTPA id D7482405CD; Fri, 19 Apr 2024 10:11:17 +0200 (CEST) Message-ID: <4c474300-2662-4b4e-bad1-ca9d36324a2c@negora.com> Date: Fri, 19 Apr 2024 10:11:17 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Why does it sort rows after a nested loop that uses already-sorted indexes? To: Tom Lane Cc: PostgreSQL - General References: <07b89cad-b2a1-48c2-8a14-67d1b93098c9@negora.com> <1825378.1713452017@sss.pgh.pa.us> Content-Language: en-US, es-ES, en-GB From: negora In-Reply-To: <1825378.1713452017@sss.pgh.pa.us> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.6.2 (mail-node.dondominio.com [0.0.0.0]); Fri, 19 Apr 2024 10:11:18 +0200 (CEST) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > That's a level of analysis that it doesn't do... Great. I suspected that, but I needed a confirmation from a reliable source. Thank you! > ...and TBH I'm not even > entirely sure it's correct to assume that the output is sorted like > that. At minimum you'd need an additional assumption that the > outer side's join key is unique, which is a factor that we don't > currently track when reasoning about ordering. Ouch! I hadn't thought about that possibility! When I tried to mentally reproduce the nested loop, I always considered the values of the outer loop to be unique. I guess that was because, very often, I used unique indexes for my tests... But it doesn't have to be so, of course. Best regards. On 18/04/2024 16:53, Tom Lane wrote: > negora writes: >> As you can see, the planner does detect that the outer loop returns the >> rows presorted by [sales_order.id]. However, it's unable to detect that >> the rows returned by the inner loop are also sorted by [sales_order.id] >> first, and then by [order_line.id]. > > That's a level of analysis that it doesn't do, and TBH I'm not even > entirely sure it's correct to assume that the output is sorted like > that. At minimum you'd need an additional assumption that the > outer side's join key is unique, which is a factor that we don't > currently track when reasoning about ordering. > > regards, tom lane