Thread: Rule creation and evaluation order

Rule creation and evaluation order

From
Vincenzo Romano
Date:
Hi all.
I've already read the friendly manual but have not found any clue.

I would like to do some calculation on the rows of a table every time it
gets updated.
I've decided to use:

create table table1 (
  afield integrer,
  id integrer
);

create table table2 (
  overall integer,
  id integer unique not null,
  primary key(id)
);

create rule myrule
  as on update
  to table1
  do also
    update table2
      set overall=(select count(*) from table1 where afield>0 and
table1.id=NEW.id)
      from table1
      where table2.id = NEW.id
;

What it seems to me is that the table "table1 t1" is being processed *before*
the update and not *after*. Infact the sum() returns the old value.

Is this the expected behaviour?
How can I have my rule action run *after* the actual update?

Thanks to anyone that can help me.

--
Vincenzo Romano
--
Maybe Computers will never be as intelligent as Humans.
For sure they won't ever become so stupid. [VR-1988]

Re: Rule creation and evaluation order

From
Tom Lane
Date:
Vincenzo Romano <vincenzo.romano@gmail.com> writes:
> How can I have my rule action run *after* the actual update?

You can't.  You might consider using a trigger, instead.

            regards, tom lane

Re: Rule creation and evaluation order

From
Vincenzo Romano
Date:
Il 16:58, mercoledì 29 giugno 2005, Tom Lane ha scritto:
> Vincenzo Romano <vincenzo.romano@gmail.com> writes:
> > How can I have my rule action run *after* the actual update?
>
> You can't.  You might consider using a trigger, instead.
>
>             regards, tom lane

Many thanks, Tom!

I have not found much infos about rules and their (weird) behaviour.
Do you have any useful pointer to this topic?

Thanks again.

--
Vincenzo Romano
--
Maybe Computers will never be as intelligent as Humans.
For sure they won't ever become so stupid. [VR-1988]