X-Original-To: pgsql-docs-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id DAFC19DD775 for ; Fri, 13 Jan 2006 20:56:25 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 34681-05 for ; Fri, 13 Jan 2006 20:56:27 -0400 (AST) 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 15B929DD756 for ; Fri, 13 Jan 2006 20:56:21 -0400 (AST) Received: by noel.decibel.org (Postfix, from userid 1001) id 5C7F83983D; Fri, 13 Jan 2006 18:56:24 -0600 (CST) Resent-From: jnasby@pervasive.com Resent-Date: Fri, 13 Jan 2006 18:56:24 -0600 Resent-Message-ID: <20060114005624.GV9017@pervasive.com> Resent-To: pgsql-docs@postgresql.org X-Original-To: decibel@decibel.org Received: from mx2.hub.org (mx2.hub.org [200.46.204.254]) by noel.decibel.org (Postfix) with ESMTP id DCBE439834 for ; Fri, 13 Jan 2006 18:44:24 -0600 (CST) Received: from postgresql.org (postgresql.org [200.46.204.71]) by mx2.hub.org (Postfix) with ESMTP id 2C6AE513079; Fri, 13 Jan 2006 20:44:19 -0400 (AST) X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 396D29DC838 for ; Fri, 13 Jan 2006 20:43:48 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 32213-04 for ; Fri, 13 Jan 2006 20:43:50 -0400 (AST) 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 B8ED09DC80C for ; Fri, 13 Jan 2006 20:43:44 -0400 (AST) Received: by noel.decibel.org (Postfix, from userid 1001) id 0BC5E3983D; Fri, 13 Jan 2006 18:43:48 -0600 (CST) Date: Fri, 13 Jan 2006 18:43:48 -0600 From: "Jim C. Nasby" To: Tom Lane Cc: Mark Liberman , pgsql-performance@postgresql.org Subject: Re: [PERFORM] Stable function being evaluated more than once in a single query Message-ID: <20060114004348.GT9017@pervasive.com> References: <200601111641.20627.mliberman@mixedsignals.com> <935.1137040403@sss.pgh.pa.us> <20060114000640.GN9017@pervasive.com> <16007.1137198448@sss.pgh.pa.us> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <16007.1137198448@sss.pgh.pa.us> X-Operating-System: FreeBSD 6.0-RELEASE amd64 X-Distributed: Join the Effort! http://www.distributed.net User-Agent: Mutt/1.5.11 X-Virus-Scanned: by amavisd-new at hub.org X-Mailing-List: pgsql-performance Precedence: bulk X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.09 required=5 tests=[AWL=0.090] X-Spam-Score: 0.09 X-Spam-Level: X-Archive-Number: 200601/22 X-Sequence-Number: 3412 Adding -docs... On Fri, Jan 13, 2006 at 07:27:28PM -0500, Tom Lane wrote: > "Jim C. Nasby" writes: > > Is the issue that the optimizer won't combine two function calls (ie: > > SELECT foo(..) ... WHERE foo(..)), or is it that sometimes it won't make > > the optimization (maybe depending on the query plan, for example)? > > What the STABLE category actually does is give the planner permission to > use the function within an indexscan qualification, eg, > WHERE indexed_column = f(42) > Since an indexscan involves evaluating the comparison expression just > once and using its value to search the index, this would be incorrect > if the expression's value might change from row to row. (For VOLATILE > functions, we assume that the correct behavior is the naive SQL > semantics of actually computing the WHERE clause at each candidate row.) > > There is no function cache and no checking for duplicate expressions. > I think we do check for duplicate aggregate expressions, but not > anything else. In that case I'd say that the sSTABLE section of 32.6 should be changed to read: A STABLE function cannot modify the database and is guaranteed to return the same results given the same arguments for all calls within a single surrounding query. This category gives the planner permission to use the function within an indexscan qualification. (Since an indexscan involves evaluating the comparison expression just once and using its value to search the index, this would be incorrect if the expression's value might change from row to row.) There is no function cache and no checking for duplicate expressions. I can provide a patch to that effect if it's easier... On a related note, would it be difficult to recognize multiple calls of the same function in one query? ISTM that would be a win for all but the most trivial of functions... -- Jim C. Nasby, Sr. Engineering Consultant jnasby@pervasive.com Pervasive Software http://pervasive.com work: 512-231-6117 vcard: http://jim.nasby.net/pervasive.vcf cell: 512-569-9461 ---------------------------(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