X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 6F4BD9FA2D5 for ; Wed, 6 Sep 2006 20:15:08 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 90251-03 for ; Wed, 6 Sep 2006 20:15:01 -0300 (ADT) X-Greylist: delayed 00:09:49.711471 by SQLgrey- Received: from smtp02.lnh.mail.rcn.net (smtp02.lnh.mail.rcn.net [207.172.157.102]) by postgresql.org (Postfix) with ESMTP id 6DDED9FA1AD for ; Wed, 6 Sep 2006 20:15:01 -0300 (ADT) Received: from mr08.lnh.mail.rcn.net ([207.172.157.28]) by smtp02.lnh.mail.rcn.net with ESMTP; 06 Sep 2006 19:05:06 -0400 X-IronPort-AV: i="4.08,221,1154923200"; d="scan'208"; a="294191596:sNHT9676168196" Received: from smtp01.lnh.mail.rcn.net (smtp01.lnh.mail.rcn.net [207.172.4.11]) by mr08.lnh.mail.rcn.net (MOS 3.7.5a-GA) with ESMTP id GWV77739; Wed, 6 Sep 2006 19:04:45 -0400 (EDT) Received: from 146-115-123-113.c3-0.smr-ubr2.sbo-smr.ma.cable.rcn.com (HELO [192.168.0.2]) ([146.115.123.113]) by smtp01.lnh.mail.rcn.net with ESMTP; 06 Sep 2006 19:04:45 -0400 X-IronPort-AV: i="4.08,221,1154923200"; d="scan'208"; a="272254338:sNHT24035412" Message-ID: <44FF5404.2010604@oisinglynn.com> Date: Wed, 06 Sep 2006 19:04:36 -0400 From: Oisin Glynn Reply-To: me@oisinglynn.com User-Agent: Thunderbird 1.5.0.5 (Windows/20060719) MIME-Version: 1.0 To: Daniel CAUNE CC: pgsql-sql@postgresql.org Subject: Re: Evaluation of if conditions References: <001601c6d204$5d2cd840$0b00a8c0@tedy> In-Reply-To: <001601c6d204$5d2cd840$0b00a8c0@tedy> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Junkmail-Status: score=10/50, host=mr08.lnh.mail.rcn.net X-Junkmail-SD-Raw: score=unknown, refid=str=0001.0A090204.44FF52A3.00CB,ss=1,fgs=0, ip=207.172.4.11, so=2006-05-09 23:27:51, dmn=5.2.4/2006-05-04 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.1 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200609/40 X-Sequence-Number: 26251 Daniel CAUNE wrote: > Hi, > > How does the IF statement evaluate conditions? Does it evaluate conditions > following their declaration order from left to right? In case of > or-conditions, does the IF statement stop evaluating conditions whenever a > first or-condition is true? > > The following snippet seems to be invalid, which let me think that PL/PGSQL > evaluates all the conditions: > > IF (TG_OP = 'INSERT') OR > (OLD.bar = ...) THEN > statement > END IF; > > Should be rewritten as (for example): > > IF (TG_OP = 'INSERT') THEN > statement > ELSIF (OLD.bar = ...) THEN > statement > END IF; > > > Regards, > > -- > Daniel > > > ---------------------------(end of broadcast)--------------------------- > TIP 9: In versions below 8.0, the planner will ignore your desire to > choose an index scan if your joining column's datatypes do not > match > The following is working fine for me on 8.1.x on Windows. I am not sure what order it is evaluating the if statement in but it is working correctly. Oisin CREATE OR REPLACE FUNCTION zfunc_testor(bool, bool) RETURNS "varchar" AS $BODY$DECLARE v_1 boolean; v_2 boolean; BEGIN v_1 :=$1; v_2 := $2; if (v_1 = TRUE) OR (v_2 = TRUE) then return 'At least 1 true'; else return 'neither true'; end if; END;$BODY$ LANGUAGE 'plpgsql' VOLATILE; ALTER FUNCTION zfunc_testor(bool, bool) OWNER TO postgres;