Re: Multiple-action rule surprise - Mailing list pgsql-bugs

From Jaime Casanova
Subject Re: Multiple-action rule surprise
Date
Msg-id c2d9e70e050804123513a0f538@mail.gmail.com
Whole thread Raw
In response to Multiple-action rule surprise  ("Eugene Shekhtman" <postgre@xenomics.com>)
List pgsql-bugs
> -- Now I add to the "upd_if" rule
> -- a condition that checks if a similar record already exists
> CREATE OR REPLACE RULE upd_if AS ON UPDATE TO test.view1
>        WHERE ((SELECT count(*) AS count1 FROM test.table1 WHERE table1.da=
ta1 =3D
> new.data1)) =3D 0
>        DO (
>                UPDATE test.table1 SET data1 =3D new.data1 WHERE table1.id=
1 =3D old.id1;
>                UPDATE test.table1 SET flag1 =3D true WHERE table1.id1 =3D=
 old.id1;
>        );
>=20
> -- Start with fresh data
> DELETE FROM test.table1;
> INSERT INTO test.table1 (id1, data1) VALUES (1, 'foo');
> SELECT * FROM test.view1;
> --   id1   data1   flag1
> --  ----------------------
> --    1     foo    FALSE
> --
>=20
> UPDATE test.view1 SET data1 =3D 'bar' WHERE view1.id1 =3D 1;
> SELECT * FROM test.view1;
> --   id1   data1   flag1
> --  ----------------------
> --    1     bar    FALSE
> --
> -- Only the first of the 2 commands in the "upd_if" rule was executed!
> -- The second command is silently ignored.
> --
> </SQL>
>=20
> I haven't found anything in the doc or in the list archives explicitly
> addressing this point. Is this a bug or a feature?
>=20

Actually the rule is executing as expecting without executing at all
when if founds no rows matching your condition.

but because you don't specify anything it's executing your query and
ALSO the actions in the rule if the condition were pass...

http://www.postgresql.org/docs/8.0/static/sql-createrule.html
ALSO
ALSO indicates that the commands should be executed in addition to the
original command.

If neither ALSO nor INSTEAD is specified, ALSO is the default.=20

--=20
regards,
Jaime Casanova
(DBA: DataBase Aniquilator ;)

pgsql-bugs by date:

Previous
From: "Eugene Shekhtman"
Date:
Subject: Multiple-action rule surprise
Next
From: Andrew - Supernews
Date:
Subject: Re: Multiple-action rule surprise