Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ezxm1-0008Ki-LA for pgsql-performance@arkaria.postgresql.org; Sun, 25 Mar 2018 05:00:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ezxm0-0008J6-B6 for pgsql-performance@arkaria.postgresql.org; Sun, 25 Mar 2018 05:00:52 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ezxm0-0008In-0L for pgsql-performance@lists.postgresql.org; Sun, 25 Mar 2018 05:00:52 +0000 Received: from mx2.mailbox.org ([80.241.60.215]) by makus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ezxlw-0006YC-RI for pgsql-performance@lists.postgresql.org; Sun, 25 Mar 2018 05:00:50 +0000 Received: from smtp2.mailbox.org (smtp2.mailbox.org [80.241.60.241]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx2.mailbox.org (Postfix) with ESMTPS id 0534140F26; Sun, 25 Mar 2018 07:00:45 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp2.mailbox.org ([80.241.60.241]) by hefe.heinlein-support.de (hefe.heinlein-support.de [91.198.250.172]) (amavisd-new, port 10030) with ESMTP id JNkMw2CA1ndo; Sun, 25 Mar 2018 07:00:44 +0200 (CEST) Message-ID: <1521954043.2350.15.camel@cybertec.at> Subject: Re: functions: VOLATILE performs better than STABLE From: Laurenz Albe To: Peter , pgsql-performance@lists.postgresql.org Date: Sun, 25 Mar 2018 07:00:43 +0200 In-Reply-To: <20180324012746.GA14631@gate.oper.dinoex.org> References: <20180324012746.GA14631@gate.oper.dinoex.org> Organization: Cybertec Content-Type: text/plain; charset="UTF-8" Mime-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Peter wrote: > Given an arbitrary function fn(x) returning numeric. > > Question: how often is the function executed? > [...] > C. > select v.v,v.v from (select fn('const') as v) as v; > > Answer: > Once if declared VOLATILE. > Twice if declared STABLE. > > Now this IS a surprize. It is clear that the system is not allowed to > execute the function twice when declared VOLATILE. It IS ALLOWED to > execute it twice when STABLE - but to what point, except prolonging > execution time? > > Over all, VOLATILE performs better than STABLE. The reason is that the subquery with the VOLATILE function can be flattened; see the EXPLAIN (VERBOSE) output. There is not guarantee that less volatility means better performance. Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com