X-Original-To: pgsql-performance@postgresql.org Received: from spampd.localdomain (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 21055476368 for ; Thu, 24 Apr 2003 11:57:13 -0400 (EDT) Received: from megazone.bigpanda.com (megazone.bigpanda.com [63.150.15.178]) by postgresql.org (Postfix) with ESMTP id E67A9476361 for ; Thu, 24 Apr 2003 11:57:08 -0400 (EDT) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id D0753D618; Thu, 24 Apr 2003 08:57:13 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id C5EE15C0A; Thu, 24 Apr 2003 08:57:13 -0700 (PDT) Date: Thu, 24 Apr 2003 08:57:13 -0700 (PDT) From: Stephan Szabo To: Frederic Jolliton Cc: Subject: Re: Important speed difference between a query and a In-Reply-To: <86el3rsxqu.fsf@mau.localdomain> Message-ID: <20030424085451.P1362-100000@megazone23.bigpanda.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Spam-Status: No, hits=-26.6 required=5.0 tests=BAYES_01,EMAIL_ATTRIBUTION,IN_REP_TO,QUOTED_EMAIL_TEXT, QUOTE_TWICE_1,REPLY_WITH_QUOTES autolearn=ham version=2.50 X-Spam-Level: X-Spam-Checker-Version: SpamAssassin 2.50 (1.173-2003-02-20-exp) X-Archive-Number: 200304/198 X-Sequence-Number: 1704 On Thu, 24 Apr 2003, Frederic Jolliton wrote: > > (PostgreSQL 7.3.2 on i386-portbld-freebsd4.7, compiled by GCC 2.95.4) > > > > I've a curious performance problem with a function returning set of > > rows. The query alone is very fast, but not when called from the > > function. > > > > To "emulate" a parametred view, I created a function as follow: > > > > CREATE FUNCTION get_info (integer) RETURNS SETOF type_get_info > > AS '...' <- here the query show below, where 'LIMIT $1' is used instead of 'LIMIT 10' > > LANGUAGE sql; > > Setting enable_seqscan to off give same result speed between the query > and the function ! > > So, the query in the function is not using index but the exact same > query alone does ! > > Is there an explanation ? My guess is that limit $1 is assuming a larger number of rows when planning the queries, large enough that it expects seqscan to be better (assuming the limit is what it expects). It's probably not going to plan that query each time the function is called so it's not going to know whether you're calling with a small number (index scan may be better) or a large number (seq scan may be better). For example, if you sent 100000, the index scan might be a loser. Perhaps plpgsql with EXECUTE would work better for that, although it's likely to have some general overhead.