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 1v4gob-004YhS-HN for pgsql-bugs@arkaria.postgresql.org; Fri, 03 Oct 2025 14:31:21 +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 1v4goZ-00DoZJ-8J for pgsql-bugs@arkaria.postgresql.org; Fri, 03 Oct 2025 14:31:19 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1v4goY-00DoYO-VV for pgsql-bugs@lists.postgresql.org; Fri, 03 Oct 2025 14:31:19 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1v4goX-000DCI-1J for pgsql-bugs@postgresql.org; Fri, 03 Oct 2025 14:31:19 +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 593EVCsB1314568; Fri, 3 Oct 2025 10:31:13 -0400 From: Tom Lane To: Marko Tiikkaja cc: 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> Comments: In-reply-to Marko Tiikkaja message dated "Fri, 03 Oct 2025 09:23:20 +0300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1314566.1759501872.1@sss.pgh.pa.us> Date: Fri, 03 Oct 2025 10:31:12 -0400 Message-ID: <1314567.1759501872@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Marko Tiikkaja writes: > ... we have a frequent query like this: > SELECT .. > FROM orders > WHERE > order_id = $1 AND > state = 'WAIT_EVENT'; > which almost always uses the primary key. But sometimes, perhaps > after an autovacuum or something, something changes and postgres > decides to start serving that query through the orders_wait_event_idx > index. You haven't given us a lot to go on: no reproducible test case, no clear description of what triggers the issue, not even the complete EXPLAIN output for the problem query. But it's hard to believe that the planner would estimate a probe on a unique index as costing more than 94812.85 units, which is what this fragment seems to suggest. If you look at eqsel() you will observe that the presence of a unique index overrides any information from statistics, so there is no "casino" behavior here IMO: it should realize that "order_id = $1" selects a single row no matter what. I'm wondering if it didn't use that index because it *couldn't* use that index, for who-knows-what reason. There are operations that will leave an index marked as invalid and hence unsafe to query on. Anyway, -EINSUFFICIENTINFORMATION. regards, tom lane