Received: from localhost (wm.hub.org [200.46.204.128]) by postgresql.org (Postfix) with ESMTP id B2E5F9FA259 for ; Thu, 26 Oct 2006 16:20:19 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.128]) (amavisd-new, port 10024) with ESMTP id 05125-03 for ; Thu, 26 Oct 2006 19:19:23 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey- Received: from noel.decibel.org (noel.decibel.org [67.100.216.10]) by postgresql.org (Postfix) with ESMTP id 61D109F9F08 for ; Thu, 26 Oct 2006 16:19:23 -0300 (ADT) Received: by noel.decibel.org (Postfix, from userid 1001) id 5B8CF56427; Thu, 26 Oct 2006 14:19:22 -0500 (CDT) Received: (hashcash-sendmail, from uid 1001); Thu, 26 Oct 2006 14:19:18 -0500 Date: Thu, 26 Oct 2006 14:19:18 -0500 From: "Jim C. Nasby" To: Matthew Peters Cc: Tom Lane , pgsql-performance@postgresql.org Subject: Re: Stored procedure slower than sql? Message-ID: <20061026191918.GE26892@nasby.net> References: <21413.1161879278@sss.pgh.pa.us> <775F825B46B551499DC75DC4E92F80AB564D05@jupiter.ad.haydrian.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <775F825B46B551499DC75DC4E92F80AB564D05@jupiter.ad.haydrian.com> X-Operating-System: FreeBSD 6.1-RELEASE-p10 amd64 X-Distributed: Join the Effort! http://www.distributed.net User-Agent: Mutt/1.5.13 (2006-08-11) X-Hashcash: 1:20:061026:matthew@haydrian.com::ElUzocvF2n0U1XIl:00000000000000000 0000000000000000000000009MCl X-Hashcash: 1:20:061026:tgl@sss.pgh.pa.us::CUBNFrSWzflYeJAj:00000000000000000000 00000000000000000000000058Z6 X-Hashcash: 1:20:061026:pgsql-performance@postgresql.org::v6Yd3zwUXmB3mGtJ:00000 0000000000000000000000009iYR X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200610/562 X-Sequence-Number: 21691 The planner has no idea what $1 and $2 are when it plans the query, so that could easily explain why the performance is different. You can prepare statements in psql (at least in 8.1), which would be a good way to verify that theory (compare EXPLAIN for prepared vs. non). On Thu, Oct 26, 2006 at 09:21:37AM -0700, Matthew Peters wrote: > Parameterized. > > IE (very simplified) > > CREATE OR REPLACE FUNCTION my_function(IN param1 BIGINT, IN param2 > INTEGER) > RETURNS my_type > SECURITY DEFINER > AS > $$ > /* my_type = (a,b,c) */ > Select a,b,c > FROM my_table > WHERE indexed_column = $1 > AND partition_constraint_column = $2; > $$ > LANGUAGE SQL; > > > > > Matthew A. Peters > Sr. Software Engineer, Haydrian Corp. > matthew@haydrian.com > (mobile) 425-941-6566 > Haydrian Corp. > -----Original Message----- > From: Tom Lane [mailto:tgl@sss.pgh.pa.us] > Sent: Thursday, October 26, 2006 9:15 AM > To: Matthew Peters > Cc: pgsql-performance@postgresql.org > Subject: Re: [PERFORM] Stored procedure slower than sql? > Importance: High > > "Matthew Peters" writes: > > How can a stored procedure containing a single query not implement the > > same execution plan (assumption based on the dramatic performance > > difference) that an identical ad-hoc query generates? > > Parameterized vs non parameterized query? > > regards, tom lane > > ---------------------------(end of broadcast)--------------------------- > TIP 1: if posting/reading through Usenet, please send an appropriate > subscribe-nomail command to majordomo@postgresql.org so that your > message can get through to the mailing list cleanly > -- Jim Nasby jim@nasby.net EnterpriseDB http://enterprisedb.com 512.569.9461 (cell)