Rule Question

From: "Mitch Vincent" <mitch(at)venux(dot)net>
To: <pgsql-sql(at)postgreSQL(dot)org>
Subject: Rule Question
Date: 1999-12-19 21:40:37
Message-ID: 01bb01bf4a69$b0fdae60$0300000a@doot.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 1999-12-19 22:38:56 Re: [SQL] New count() question
Previous Message Timothy Laswell 1999-12-19 20:09:00 Re: [GENERAL] NOTICE: (transaction aborted): queries ignored until END