X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 469E732A0FA; Thu, 30 Sep 2004 15:45:57 +0100 (BST) 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 21777-08; Thu, 30 Sep 2004 14:45:52 +0000 (GMT) Received: from megazone.bigpanda.com (megazone.bigpanda.com [64.147.171.210]) by svr1.postgresql.org (Postfix) with ESMTP id BE05F32A09B; Thu, 30 Sep 2004 15:45:55 +0100 (BST) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id D7DCD3510F; Thu, 30 Sep 2004 07:45:40 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id D5E8E3510A; Thu, 30 Sep 2004 07:45:40 -0700 (PDT) Date: Thu, 30 Sep 2004 07:45:40 -0700 (PDT) From: Stephan Szabo To: Merlin Moncure Cc: pgsql-performance@postgresql.org, pgsql-hackers@postgresql.org Subject: Re: spurious function execution in prepared statements. In-Reply-To: <6EE64EF3AB31D5448D0007DD34EEB3412A74DD@Herge.rcsinc.local> Message-ID: <20040930074054.T8163@megazone.bigpanda.com> References: <6EE64EF3AB31D5448D0007DD34EEB3412A74DD@Herge.rcsinc.local> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200409/338 X-Sequence-Number: 8466 On Thu, 30 Sep 2004, Merlin Moncure wrote: > OK, I have a situation that might be a performance problem, a bug, or an > unavoidable consequence of using prepared statements. The short version > is that I am getting function executions for rows not returned in a > result set when they are in a prepared statement. > > In other words, I have a query: > select f(t.c) from t where [boolean expr on t] limit 1; An actual boolean expr on t? Or on a column in t? > because of the limit phrase, obviously, at most one record is returned > and f executes at most once regardless of the plan used (in practice, > sometimes index, sometimes seq_scan. > > Now, if the same query is executed as a prepared statement, > prepare ps(...) as select f(t.c) from t where [expr] limit 1; > execute ps; > > now, if ps ends up using a index scan on t, everything is ok. However, > if ps does a seqscan, f executes for every row on t examined until the > [expr] criteria is met. Is this a bug? If necessary I should be able > to set up a reproducible example. The easy workaround is to not use > prepared statements in these situations, but I need to be able to > guarantee that f only executes once (even if that means exploring > subqueries). I think a reproducible example would be good. Simple attempts to duplicate this on 8.0b2 have failed for me, unless I'm using order by.