X-Original-To: pgsql-bugs-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 9EB53529F6 for ; Fri, 29 Jul 2005 11:00:43 -0300 (ADT) 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 48716-05 for ; Fri, 29 Jul 2005 14:00:40 +0000 (GMT) Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by svr1.postgresql.org (Postfix) with ESMTP id D3935529F3 for ; Fri, 29 Jul 2005 11:00:38 -0300 (ADT) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.3/8.13.3) with ESMTP id j6TE0dhM029609 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 29 Jul 2005 08:00:42 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.3/8.13.3) with ESMTP id j6TE0c4k083725; Fri, 29 Jul 2005 08:00:38 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.3/8.13.3/Submit) id j6TE0cNO083724; Fri, 29 Jul 2005 08:00:38 -0600 (MDT) (envelope-from mfuhr) Date: Fri, 29 Jul 2005 08:00:38 -0600 From: Michael Fuhr To: Magno Leite Cc: pgsql-bugs@postgresql.org Subject: Re: BUG #1797: Problem using Limit in a function, seqscan Message-ID: <20050729140038.GA83653@winnie.fuhr.org> References: <20050729125245.1166CF0B14@svr2.postgresql.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20050729125245.1166CF0B14@svr2.postgresql.org> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.005 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200507/249 X-Sequence-Number: 12412 On Fri, Jul 29, 2005 at 01:52:45PM +0100, Magno Leite wrote: > I looked for about this problem in BUG REPORT but I can't find. This is my > problem, when I try to use limit in a function, the Postgre doesn't use my > index, then it use sequencial scan. What is the problem ? Without more information we can only guess, but if you're using PL/pgSQL then a cached query plan might be responsible. Here's an excerpt from the PREPARE documentation: In some situations, the query plan produced for a prepared statement will be inferior to the query plan that would have been chosen if the statement had been submitted and executed normally. This is because when the statement is planned and the planner attempts to determine the optimal query plan, the actual values of any parameters specified in the statement are unavailable. PostgreSQL collects statistics on the distribution of data in the table, and can use constant values in a statement to make guesses about the likely result of executing the statement. Since this data is unavailable when planning prepared statements with parameters, the chosen plan may be suboptimal. If you'd like us to take a closer look, then please post a self- contained example, i.e., all SQL statements that somebody could load into an empty database to reproduce the behavior you're seeing. -- Michael Fuhr http://www.fuhr.org/~mfuhr/