dbTalk Databases Forums  

[BUGS] BUG #1447: Sometimes rules don't work

mailing.database.pgsql-bugs mailing.database.pgsql-bugs


Discuss [BUGS] BUG #1447: Sometimes rules don't work in the mailing.database.pgsql-bugs forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Olleg Samoylov
 
Posts: n/a

Default [BUGS] BUG #1447: Sometimes rules don't work - 01-29-2005 , 01:58 PM







The following bug has been logged online:

Bug reference: 1447
Logged by: Olleg Samoylov
Email address: olleg (AT) mipt (DOT) ru
PostgreSQL version: 7.4.6, 8.0.0
Operating system: x86_64-pc-linux-gnu, compiled by GCC x86_64-linux-gcc
(GCC) 3.3.5 (Debian 1:3.3.5-6)
Description: Sometimes rules don't work
Details:

Test below show this bug. This is very simplified query from real billing
system. If abonent get service less then $5 per month, the billing must get
other.

--create structure
-- table with abonent and his money
create table abonent (abonent integer primary key, money double precision
not null default 0);
-- table with history of money change
create table history (abonent integer not null references abonent, money
double precision not null);
-- to change money enough add record to table history
create rule history_i as on insert to history do (update abonent set
money=money+new.money where abonent=new.abonent);

-- insert example values
-- one abonent
insert into abonent values (1);
-- it get service for $2
insert into history values (1,-2);

-- Always must be 0, due to rule history_i
select abonent.money-history.money from (select sum(money) as money from
abonent) abonent, (select sum(money) as money from history) history;
-- Okey

-- payment per month, simplified but still show bug
-- minimal payment $5
insert into history (abonent,money) select abonent,-(money.money+5) as pay
from
( select abonent,sum(money) as money from history where money<0 group by
abonent) money
where money.money+5>0;

-- must be 0
select abonent.money-history.money from (select sum(money) as money from
abonent) abonent, (select sum(money) as money from history) history;
-- fail

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Reply With Quote
  #2  
Old   
Tom Lane
 
Posts: n/a

Default Re: [BUGS] BUG #1447: Sometimes rules don't work - 01-29-2005 , 05:37 PM






"Olleg Samoylov" <olleg (AT) mipt (DOT) ru> writes:
Quote:
create rule history_i as on insert to history do (update abonent set
money=money+new.money where abonent=new.abonent);

insert into history (abonent,money) select abonent,-(money.money+5) as pay
from
( select abonent,sum(money) as money from history where money<0 group by
abonent) money
where money.money+5>0;
What happens in the above is that the "new.money" placeholder is
replaced by the subselect from the INSERT command, and since the rule
fires after the insert itself is done, you get different results from
the second evaluation of the subselect.

You should be using a trigger for this sort of thing, not a rule.
Rules have their uses, but propagating copies of data from one place
to another is generally not one of them. It's too easy to get bit by
the fact that a rule is a macro and thus subject to multiple-evaluation
gotchas.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.