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 1jmHhk-0002j6-2j for pgsql-bugs@arkaria.postgresql.org; Fri, 19 Jun 2020 14:09: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 1jmHhi-0004Ho-Up for pgsql-bugs@arkaria.postgresql.org; Fri, 19 Jun 2020 14:09:14 +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 1jmHhi-0004Hf-Oo for pgsql-bugs@lists.postgresql.org; Fri, 19 Jun 2020 14:09:14 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jmHhc-0004s5-DV for pgsql-bugs@lists.postgresql.org; Fri, 19 Jun 2020 14:09: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 05JE96ov1647564; Fri, 19 Jun 2020 10:09:06 -0400 From: Tom Lane To: postgresql.20.drkshadow@spamgourmet.com cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #16504: Wrapping query in EXISTS() causes sequential scans of tables In-reply-to: <16504-d8c5d6beb38e8b6e@postgresql.org> References: <16504-d8c5d6beb38e8b6e@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Fri, 19 Jun 2020 03:31:29 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1647562.1592575746.1@sss.pgh.pa.us> Date: Fri, 19 Jun 2020 10:09:06 -0400 Message-ID: <1647563.1592575746@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk PG Bug reporting form writes: > In this case, I have a query that selects from a table, joins a table, joins > that same table, joins the first table, performs a NOT > firsttable.value=lasttable.value to make sure that I'm not joining an item > with itself. There are indexes for the conditions in each join. When I run > this as a select query, the EXPLAIN output uses index scans the whole way > through. When I SELECT EXISTS(thatquery) FROM firsttable WHERE firsttable.id > IN (1,2,3), the EXPLAIN output switches to using sequential scans. This > destroys performance on this 3.8m row table. Are you claiming that 9.6 did better? I'm not aware that we changed anything significant about how a sub-select in a query's select list works. The table/view definitions alone are not enough to investigate this, since it's unlikely that we'd get the same plans on an empty table as a populated one. Please see if you can make a self-contained test case with some dummy data that reproduces the problem (ie better plan on 9.6 than later). regards, tom lane