Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 4EBE36339EB for ; Fri, 9 Apr 2010 10:52:01 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 30079-02 for ; Fri, 9 Apr 2010 13:51:50 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from marajade.localdomain (vcs5.camavision.com [63.228.164.252]) by mail.postgresql.org (Postfix) with ESMTP id B53106334F2 for ; Fri, 9 Apr 2010 10:51:50 -0300 (ADT) Received: from [192.168.10.84] (unknown [192.168.10.84]) (Authenticated sender: andy) by marajade.localdomain (Postfix) with ESMTPA id A4A5140C69; Fri, 9 Apr 2010 07:51:48 -0500 (CDT) Message-ID: <4BBF3100.4060707@squeakycode.net> Date: Fri, 09 Apr 2010 08:52:00 -0500 From: Andy Colson User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.9) Gecko/20100317 Lightning/1.0b1 Thunderbird/3.0.4 MIME-Version: 1.0 To: Sabin Coanda , pgsql-performance@postgresql.org Subject: Re: How check execution plan of a function References: <4BBDF9C9.9050504@squeakycode.net> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.74 tagged_above=-10 required=5 tests=BAYES_20=-0.74 X-Spam-Level: X-Archive-Number: 201004/113 X-Sequence-Number: 38363 On Fri Apr 9 2010 8:18 AM, Sabin Coanda wrote: > I have just a function returning a cursor based on a single coplex query. > When I check the execution plan of that query it takes about 3 seconds. Just > when it is used inside the function it freezes. > > This is the problem, and this is the reason I cannot imagine what is happen. > Also I tried to recreate the function as it was before when it run in 3 > seconds, but I cannot make it to run properly now. > > a query, like: "select stuff from aTable where akey = 5" can be planned/prepared differently than a function containing: "select stuff from aTable where akey = $1". I'm guessing this is the problem you are running into. The planner has no information about $1, so cannot make good guesses. I think you have two options: 1) dont use a function, just fire off the sql. 2) inside the function, create the query as a string, then execute it, like: a := "select junk from aTable where akey = 5"; EXECUE a; (I dont think that's the exact right syntax, but hopefully gets the idea across) -Andy