Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VBrzJ-0000Bx-Kp for pgsql-performance@arkaria.postgresql.org; Tue, 20 Aug 2013 19:53:09 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VBrzJ-0008D5-45 for pgsql-performance@arkaria.postgresql.org; Tue, 20 Aug 2013 19:53:09 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VBrzG-0008Cw-UG for pgsql-performance@postgresql.org; Tue, 20 Aug 2013 19:53:06 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VBrzE-0005nC-0c for pgsql-performance@postgresql.org; Tue, 20 Aug 2013 19:53:06 +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 r7KJqqJn012173; Tue, 20 Aug 2013 15:52:52 -0400 From: Tom Lane To: David McNett cc: pgsql-performance@postgresql.org Subject: Re: Can query planner prefer a JOIN over a high-cost Function? In-reply-to: <20130820180651.GA72756@macnugget.org> References: <20130820180651.GA72756@macnugget.org> Comments: In-reply-to David McNett message dated "Tue, 20 Aug 2013 13:06:51 -0500" Date: Tue, 20 Aug 2013 15:52:52 -0400 Message-ID: <12172.1377028372@sss.pgh.pa.us> X-Pg-Spam-Score: -4.7 (----) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org David McNett writes: > Is there any way for me to influence the query planner so that it can > know that the JOIN is far less expensive than the function for planning? > The COST attribute on the function appears to have no effect. I think what you're missing is an index on examples.code_id, which would allow for a plan like this one: Nested Loop (cost=154.41..205263.18 rows=2185 width=16) -> Seq Scan on codes c (cost=0.00..1.06 rows=1 width=8) Filter: ((code)::text = 'three'::text) -> Bitmap Heap Scan on examples e (cost=154.41..205234.81 rows=2731 width=1 2) Recheck Cond: (code_id = c.code_id) Filter: (painfully_slow_function(example_id, value) IS TRUE) -> Bitmap Index Scan on examples_code_id_idx (cost=0.00..153.73 rows= 8192 width=0) Index Cond: (code_id = c.code_id) If you really want to force the join to occur separately, you could probably do something involving a sub-select with OFFSET 0, but I wouldn't recommend pursuing that path unless you can't get a decent result without contorting the query. Another thing worth thinking about is whether you could precalculate the expensive function via a functional index. It'd have to be immutable, but if it is, this is a useful way of changing the ground rules. regards, tom lane -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance