Re: rules on INSERT can't UPDATE new instance?

From: Louis-David Mitterrand <cunctator(at)apartia(dot)ch>
To: pgsql-general(at)hub(dot)org
Subject: Re: rules on INSERT can't UPDATE new instance?
Date: 2000-05-20 16:06:35
Message-ID: 20000520180634.A638@styx
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

On Sat, May 20, 2000 at 10:41:53AM -0400, Bruce Momjian wrote:
> > But this doesn't work in PG 7.0:
> >
> > auction=> create table test (price float);
> > CREATE
> > auction=> create rule price_control AS ON INSERT TO test WHERE new.price > 100 DO UPDATE test SET price = 100 where test.oid = new.oid;
> > CREATE 27913 1
> > auction=> INSERT INTO test VALUES (101);
> > INSERT 27914 1
> > auction=> SELECT test.*;
> > price
> > -------
> > 101
> > (1 row)
>
> Yes, I see it failing too. I tried old.oid, and that failed too.
>
> I know there is a recursive problem with rules acting on their own
> table, where if you have an INSERT rule that performs an INSERT on the
> same table, the rules keep firing in a loop.
>
> I thought an INSERT rule with an UPDATE action would work on the same
> table, but that fails. Seems the rule is firing before the INSERT
> happens.
>
> I am not really sure what to recommend. The INSERT rule clearly doesn't
> fix cases where someone UPDATE's the row to != 100. A CHECK constraint
> could be used to force the column to contain 100, but that doesn't
> silently fix non-100 values, which seemed to be your goal. A trigger
> will allow this kind of action, on INSERT and UPDATE, though they are a
> little more complicated than rules.

Thanks for all your help. You are right: this seems more like the job of
a trigger and I am exploring that topic in depth right now.

Cheers,

--
Louis-David Mitterrand - ldm(at)apartia(dot)org - http://www.apartia.fr

Parkinson's Law: Work expands to fill the time alloted it.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2000-05-20 16:19:51 Re: rules on INSERT can't UPDATE new instance?
Previous Message Tom Lane 2000-05-20 15:41:38 Re: Alias in WHERE clause

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2000-05-20 16:19:51 Re: rules on INSERT can't UPDATE new instance?
Previous Message Chris 2000-05-20 15:55:39 Re: OO Patch