Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WXZu8-0007dv-Jb for pgsql-sql@arkaria.postgresql.org; Tue, 08 Apr 2014 17:33:48 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WXZu8-0001Lb-3o for pgsql-sql@arkaria.postgresql.org; Tue, 08 Apr 2014 17:33:48 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WXZu7-0001LR-FV; Tue, 08 Apr 2014 17:33:47 +0000 Received: from mail.fmed.uba.ar ([157.92.152.1] helo=azteca.fmed.uba.ar) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WXZu3-00040y-24; Tue, 08 Apr 2014 17:33:47 +0000 Received: from localhost (localhost [127.0.0.1]) by azteca.fmed.uba.ar (Postfix) with ESMTP id 41FAC5CA05D; Tue, 8 Apr 2014 14:33:36 -0300 (ART) X-Virus-Scanned: amavisd-new at fmed.uba.ar Received: from azteca.fmed.uba.ar ([127.0.0.1]) by localhost (azteca.fmed.uba.ar [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id sM5t83Urxshx; Tue, 8 Apr 2014 14:33:31 -0300 (ART) Received: from azteca.fmed.uba.ar (azteca.fmed.uba.ar [157.92.152.1]) by azteca.fmed.uba.ar (Postfix) with ESMTP id 20E535CA045; Tue, 8 Apr 2014 14:33:28 -0300 (ART) Date: Tue, 8 Apr 2014 14:33:27 -0300 (ART) From: Gerardo Herzig To: Tom Lane Cc: pgsql-performance@postgresql.org, pgsql-sql Message-ID: <34678100.310095.1396978407846.JavaMail.root@fmed.uba.ar> In-Reply-To: <23396.1396965001@sss.pgh.pa.us> Subject: Re: [PERFORM] performance drop when function argument is evaluated in WHERE clause MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Originating-IP: [157.92.152.143] X-Mailer: Zimbra 7.2.0_GA_2669 (ZimbraWebClient - FF3.0 (Linux)/7.2.0_GA_2669) X-Pg-Spam-Score: -2.9 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org Tom, thanks (as allways) for your answer. This is a 9.1.12. I have to say, im not very happy about if-elif-else'ing at all. The "conditional filter" es a pretty common pattern in our functions, i would have to add (and maintain) a substantial amount of extra code. And i dont really understand why the optimizer issues, since the arguments are immutable "strings", and should (or could at least) be evaluated only once. Thanks again for your time! Gerardo ----- Mensaje original ----- > De: "Tom Lane" > Para: "Gerardo Herzig" > CC: pgsql-performance@postgresql.org, "pgsql-sql" > Enviados: Martes, 8 de Abril 2014 10:50:01 > Asunto: Re: [PERFORM] performance drop when function argument is evaluated in WHERE clause > > Gerardo Herzig writes: > > Hi all. I have a function that uses a "simple" select between 3 > > tables. There is a function argument to help choose how a WHERE > > clause applies. This is the code section: > > select * from.... > > [...] > > where case $3 > > when 'I' then [filter 1] > > when 'E' then [filter 2] > > when 'P' then [filter 3] > > else true end > > > When the function is called with, say, parameter $3 = 'I', the > > funcion run in 250ms, > > but when there is no case involved, and i call directly "with > > [filter 1]" the function runs in 70ms. > > > Looks like the CASE is doing something nasty. > > Any hints about this? > > Don't do it like that. You're preventing the optimizer from > understanding > which filter applies. Better to write three separate SQL commands > surrounded by an if/then/else construct. > > (BTW, what PG version is that? I would think recent versions would > realize that dynamically generating a plan each time would work > around > this. Of course, that approach isn't all that cheap either. You'd > probably still be better off splitting it up manually.) > > regards, tom lane > -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql