Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1beOUL-0006Wr-6j for pgsql-bugs@arkaria.postgresql.org; Mon, 29 Aug 2016 15:28:41 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1beOUK-00012h-L1 for pgsql-bugs@arkaria.postgresql.org; Mon, 29 Aug 2016 15:28:40 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1beOUJ-00011z-PA for pgsql-bugs@postgresql.org; Mon, 29 Aug 2016 15:28:39 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1beOUH-0003E7-9L for pgsql-bugs@postgresql.org; Mon, 29 Aug 2016 15:28:39 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id u7TFSYH0007179; Mon, 29 Aug 2016 11:28:34 -0400 From: Tom Lane To: rikard@ngs.hr cc: pgsql-bugs@postgresql.org Subject: Re: BUG #14301: function in case expression called when it should not be In-reply-to: <20160829151433.15676.91530@wrigleys.postgresql.org> References: <20160829151433.15676.91530@wrigleys.postgresql.org> Comments: In-reply-to rikard@ngs.hr message dated "Mon, 29 Aug 2016 15:14:33 -0000" Date: Mon, 29 Aug 2016 11:28:34 -0400 Message-ID: <7178.1472484514@sss.pgh.pa.us> X-Pg-Spam-Score: -3.4 (---) 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 rikard@ngs.hr writes: > select case when doSlow then sum(slowFunction()) else sum(1) end as total > from something group by doSlow You are misunderstanding how aggregates work. The aggregates are evaluated first, then the surrounding expressions are done once at the end of the query (or group). The fact that the CASE might choose the other branch at the end doesn't eliminate the necessity to run both aggregates across all the rows. regards, tom lane -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs