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 1gNjPJ-0001WP-TY for pgsql-hackers@arkaria.postgresql.org; Fri, 16 Nov 2018 19:03:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gNjPG-0003Rv-D5 for pgsql-hackers@arkaria.postgresql.org; Fri, 16 Nov 2018 19:03:54 +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 1gNjPG-0003PI-5j for pgsql-hackers@lists.postgresql.org; Fri, 16 Nov 2018 19:03:54 +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 1gNjPD-0007rT-0T for pgsql-hackers@lists.postgresql.org; Fri, 16 Nov 2018 19:03:53 +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 wAGJ3kR9002907; Fri, 16 Nov 2018 14:03:46 -0500 From: Tom Lane To: Aleksandr Parfenov cc: a.bykov@postgrespro.ru, pgsql-hackers@lists.postgresql.org Subject: Re: Optimze usage of immutable functions as relation In-reply-to: References: <402356c32eeb93d4fed01f66d6c7fe2d@postgrespro.ru> <20180906185109.661aaef0@asp437-ThinkPad-L380> <153994199621.10161.17815885396545049364.pgcf@coridan.postgresql.org> Comments: In-reply-to Aleksandr Parfenov message dated "Sat, 20 Oct 2018 01:31:04 +0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2905.1542395026.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Fri, 16 Nov 2018 14:03:46 -0500 Message-ID: <2906.1542395026@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Aleksandr Parfenov writes: > [ funcscan_plan_optimizer_v4.patch ] A small note here --- this would be noticeably cleaner if removal of the no-longer-needed function RTE were done using the dummy-relation infrastructure I proposed in https://commitfest.postgresql.org/20/1827/ Maybe we should get that done first and then come back to this. I'm a bit suspicious of the premise of this though, because it's going to result in a single call of a function being converted into possibly many calls, if the RTE is referenced in a lot of places. Even if the function is immutable so that those calls get evaluated at plan time not run time, it's still N calls not one, which'd be bad if the function is expensive. See e.g. https://www.postgresql.org/message-id/flat/CAOYf6edujEOGB-9fGG_V9PGQ5O9yoy= WmTnE9RyBYTGw%2BDq3SpA%40mail.gmail.com for an example where we're specifically telling people that they can put a function into FROM to avoid multiple evaluation. This patch breaks that guarantee. A possible fix for this is to do eval_const_expressions() on function RTE expressions at this stage (and then not need to do it later), and then pull up only when we find that the RTE expression has been reduced to a single Const. I'm not sure whether invoking eval_const_expressions() so early would create any issues. But if it can be made to work, this would eliminate quite a lot of the ad-hoc conditions that you have in the patch now. regards, tom lane