Re: Why is there a Sort after an Index Only Scan?

From: Jeff Janes <jeff(dot)janes(at)gmail(dot)com>
To: André Hänsel <andre(at)webkr(dot)de>
Cc: Pgsql Performance <pgsql-performance(at)lists(dot)postgresql(dot)org>
Subject: Re: Why is there a Sort after an Index Only Scan?
Date: 2022-05-04 23:37:08
Message-ID: CAMkU=1yjuKjFKsZ3h2yuwGHCZxm==yp6C2gim-tXh6q3xK51Lg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

On Wed, May 4, 2022 at 7:15 PM André Hänsel <andre(at)webkr(dot)de> wrote:

> Quick(?) question... why is there a Sort node after an Index Only Scan?
> Shouldn't the index already spit out sorted tuples?
>
> CREATE INDEX ON orders_test(shipping_date, order_id);
>
> EXPLAIN ANALYZE SELECT
> FROM orders_test
> WHERE TRUE
> AND shipping_date >= '2022-05-01'
> AND shipping_date <= '2022-05-01'
> ORDER BY order_id
> LIMIT 50;
>

They are sorted by order_id only within sets of the same shipping_date,
which is not good enough. (It would be good enough if it were smart enough
to know that there is only one possible shipping date to satisfy your weird
range condition.)

Cheers,

Jeff

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message David Rowley 2022-05-04 23:42:08 Re: Why is there a Sort after an Index Only Scan?
Previous Message André Hänsel 2022-05-04 23:15:43 Why is there a Sort after an Index Only Scan?