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 1nmQwP-0003Yt-PP for pgsql-performance@arkaria.postgresql.org; Thu, 05 May 2022 02:10:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nmQvQ-0003H1-5l for pgsql-performance@arkaria.postgresql.org; Thu, 05 May 2022 02:09:04 +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 1nmQvP-0003Gs-Sk for pgsql-performance@lists.postgresql.org; Thu, 05 May 2022 02:09:03 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nmQvN-00025o-2a for pgsql-performance@lists.postgresql.org; Thu, 05 May 2022 02:09:02 +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 24528x4p2604687; Wed, 4 May 2022 22:08:59 -0400 From: Tom Lane To: =?UTF-8?Q?Andr=C3=A9_H=C3=A4nsel?= cc: "'Pgsql Performance'" Subject: Re: Why is there a Sort after an Index Only Scan? In-reply-to: <015101d8601d$37df5190$a79df4b0$@webkr.de> References: <014601d8600c$e15aadc0$a4100940$@webkr.de> <015101d8601d$37df5190$a79df4b0$@webkr.de> Comments: In-reply-to =?UTF-8?Q?Andr=C3=A9_H=C3=A4nsel?= message dated "Thu, 05 May 2022 03:12:40 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2604685.1651716539.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 04 May 2022 22:08:59 -0400 Message-ID: <2604686.1651716539@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk =3D?UTF-8?Q?Andr=3DC3=3DA9_H=3DC3=3DA4nsel?=3D writes: > Limit (cost=3D0.43..296.63 rows=3D50 width=3D4) (actual time=3D1052.692= ..1052.737 rows=3D50 loops=3D1) > -> Index Scan using orders_test_pkey on orders_test (cost=3D0.43..71= 149.43 rows=3D12010 width=3D4) (actual time=3D1052.690..1052.728 rows=3D50= loops=3D1) > Filter: ((shipping_date >=3D '2022-04-30'::date) AND (shipping_d= ate <=3D '2022-05-01'::date)) > Rows Removed by Filter: 1998734 > Is it overestimating the cost of the sorting? No, but it's guessing it will hit 50 rows that satisfy the filter before it's gone very far in this index. If the shipping date and pkey are correlated in the wrong direction, that could be a very overoptimistic guess. I don't think we have adequate stats yet to detect this sort of problem. regards, tom lane