Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1beTQX-0007mM-WA for pgsql-bugs@arkaria.postgresql.org; Mon, 29 Aug 2016 20:45:06 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1beTQX-0004DD-FU for pgsql-bugs@arkaria.postgresql.org; Mon, 29 Aug 2016 20:45:05 +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.84_2) (envelope-from ) id 1beTPt-0003BJ-5a for pgsql-bugs@postgresql.org; Mon, 29 Aug 2016 20:44:25 +0000 Received: from 173-164-140-181-sfba.hfc.comcastbusiness.net ([173.164.140.181] helo=fetter.org) by makus.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1beTPq-0002cR-6d for pgsql-bugs@postgresql.org; Mon, 29 Aug 2016 20:44:23 +0000 Received: by fetter.org (Postfix, from userid 1000) id DB8D5E05EC; Mon, 29 Aug 2016 13:44:19 -0700 (PDT) Date: Mon, 29 Aug 2016 13:44:19 -0700 From: David Fetter To: rikard@ngs.hr Cc: pgsql-bugs@postgresql.org Subject: Re: BUG #14301: function in case expression called when it should not be Message-ID: <20160829204419.GF29482@fetter.org> References: <20160829151433.15676.91530@wrigleys.postgresql.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20160829151433.15676.91530@wrigleys.postgresql.org> User-Agent: Mutt/1.7.0 (2016-08-17) X-Pg-Spam-Score: -0.9 (/) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org On Mon, Aug 29, 2016 at 03:14:33PM +0000, rikard@ngs.hr wrote: > The following bug has been logged on the website: > > Bug reference: 14301 > Logged by: Rikard Pavelic > Email address: rikard@ngs.hr > PostgreSQL version: 9.4.9 > Operating system: CentOS > Description: > > I was investigating some performance issues and stumbled upon this > behavior: > > create function slowFunction() returns int as $$ > begin > raise notice 'called'; > return 0; > end; > $$ language plpgsql volatile; > > create table something (i int, doSlow boolean); > insert into something values(1, false),(2,false); > > select case when doSlow then sum(slowFunction()) else sum(1) end as total > from something group by doSlow > > Postgres will correctly evaluate the last query, but it will also call the > plpgsql volatile function. > > Luckly for us, this was read only function, but Postgres will happily > execute DML commands inside it ;( This is the expected behavior. You might look into FILTER clauses in your queries, which might be closer to the behavior you want. Best, David. -- David Fetter http://fetter.org/ Phone: +1 415 235 3778 AIM: dfetter666 Yahoo!: dfetter Skype: davidfetter XMPP: david(dot)fetter(at)gmail(dot)com Remember to vote! Consider donating to Postgres: http://www.postgresql.org/about/donate -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs