Re: BUG #4997: Expression evaluation rules - Mailing list pgsql-bugs

From Pavel Stehule
Subject Re: BUG #4997: Expression evaluation rules
Date
Msg-id 162867790908200242q3ba9fccfu8c6475b29821ded8@mail.gmail.com
Whole thread Raw
In response to BUG #4997: Expression evaluation rules  ("Dmitry Samokhin" <sdld@mail.ru>)
List pgsql-bugs
Hello

it's not bug, but feature.


>
> CREATE OR REPLACE FUNCTION trg_mytable_after()
> =C2=A0RETURNS trigger AS
> $BODY$
> BEGIN
> =C2=A0IF TG_OP =3D 'UPDATE' OR TG_OP =3D 'DELETE' THEN
> =C2=A0 =C2=A0IF (CASE WHEN TG_OP =3D 'DELETE' THEN TRUE ELSE OLD.a <> NEW=
.a END) THEN
> =C2=A0 =C2=A0 =C2=A0RAISE NOTICE 'OK!';
> =C2=A0 =C2=A0END IF;
> =C2=A0END IF;
>

CASE expression isn't evaluated internally in plpgsql, but it is
transformed to SQL statement, that is evaluated by SQL procession. You
code is some like:

DECLARE result boolean;
BEGIN
   IF TG_OP =3D 'UPDATE' OR TG_OP =3D 'DELETE' THEN
     EXECUTE 'CASE WHEN $1 =3D 'DELETE' THEN true ELSE $2.a <> $3.a END'
USING TG_OP, OLD.a, OLD.b INTO result;
    IF result THEN
      RAISE NOTICE 'OK!';

Regards
Pavel Stehule

pgsql-bugs by date:

Previous
From: Pavel Stehule
Date:
Subject: Re: BUG #4997: Expression evaluation rules
Next
From: "Dmitry Samokhin"
Date:
Subject: Re: BUG #4997: Expression evaluation rules