Re: Why does it sort rows after a nested loop that uses already-sorted indexes?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: negora <public(at)negora(dot)com>
Cc: PostgreSQL - General <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: Why does it sort rows after a nested loop that uses already-sorted indexes?
Date: 2024-04-18 14:53:37
Message-ID: 1825378.1713452017@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

negora <public(at)negora(dot)com> 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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Michael Corey 2024-04-18 17:47:19 Foreign Key error between two partitioned tables
Previous Message negora 2024-04-18 11:00:38 Why does it sort rows after a nested loop that uses already-sorted indexes?