Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iIEVQ-0004Ne-Hc for pgsql-performance@arkaria.postgresql.org; Wed, 09 Oct 2019 16:08:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iIEVO-0000B1-UN for pgsql-performance@arkaria.postgresql.org; Wed, 09 Oct 2019 16:08:02 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iIEVO-0000Au-Kp for pgsql-performance@lists.postgresql.org; Wed, 09 Oct 2019 16:08:02 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iIEVL-0003wt-Q5 for pgsql-performance@lists.postgresql.org; Wed, 09 Oct 2019 16:08:02 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x99G7lFs028167; Wed, 9 Oct 2019 12:07:47 -0400 From: Tom Lane To: Michael Lewis cc: Jeff Janes , =?UTF-8?Q?Yavuz_Selim_Serto=C4=9Flu_=28ETIYA=29?= , "pgsql-performance@lists.postgresql.org" Subject: Re: Query slows when used with view In-reply-to: References: <20080.1570629421@sss.pgh.pa.us> Comments: In-reply-to Michael Lewis message dated "Wed, 09 Oct 2019 09:32:37 -0600" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <28165.1570637267.1@sss.pgh.pa.us> Date: Wed, 09 Oct 2019 12:07:47 -0400 Message-ID: <28166.1570637267@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Michael Lewis writes: >> When you join to a view, the view sticks together, as if they were all in >> parentheses. But when you substitute the text of a view into another >> query, then they are all on the same level and can be parsed differently. >> >> Consider the difference between "1+1 * 3", and "(1+1) * 3" > I thought from_collapse_limit being high enough meant that it will get > re-written and inlined into the same level. To extend your metaphor, that > it would be 1 * 3 + 1 * 3. The point is that the semantics are actually different --- in Jeff's example, the answer is 4 vs. 6, and in the OP's query, the joins have different scopes. from_collapse_limit has to do with whether the planner can rewrite the query into a different form, but it's not allowed to change the semantics by doing so. In some cases you can re-order joins without changing the semantics, just as arithmetic has associative and commutative laws. But you can't always re-order outer joins like that. I didn't dig into the details of the OP's query too much, but I believe that the two forms of his join tree are semantically different, resulting in different runtimes. regards, tom lane