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 1v55tC-001FuB-R8 for pgsql-bugs@arkaria.postgresql.org; Sat, 04 Oct 2025 17:17:47 +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 1v55tB-000ZXC-1l for pgsql-bugs@arkaria.postgresql.org; Sat, 04 Oct 2025 17:17:45 +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 1v55tA-000ZX4-PR for pgsql-bugs@lists.postgresql.org; Sat, 04 Oct 2025 17:17:45 +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 1v55t8-0002AT-2S for pgsql-bugs@postgresql.org; Sat, 04 Oct 2025 17:17:44 +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 594HHbH41525406; Sat, 4 Oct 2025 13:17:37 -0400 From: Tom Lane To: David Rowley cc: Marko Tiikkaja , 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> Comments: In-reply-to David Rowley message dated "Sat, 04 Oct 2025 16:19:31 +1300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1525404.1759598257.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Sat, 04 Oct 2025 13:17:37 -0400 Message-ID: <1525405.1759598257@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk David Rowley writes: > On Sat, 4 Oct 2025 at 15:40, Marko Tiikkaja wrote: >> On Fri, Oct 3, 2025 at 16:31 Tom Lane wrote: >>> You haven't given us a lot to go on: no reproducible test case, >> I've provided two. Both make the planner look bad. You've provided *no* reproducible test case that makes the planner look bad. The filled-in test case has two possibilities that are both pretty cheap and the planner knows they are pretty cheap, so it hardly matters which one it takes. You showed us a fragment of a case where it chose a very expensive scan that it shouldn't have, but no useful information about how to reproduce that. > I expect what might be happening here is that ANALYZE runs when there > are no state =3D 'WAIT_EVENT' rows and thinks using the index on that > column is a better idea than the primary key index. Marko's fragment at [1] shows Index Scan using orders_wait_event_idx on orders (cost=3D0.54..94812.85 rows=3D85043 width=3D1223) (actual time=3D0.166..7199.020 rows=3D84535 loops=3D1) Buffers: shared hit=3D15676 read=3D91962 dirtied=3D1988 Planning: Buffers: shared hit=3D807 read=3D11 dirtied=3D1 Planning Time: 4.634 ms Execution Time: 7204.117 ms That's a pretty darn accurate rowcount estimate, so "out of date stats" doesn't seem to be the problem. I don't think it could possibly have believed that scanning the pkey index would fetch that many rows, or cost that much. So why didn't it pick that index? No way to tell from this amount of info, but I'm suspecting a moving part that we don't know about. (Hmmm ... why is there not a "Filter: (order_id =3D $1)" line here?) regards, tom lane [1] https://www.postgresql.org/message-id/CAL9smLB1szUHLMfpN19FKiCHRCs4Wvf= jqXbxKaCUjmDzEtT%3Dng%40mail.gmail.com