Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dOKS0-0004r6-Qg for pgsql-bugs@arkaria.postgresql.org; Fri, 23 Jun 2017 09:00:25 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dOKS0-0005Ra-Cy for pgsql-bugs@arkaria.postgresql.org; Fri, 23 Jun 2017 09:00:24 +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_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dOKRI-0002wt-0F for pgsql-bugs@postgresql.org; Fri, 23 Jun 2017 08:59:40 +0000 Received: from know-smtprelay-omc-7.server.virginmedia.net ([80.0.253.71]) by magus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1dOKRD-0003Zf-7K for pgsql-bugs@postgresql.org; Fri, 23 Jun 2017 08:59:39 +0000 Received: from vm-cliveevans-d03.circle.byhiras.com ([195.110.85.254]) by know-smtprelay-7-imp with bizsmtp id c8zY1v00L5VEh2z018zZya; Fri, 23 Jun 2017 09:59:33 +0100 X-Originating-IP: [195.110.85.254] X-Authenticated-User: cliveevans@ntlworld.com X-Spam: 0 X-Authority: v=2.1 cv=Gd7q5ljL c=1 sm=1 tr=0 a=krHGFvvOGbZX40VCRH9Y2g==:117 a=krHGFvvOGbZX40VCRH9Y2g==:17 a=L9H7d07YOLsA:10 a=9cW_t1CCXrUA:10 a=s5jvgZ67dGcA:10 a=IkcTkHD0fZMA:10 a=x7bEGLp0ZPQA:10 a=NLZqzBF-AAAA:8 a=epTmVMiNAAAA:8 a=rG6vkLOJnqO_F2e6XKwA:9 a=Xl2RDhrHzDE8FJu-:21 a=QEXdDO2ut3YA:10 a=_W_S_7VecoQA:10 a=wW_WBVUImv98JQXhvVPZ:22 a=ndEWmUVY6Yapc0oHF_P4:22 Subject: Re: BUG #14715: Constraint exclusion isn't used in function using language sql To: pgsql-bugs@postgresql.org References: <20170621140056.27883.82221@wrigleys.postgresql.org> <20170623.103957.31224862.horiguchi.kyotaro@lab.ntt.co.jp> From: Clive Evans Message-ID: Date: Fri, 23 Jun 2017 08:59:32 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.1.0 MIME-Version: 1.0 In-Reply-To: <20170623.103957.31224862.horiguchi.kyotaro@lab.ntt.co.jp> Content-Type: text/html; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ntlworld.com; s=meg.feb2017; t=1498208373; bh=PjRzYnK/dlMrGMk3M/KjbgTF4QtPQwM3/T5Q9GuVe8w=; h=Subject:To:References:From:Date:In-Reply-To; b=hd8aYK9j9TwgTQlDwSCPpIkuX+AZ04duW/RJl1EA5kJZZfQNfDSkor0VR8eOPag5y SjMbGRRWgGUOle4V92T3pqYvFvc46wDVwe8WxZuc3fd5PwYNoM88kTyppzHzDtRuh4 V8Ev38MiEj4Cnd8iMsX0QBBcWvVpWuUegw55utJ7gDJYHlG0uRSEFa+1kkKNFwrABn j3yiXEZqotiakftEAZ6VtRkkayvW+6TxtOxfyis+fptU1zcCqhaBNh5Gi7k4EqfJ9A ucjrjK8svoC/HXDFIsq+dUP/I9aflS6KS2LK4TAmeZcCgHPk5D2MZCK52dQNr66rLj jbYsDctFqBDSA== List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org

On 23/06/17 01:39, Kyotaro HORIGUCHI wrote:
Hello,

At Wed, 21 Jun 2017 14:00:56 +0000, cliveevans@ntlworld.com wrote in <20170621140056.27883.82221@wrigleys.postgresql.org>
PostgreSQL version: 9.6.3
...
The same query written using PL/PGSQL will only scan the expected partition
tables.
The two are different in that the parameters of the PL/PgSQL
function are regarded as constants at the time of planning of the
inner SQL statement, while those of the SQL function are
not. Constraint exclusion is considered while planning so the SQL
function doesn't get benefit of it.

I understand this, and I understand why. You can't plan based on what you don't yet know. I'm not entirely clear why the inner query can treat the parameter as a constant, whereas the outer one is forced to treat it as dynamic.

I assume it's something to do with when the plan is created. Possibly it's an attempt to save the planning time and re-use the plan in the case of a 'normal' SQL function, although this seems like a trade off that may well have significant downsides - in this case obviously, but also others.

Perhaps I'm looking for a new feature, rather than reporting a bug. Something that allows lazy planning, where there's a good chance that treating the parameters as constants is likely to improve the produced plan sufficiently to offset the additional overhead of repeatedly planning.

thanks,
--
Clive Evans