Rule Question - Mailing list pgsql-sql

From Mitch Vincent
Subject Rule Question
Date
Msg-id 01bb01bf4a69$b0fdae60$0300000a@doot.org
Whole thread Raw
Responses Re: [SQL] Rule Question  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-sql
Boy, I'm just full of questions this weekend. On with another one..

The overall objective here is to have the field "total" on an invoice
calculated every time the table is updated, so it's always up to date. I
could do this programatically but there are so many ways that this can
change, it would be great if Postgre just did it for me every time :-)

>From what I read about rules, this is possible so what I did was this :


Created this function to do the totalling:
create function total_up(int4) returns float8 as 'select fee_membership +
fee_logins + fee_convention + fee_prints+ fee_hotlines + fee_postage +
fee_ups + fee_late + fee_other1 + fee_other2 + fee_other3 + fee_pastdue from
invoice where invoice_number = $1;' language 'sql';

And this rule to call the function and update the invoice that has been
updated, inserting the new total.  :
create rule total_invoice as on update to invoice where
invoice_number=NEW.invoice_number DO update invoice set total =
total_up(NEW.invoice_number) where invoice_number=NEW.invoice_number;

However I get this when I do an update on an invoice:

PostgreSQL Error: 1 (ERROR: query rewritten 10 times, may contain cycles )
Session halted.

Some kind of loop? Well, I didn't see any kind of loop or error but that is
probably due to my little understanding of rules..

Thanks yet again!

-Mitch




pgsql-sql by date:

Previous
From: Timothy Laswell
Date:
Subject: Re: [GENERAL] NOTICE: (transaction aborted): queries ignored until END
Next
From: Tom Lane
Date:
Subject: Re: [SQL] New count() question