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 0CC5E329D55; Thu, 30 Sep 2004 15:13:38 +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 07285-07; Thu, 30 Sep 2004 14:13:25 +0000 (GMT) Received: from mail1.panix.com (mail1.panix.com [166.84.1.72]) by svr1.postgresql.org (Postfix) with ESMTP id CD01232A345; Thu, 30 Sep 2004 15:13:27 +0100 (BST) Received: from panix2.panix.com (panix2.panix.com [166.84.1.2]) by mail1.panix.com (Postfix) with ESMTP id 4334548719; Thu, 30 Sep 2004 10:13:24 -0400 (EDT) Received: (from adler@localhost) by panix2.panix.com (8.11.6p2-a/8.8.8/PanixN1.1) id i8UEDOL25410; Thu, 30 Sep 2004 10:13:24 -0400 (EDT) Date: Thu, 30 Sep 2004 10:13:24 -0400 From: Michael Adler To: Merlin Moncure Cc: pgsql-performance@postgresql.org, pgsql-hackers@postgresql.org Subject: Re: [HACKERS] spurious function execution in prepared statements. Message-ID: <20040930141324.GA9170@pobox.com> References: <6EE64EF3AB31D5448D0007DD34EEB3412A74DD@Herge.rcsinc.local> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <6EE64EF3AB31D5448D0007DD34EEB3412A74DD@Herge.rcsinc.local> User-Agent: Mutt/1.4.2.1i 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/336 X-Sequence-Number: 8464 On Thu, Sep 30, 2004 at 09:45:51AM -0400, Merlin Moncure wrote: > 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). Here's another workaround that may let you use a prepared statement: prepare ps(...) as select f(c) from (select c from t where [expr] limit 1) as t1 -Mike