Re: plpgsql and control structures

Lists: pgsql-general
From: Nick Boutelier <nboutelier(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: plpgsql and control structures
Date: 2009-01-30 22:08:19
Message-ID: 7827ee3c-d51d-4858-abc1-eb4b36025aed@y23g2000pre.googlegroups.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

I noticed that if I use...

IF (TG_OP = 'DELETE' AND OLD.myColumn) THEN
-- Do something
END IF;

when an insert happens, I get an error that 'OLD' has not been
assigned. I would of thought that the control structure would just
ignored the rest of the 'if' statement if the first part (TG_OP) was
false.

Does this mean I have to branch out my control structures if I want
better speed? If the first part of the 'if' is rarely true, then I
might as well put the second control within the first.

-Nick


From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: Nick Boutelier <nboutelier(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: plpgsql and control structures
Date: 2009-02-02 22:00:08
Message-ID: 20090202220008.GC16385@svana.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Fri, Jan 30, 2009 at 02:08:19PM -0800, Nick Boutelier wrote:
> I noticed that if I use...
>
> IF (TG_OP = 'DELETE' AND OLD.myColumn) THEN
> -- Do something
> END IF;
>
> when an insert happens, I get an error that 'OLD' has not been
> assigned. I would of thought that the control structure would just
> ignored the rest of the 'if' statement if the first part (TG_OP) was
> false.

SQL is not C, AND and OR in if statements do not short-circuit.

> Does this mean I have to branch out my control structures if I want
> better speed? If the first part of the 'if' is rarely true, then I
> might as well put the second control within the first.

Nothing to do with speed, the entire expression needs to be parsable at
the point it is executed. So in your above example you need to nest two
if statements.

Have a nice day,
--
Martijn van Oosterhout <kleptog(at)svana(dot)org> http://svana.org/kleptog/
> Please line up in a tree and maintain the heap invariant while
> boarding. Thank you for flying nlogn airlines.