On Fri, Nov 20, 2009 at 10:11 AM, Marcel Wieland
<marcel.wieland@fondsnet.de> wrote:
>
> The following bug has been logged online:
>
> Bug reference: =A0 =A0 =A05203
> Logged by: =A0 =A0 =A0 =A0 =A0Marcel Wieland
> Email address: =A0 =A0 =A0marcel.wieland@fondsnet.de
> PostgreSQL version: 8.2
> Operating system: =A0 Linux
> Description: =A0 =A0 =A0 =A0Rule affecting more than one row is only fire=
d once,
> when there is no reference to the row.
> Details:
>
> BEGIN;
>
> -- Create testing Tables
> CREATE TABLE footable (
> =A0 =A0name char
> );
> CREATE TABLE bartable (
> =A0 =A0foo char
> );
>
> -- Insert testing Values
> INSERT INTO footable (name) VALUES('a'), ('b');
>
> -- RULE without row-reference
> CREATE OR REPLACE RULE foorule AS ON UPDATE TO footable DO
> =A0 =A0INSERT INTO bartable (foo) SELECT * FROM (SELECT 'a' UNION SELECT =
'b')
> AS x;
>
> -- Query fires Rule
> UPDATE footable SET name =3D name;
> -- Result
> SELECT * FROM bartable;
>
> -- Reset
> DELETE FROM bartable;
>
> -- RULE with row-reference
> CREATE OR REPLACE RULE foorule AS ON UPDATE TO footable DO
> =A0 =A0INSERT INTO bartable (foo) SELECT * FROM (SELECT 'a' UNION SELECT =
'b')
> AS x WHERE old.name =3D old.name;
>
> -- Query fires Rule
> UPDATE footable SET name =3D name;
> -- Result
> SELECT * FROM bartable;
>
> -- Cleanup
> DROP TABLE footable;
> DROP TABLE bartable;
>
> ROLLBACK;
You haven't really said what the actual behavior is and what behavior
you expected, so it's a bit difficult to judge whether the behavior is
wrong, or whether your expectations are wrong. However, based on the
subject line, I'm guessing that you may be misunderstanding how rules
work. Rules operate on a query level, not on a tuple level. If you
add a DO ALSO rule, you're just constructing a new query which gets
executed in addition to the original query. If you want something
that gets fired for every tuple, you probably want a trigger.
In my experience, triggers are always better than rules for solving
all real-world problems.
...Robert