X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 4EA20D1B4F2 for ; Thu, 2 Oct 2003 15:31:32 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 17850-02 for ; Thu, 2 Oct 2003 12:30:45 -0300 (ADT) Received: from sss.pgh.pa.us (unknown [192.204.191.242]) by svr1.postgresql.org (Postfix) with ESMTP id 42C8BD1B4E1 for ; Thu, 2 Oct 2003 12:30:45 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.10/8.12.10) with ESMTP id h92FUVhn027543; Thu, 2 Oct 2003 11:30:31 -0400 (EDT) To: Andriy Tkachuk Cc: pgsql-performance@postgresql.org Subject: Re: runtime of the same query in function differs on 2 degree! In-reply-to: <20031002163005.N53420-100000@pool.imt.com.ua> References: <20031002163005.N53420-100000@pool.imt.com.ua> Comments: In-reply-to Andriy Tkachuk message dated "Thu, 02 Oct 2003 16:39:11 +0300" Date: Thu, 02 Oct 2003 11:30:31 -0400 Message-ID: <27542.1065108631@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200310/50 X-Sequence-Number: 3798 Andriy Tkachuk writes: > What's wrong with planner that executes my query in function?: > tele=# EXPLAIN analyze select sum(cost) from bills where (parent(user_id) = 6916799 or user_id = 6916799) and dat >= 1062363600 and dat < 10649555 > 99; In the function case, the planner will not have access to the specific values that "dat" is being compared to --- it'll see something like ... and dat >= $1 and dat < $2 In this case it has to fall back on a default estimate of how many rows will be selected, and I suspect it's guessing that a seqscan will be faster. The trouble is that for a sufficiently large range of d1/d2, a seqscan *will* be faster. You might find that the best solution is to use FOR ... EXECUTE and plug the parameters into the query string so that the planner can see their values. This will mean re-planning on every function call, but the advantage is the plan will adapt to the actual range of d1/d2. regards, tom lane