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 1m0pF5-0002VK-Bu for pgsql-hackers@arkaria.postgresql.org; Tue, 06 Jul 2021 17:52:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m0pF4-00049T-2N for pgsql-hackers@arkaria.postgresql.org; Tue, 06 Jul 2021 17:52:18 +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 1m0pF3-00049L-Q3 for pgsql-hackers@lists.postgresql.org; Tue, 06 Jul 2021 17:52:17 +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 1m0pF0-0002JB-Pk for pgsql-hackers@lists.postgresql.org; Tue, 06 Jul 2021 17:52:16 +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 166HqABg1716812; Tue, 6 Jul 2021 13:52:10 -0400 From: Tom Lane To: Dean Rasheed cc: Joel Jacobson , PostgreSQL Hackers Subject: Re: Planning time grows exponentially with levels of nested views In-reply-to: References: <797aff54-b49b-4914-9ff9-aa42564a4d7d@www.fastmail.com> <140747.1618776847@sss.pgh.pa.us> <141461.1618778513@sss.pgh.pa.us> Comments: In-reply-to Dean Rasheed message dated "Tue, 06 Jul 2021 18:32:04 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1716810.1625593930.1@sss.pgh.pa.us> Date: Tue, 06 Jul 2021 13:52:10 -0400 Message-ID: <1716811.1625593930@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Dean Rasheed writes: > I took a look at this and wasn't able to find any way to break it, and > your argument that it can't really make such rewriter bugs any worse > makes sense. Thanks for looking! > Would it make sense to update the comment prior to copying the subquery? Yeah, I hadn't touched that yet because the question was exactly about whether it's correct or not. I think we can shorten it to * Need a modifiable copy of the subquery to hack on, so that the * RTE can be left unchanged in case we decide below that we can't * pull it up after all. > Out of curiosity, I also tested DML against these deeply nested views > to see how the pull-up code in the rewriter compares in terms of > performance, since it does a very similar job. As expected, it's > O(N^2) as well, but it's about an order of magnitude faster: Oh good. I hadn't thought to look at that angle of things. > ... for a one-line change, that's pretty impressive. Yeah. The problem might get less bad if we get anywhere with the idea I suggested at [1]. If we can reduce the number of RTEs in a view's query, then copying it would get cheaper. Still, not copying it at all is always going to be better. I'll go ahead and push the patch. regards, tom lane [1] https://www.postgresql.org/message-id/697679.1625154303%40sss.pgh.pa.us