X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id ACF4FD77D9 for ; Tue, 8 Nov 2005 00:12:12 -0400 (AST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 10321-06 for ; Tue, 8 Nov 2005 04:12:10 +0000 (GMT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id EBC42D7123 for ; Tue, 8 Nov 2005 00:12:09 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.1/8.13.1) with ESMTP id jA84C7jL001159; Mon, 7 Nov 2005 23:12:07 -0500 (EST) To: "Craig A. James" Cc: pgsql-performance@postgresql.org Subject: Re: Expensive function and the optimizer In-reply-to: <4370062B.4050202@modgraph-usa.com> References: <4370062B.4050202@modgraph-usa.com> Comments: In-reply-to "Craig A. James" message dated "Mon, 07 Nov 2005 17:58:03 -0800" Date: Mon, 07 Nov 2005 23:12:07 -0500 Message-ID: <1158.1131423127@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.006 required=5 tests=[AWL=0.006] X-Spam-Score: 0.006 X-Spam-Level: X-Archive-Number: 200511/136 X-Sequence-Number: 15393 "Craig A. James" writes: > Is there some way to explain this cost to the optimizer in a permanent > way, Nope, sorry. One thing you could do in the particular case at hand is to rejigger the WHERE clause involving the function so that it requires values from both tables and therefore can't be applied till after the join is made. (If nothing else, give the function an extra dummy argument that can be passed as a variable from the other table.) This is an ugly and non-general solution of course. > The only way I've thought of is something like this: > select X.id from > (select A.id, A.foo, A.row_num > from A join B ON (A.id = B.id) > where A.row_num >= 0 and A.row_num <= 43477 > and B.id = 52) as X > where myfunc(X.foo, 'FooBar') order by X.row_num; As written, that won't work because the planner will happily flatten the query to the same thing you had before. You can put an OFFSET 0 into the sub-select to prevent that from happening, but realize that this creates a pretty impervious optimization fence ... the side-effects might be undesirable when you come to look at real queries instead of toy cases. regards, tom lane