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 384C49DCA10 for ; Wed, 11 Jan 2006 20:41:29 -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 28681-08 for ; Wed, 11 Jan 2006 20:41:32 -0400 (AST) Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id BFF229DCA14 for ; Wed, 11 Jan 2006 20:41:26 -0400 (AST) Received: from mail.goldpocket.com (mail1.goldpocket.com [38.101.116.14]) by svr4.postgresql.org (Postfix) with ESMTP id DB0D75AF0A9 for ; Thu, 12 Jan 2006 00:41:30 +0000 (GMT) Received: from localhost (unknown [127.0.0.1]) by mail.goldpocket.com (Postfix) with ESMTP id 82AC8E048F23 for ; Thu, 12 Jan 2006 00:41:28 +0000 (UTC) Received: from mail.goldpocket.com ([127.0.0.1]) by localhost (mail.goldpocket.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 05506-11 for ; Wed, 11 Jan 2006 16:41:24 -0800 (PST) Received: from mail1.goldpocket.com (srvgpimail1.gpi.local [10.10.0.13]) by mail.goldpocket.com (Postfix) with ESMTP id A5B74E048F1E for ; Wed, 11 Jan 2006 16:41:21 -0800 (PST) Received: from mliberman.gpi.local ([10.10.0.158]) by mail1.goldpocket.com with Microsoft SMTPSVC(6.0.3790.211); Wed, 11 Jan 2006 16:41:20 -0800 From: Mark Liberman Organization: Mixed Signals, Inc. To: pgsql-performance@postgresql.org Subject: Stable function being evaluated more than once in a single query Date: Wed, 11 Jan 2006 16:41:20 -0800 User-Agent: KMail/1.8.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200601111641.20627.mliberman@mixedsignals.com> X-OriginalArrivalTime: 12 Jan 2006 00:41:20.0781 (UTC) FILETIME=[E81DA3D0:01C61710] X-Virus-Scanned: amavisd-new at goldpocket.com X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.12 required=5 tests=[AWL=0.120] X-Spam-Score: 0.12 X-Spam-Level: X-Archive-Number: 200601/140 X-Sequence-Number: 16618 Hi, I've got a set-returning function, defined as STABLE, that I reference twice within a single query, yet appears to be evaluated via two seperate function scans. I created a simple query that calls the function below and joins the results to itself (Note: in case you wonder why I'd do such a query, it's not my actual query, which is much more complex. I just created this simple query to try to test out the 'stable' behavior). select proname,provolatile from pg_proc where proname = 'get_tran_filesize'; proname | provolatile ----------------------------+------------- get_tran_filesize | s (1 row) explain analyze select * from get_tran_filesize('2005-12-11 00:00:00-08','2006-01-11 15:58:33-08','{228226,228222,228210}'); QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------- Function Scan on get_tran_filesize (cost=0.00..12.50 rows=1000 width=40) (actual time=49.522..49.524 rows=3 loops=1) Total runtime: 49.550 ms (2 rows) explain analyze select * from get_tran_filesize('2005-12-11 00:00:00-08','2006-01-11 15:58:33-08','{228226,228222,228210}') gt, get_tran_filesize('2005-12-11 00:00:00-08','2006-01-11 15:58:33-08','{228226,228222,228210}') gt2 where gt.tran_id = gt2.tran_id; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------------------ Merge Join (cost=124.66..204.66 rows=5000 width=80) (actual time=83.027..83.040 rows=3 loops=1) Merge Cond: ("outer".tran_id = "inner".tran_id) -> Sort (cost=62.33..64.83 rows=1000 width=40) (actual time=40.250..40.251 rows=3 loops=1) Sort Key: gt.tran_id -> Function Scan on get_tran_filesize gt (cost=0.00..12.50 rows=1000 width=40) (actual time=40.237..40.237 rows=3 loops=1) -> Sort (cost=62.33..64.83 rows=1000 width=40) (actual time=42.765..42.767 rows=3 loops=1) Sort Key: gt2.tran_id -> Function Scan on get_tran_filesize gt2 (cost=0.00..12.50 rows=1000 width=40) (actual time=42.748..42.751 rows=3 loops=1) Total runtime: 83.112 ms (9 rows) If I do get this working, then my question is, if I reference this function within a single query, but within seperate subqueries within the query, will it be re-evaluated each time, or just once. Basically, I'm not clear on the definition of "surrounding query" in the following exerpt from the Postgreql documentation: 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. Thanks, Mark