Re: CREATE RULE on VIEW with INSERT after UPDATE does not work - Mailing list pgsql-general

From Tom Lane
Subject Re: CREATE RULE on VIEW with INSERT after UPDATE does not work
Date
Msg-id 14267.1186781888@sss.pgh.pa.us
Whole thread Raw
In response to Re: CREATE RULE on VIEW with INSERT after UPDATE does not work  ("Peter Marius" <Peter.Marius@gmx.de>)
Responses Re: CREATE RULE on VIEW with INSERT after UPDATE does not work  ("Peter Marius" <Peter.Marius@gmx.de>)
List pgsql-general
"Peter Marius" <Peter.Marius@gmx.de> writes:
> I thought, the example with "mylog" would be better to
> demonstrate the problem, but it's missing the point.
> Below, if have added the code with my real problem.

> CREATE RULE sru AS ON UPDATE TO myview DO INSTEAD
> (
>   UPDATE mytable SET stop = now() WHERE id = old.id AND stop IS null;
>   INSERT INTO myview (id, proc, start, stop) VALUES (old.id, old.proc, now(), null);
> );

AFAICS, all you need to do is swap the ordering of those two operations.

It might help to understand that what you write as an INSERT/VALUES is
really more like INSERT ... SELECT ... FROM myview WHERE ..., the WHERE
condition being the same as was given in the "UPDATE myview" command
that the rule rewrites.  As soon as you change the stop value in the
UPDATE mytable, the SELECT from the view will find nothing.

            regards, tom lane

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: timestamp skew during 7.4 -> 8.2 upgrade
Next
From: "Peter Marius"
Date:
Subject: Re: CREATE RULE on VIEW with INSERT after UPDATE does not work