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 1v5TAm-007IMn-ND for pgsql-bugs@arkaria.postgresql.org; Sun, 05 Oct 2025 18:09:29 +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 1v5TAj-003GO6-2B for pgsql-bugs@arkaria.postgresql.org; Sun, 05 Oct 2025 18:09:25 +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 1v5TAi-003GNy-Od for pgsql-bugs@lists.postgresql.org; Sun, 05 Oct 2025 18:09:25 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1v5TAg-000Bet-1O for pgsql-bugs@postgresql.org; Sun, 05 Oct 2025 18:09:24 +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 595I9J3u1704046; Sun, 5 Oct 2025 14:09:19 -0400 From: Tom Lane To: Marko Tiikkaja cc: David Rowley , pgsql-bugs@postgresql.org Subject: Re: [BUGS] BUG #11500: PRIMARY KEY index not being used In-reply-to: References: <20140926080211.7639.68358@wrigleys.postgresql.org> <8043.1411753479@sss.pgh.pa.us> <1314567.1759501872@sss.pgh.pa.us> <1525405.1759598257@sss.pgh.pa.us> Comments: In-reply-to Marko Tiikkaja message dated "Sun, 05 Oct 2025 09:38:14 +0300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1704044.1759687759.1@sss.pgh.pa.us> Date: Sun, 05 Oct 2025 14:09:19 -0400 Message-ID: <1704045.1759687759@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Marko Tiikkaja writes: > I can't believe I actually have to do this, but run: That's not what was asked for. Your original report showed a case where the planner chose orders_wait_event_idx despite estimating that it would have to scan 80K-plus rows. That's the case where I don't understand how it could think that the primary key index would be even more expensive. I'm not particularly concerned about the case you show here, because if you run the EXPLAIN a second time it gets a lot cheaper. I believe the reason is that the first time is visiting a bunch of just-deleted rows and so it has to stop and update their hint bits, both in the heap and the index. That is not a cost that the planner can reasonably predict in advance, and even if we could it's not really fair to blame it on the choice of index. The pkey alternative only visits one row and therefore only updates one hint bit, but we'd have to update the rest sooner or later. So IMO the speed difference is largely illusory because it arises from deferring maintenance. You can demonstrate this by changing the "ANALYZE orders" in the test case to "VACUUM ANALYZE orders": the EXPLAIN still chooses orders_wait_event_idx, but now it's fast because all that work got done by VACUUM. regards, tom lane