Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fd0HN-0000ah-KJ for pgsql-hackers@arkaria.postgresql.org; Tue, 10 Jul 2018 21:34:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fd0HL-0006Zr-W4 for pgsql-hackers@arkaria.postgresql.org; Tue, 10 Jul 2018 21:34:35 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fd0HL-0006Zk-Ln for pgsql-hackers@lists.postgresql.org; Tue, 10 Jul 2018 21:34:35 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fd0HJ-0007E3-8z for pgsql-hackers@lists.postgresql.org; Tue, 10 Jul 2018 21:34:34 +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 w6ALYKBH032013; Tue, 10 Jul 2018 17:34:20 -0400 From: Tom Lane To: Heikki Linnakangas cc: Aleksandr Parfenov , Andrew Gierth , pgsql-hackers@lists.postgresql.org Subject: Re: Optimze usage of immutable functions as relation In-reply-to: <9f127876-2599-0485-c5ef-a5d8d7402c85@iki.fi> References: <402356c32eeb93d4fed01f66d6c7fe2d@postgrespro.ru> <87zi1g6jbj.fsf@news-spur.riddles.org.uk> <20180508125616.7ebb4d12@asp437-24-g082ur> <87po2679in.fsf@news-spur.riddles.org.uk> <20180516134746.0296a05f@asp437-g082ur> <9f127876-2599-0485-c5ef-a5d8d7402c85@iki.fi> Comments: In-reply-to Heikki Linnakangas message dated "Wed, 11 Jul 2018 00:25:28 +0300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <32011.1531258460.1@sss.pgh.pa.us> Date: Tue, 10 Jul 2018 17:34:20 -0400 Message-ID: <32012.1531258460@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Heikki Linnakangas writes: > But stepping back a bit, it's a bit weird that we're handling this > differently from VALUES and other subqueries. The planner knows how to > do this trick for simple subqueries: > postgres=# explain select * from tenk1, (select abs(100)) as a (a) where > unique1 < a; > QUERY PLAN > ----------------------------------------------------------- > Seq Scan on tenk1 (cost=0.00..483.00 rows=100 width=248) > Filter: (unique1 < 100) > (2 rows) > Note that it not only evaluated the function into a constant, but also > got rid of the join. For a function RTE, however, it can't do that: > postgres=# explain select * from tenk1, abs(100) as a (a) where unique1 < a; > QUERY PLAN > ------------------------------------------------------------------- > Nested Loop (cost=0.00..583.01 rows=3333 width=248) > Join Filter: (tenk1.unique1 < a.a) > -> Function Scan on a (cost=0.00..0.01 rows=1 width=4) > -> Seq Scan on tenk1 (cost=0.00..458.00 rows=10000 width=244) > (4 rows) > Could we handle this in pull_up_subqueries(), similar to the > RTE_SUBQUERY and RTE_VALUES cases? Perhaps. You could only do it for non-set-returning functions, which isn't the typical use of function RTEs, which is probably why we've not thought hard about it before. I'm not sure what would need to happen for lateral functions. Also to be considered, if it's not foldable to a constant, is whether we're risking evaluating it more times than before. regards, tom lane