Re: CREATE RULE on VIEW with INSERT after UPDATE does not work

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Peter Marius" <Peter(dot)Marius(at)gmx(dot)de>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: CREATE RULE on VIEW with INSERT after UPDATE does not work
Date: 2007-08-10 21:38:08
Message-ID: 14267.1186781888@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

"Peter Marius" <Peter(dot)Marius(at)gmx(dot)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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Peter Marius 2007-08-11 00:40:03 Re: CREATE RULE on VIEW with INSERT after UPDATE does not work
Previous Message Carlos Ortíz 2007-08-10 21:03:48 Re: SQL question: checking all required items