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 1p0zLM-0001yD-Ku for pgsql-performance@arkaria.postgresql.org; Fri, 02 Dec 2022 06:16:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1p0zLL-0005KU-Eo for pgsql-performance@arkaria.postgresql.org; Fri, 02 Dec 2022 06:16:15 +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 1p0zLL-0005Jp-56 for pgsql-performance@lists.postgresql.org; Fri, 02 Dec 2022 06:16:15 +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 1p0zLI-0003uy-PK for pgsql-performance@lists.postgresql.org; Fri, 02 Dec 2022 06:16:14 +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 2B26GAHA1877072; Fri, 2 Dec 2022 01:16:10 -0500 From: Tom Lane To: Ronuk Raval cc: Justin Pryzby , Paul McGarry , pgsql-performance@lists.postgresql.org Subject: Re: Odd Choice of seq scan In-reply-to: References: <20221202012124.GA14156@telsasoft.com> Comments: In-reply-to Ronuk Raval message dated "Fri, 02 Dec 2022 00:37:50 -0500" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1877070.1669961770.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Fri, 02 Dec 2022 01:16:10 -0500 Message-ID: <1877071.1669961770@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Ronuk Raval writes: > We've been working around the problem by rewriting queries to use UNION = instead. Yeah, that. The real issue here is that the seqscan and indexscan plans both suck, because they will both run that sub-select for every row in the table. The index-only plan might fetch fewer blocks along the way, because it only has to read the index not the table proper ... but that's only true if the table's pages are mostly marked all-visible. (My bet about the plan instability is that the planner might choose differently depending on how much of the table it believes is all-visible.) That only helps a bit, though. What you really want to have happen, assuming there are not too many interesting orderid values, is to do a point indexscan for each one of them. Currently the planner won't think of that by itself when faced with OR'd conditions in WHERE. You have to help it along with UNION or some similar locution. regards, tom lane