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 1nmOEn-0003uE-C7 for pgsql-performance@arkaria.postgresql.org; Wed, 04 May 2022 23:16:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nmODn-0005RB-TF for pgsql-performance@arkaria.postgresql.org; Wed, 04 May 2022 23:15:51 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nmODn-0005R1-E0 for pgsql-performance@lists.postgresql.org; Wed, 04 May 2022 23:15:51 +0000 Received: from mail.webkr.de ([78.47.173.34]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nmODk-0005ND-Mg for pgsql-performance@lists.postgresql.org; Wed, 04 May 2022 23:15:50 +0000 Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id C298B1A2314 for ; Thu, 5 May 2022 01:15:45 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=webkr.de; s=dkim; t=1651706146; h=from:subject:date:message-id:to:mime-version:content-type: content-transfer-encoding; bh=tsdsh/GdCLZoFX359MyChCd+DI3UQ2F4fd/qJiifrdE=; b=oSUnJpGqTqlfX0W+Bx6XfmQY0fPLAl53MduBvFJ9ZLvubIqacENs+sHxTJ9jQAipwwhlAN jBAhs/zFcS5x4/2rhgmRupLBKi1TP4nbz+qTkGmh6KCqCwsVE2QhuRsRfYnP99p4Qgycum Hv9Xx9ZIwO8DXlClZ9IiIJ47LHlN4e0= From: =?iso-8859-1?Q?Andr=E9_H=E4nsel?= To: Subject: Why is there a Sort after an Index Only Scan? Date: Thu, 5 May 2022 01:15:43 +0200 Message-ID: <014601d8600c$e15aadc0$a4100940$@webkr.de> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Thread-Index: AdhgDEQkr9NcbGG7TO+352BgFMIqEg== Content-Language: de X-Last-TLS-Session-Version: TLSv1 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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; Limit (cost=8.46..8.46 rows=1 width=4) (actual time=0.031..0.032 rows=0 loops=1) -> Sort (cost=8.46..8.46 rows=1 width=4) (actual time=0.025..0.025 rows=0 loops=1) Sort Key: order_id Sort Method: quicksort Memory: 25kB -> Index Only Scan using orders_test_shipping_date_order_id_idx on orders_test (cost=0.43..8.45 rows=1 width=4) (actual time=0.017..0.018 rows=0 loops=1) Index Cond: ((shipping_date >= '2022-05-01'::date) AND (shipping_date <= '2022-05-01'::date)) Heap Fetches: 0 Fiddle: https://dbfiddle.uk/?rdbms=postgres_14&fiddle=7a3bc2421b5de5a2a377bd39b78d1c d5 I am actually asking because when I skew the distribution a little and repeat the query I get a rather unfortunate plan: INSERT INTO orders_test SELECT generate_series(2000001, 2100000), '2022-05-01'; ANALYZE orders_test; Limit (cost=0.43..37.05 rows=50 width=4) (actual time=1186.565..1186.593 rows=50 loops=1) -> Index Scan using orders_test_pkey on orders_test (cost=0.43..74336.43 rows=101502 width=4) (actual time=1186.562..1186.584 rows=50 loops=1) Filter: ((shipping_date >= '2022-05-01'::date) AND (shipping_date <= '2022-05-01'::date)) Rows Removed by Filter: 2000000 Postgres here uses the primary key to get the sort order, so I'm wondering if there is anything about my index that precludes its use for ORDER BY.