Re: Stored procedure issue - Mailing list pgsql-hackers

From Dragan Zubac
Subject Re: Stored procedure issue
Date
Msg-id 838009.42660.qm@web50606.mail.re2.yahoo.com
Whole thread Raw
In response to Re: Stored procedure issue  ("Usama Dar" <munir.usama@gmail.com>)
List pgsql-hackers
Hello

Please find in attachment stored procedure
(proc_uni.txt),as well as description of tables
involved in calculations.
The idea for procedure is to find longest prefix match
for destination number,try to find it in table
'billing' for particular users,find the price,and
insert message into history and inqueue table,as well
as to decreace the user's balance in table 'users'.
Would it help to put all prefices,prices data in some
sort of cache and let procedure first try to match
with data from cache and if it can't find to try to
get data from table itself from hard disk ?

I'm looking for some solution where this procedure can
operate at higher loads and to leave other parts of
database operational as much as it could.

Sincerely

Pera

--- Usama Dar <munir.usama@gmail.com> wrote:

> On Dec 2, 2007 7:40 AM, Dragan Zubac
> <moroncic@yahoo.com> wrote:
>
> > Hello
> >
> > I have a stored procedure which does the billing
> stuff
> > in our system,it works ok,but if I put in
> > production,where there is some 5-10 billing events
> per
> > second,the whole database slows down. It won't
> even
> > drop some test table,reindex,vacuum,things which
> were
> > done before in the blink of an eye. If I stop the
> > application which calls the procedure,all is back
> to
> > normal.
> >
> > We didn't implement any special locking mechanism
> in
> > the procedure,all is default. The procedure is
> > updating user's balance in table 'users'. On the
> other
> > hand a couple of 'heavy load' table has foreign
> keys
> > pointing to table 'users'.
> >
> > Is it the matter of concurency and some locking
> issue
> > or maybe the existing of all those foreign keys
> > pointing to table 'users',or maybe something else
> > which we're not aware at the moment ?
>
>
> Can you please post your procedure and explain plan
> of the SQL which the
> procedure uses to do the billing stuff . There can
> be a zillion reasons for
> the performance problems you are seeing, but the
> email does not provide
> enough information.
>
>
> >
> > Sincerely
> >
> > Pera
> >
> >
> >
> >
>
____________________________________________________________________________________
> > Be a better sports nut!  Let your teams follow you
> > with Yahoo Mobile. Try it now.
> >
>
http://mobile.yahoo.com/sports;_ylt=At9_qDKvtAbMuh1G1SQtBI7ntAcJ
> >
> > ---------------------------(end of
> broadcast)---------------------------
> > TIP 7: You can help support the PostgreSQL project
> by donating at
> >
> >
> http://www.postgresql.org/about/donate
> >
>
>
>
> --
> Usama Munir Dar http://linkedin.com/in/usamadar
> Consultant Architect
> Cell:+92 321 5020666
> Skype: usamadar
>


      ____________________________________________________________________________________
Get easy, one-click access to your favorites.
Make Yahoo! your homepage.
http://www.yahoo.com/r/hs
create type dajbre as (status int,id bigint);

CREATE OR REPLACE FUNCTION proc_uni(integer,integer,inet,text,integer,integer,text,integer,integer,
text,int, int,boolean,text) RETURNS setof dajbre AS '

DECLARE

uid alias for $1;
pid alias for $2;
ip_i alias for $3;
s_number alias for $4;
s_ton_i alias for $5;
s_npi_i alias for $6;
d_number alias for $7;
d_ton_i alias for $8;
d_npi_i alias for $9;
mess alias for $10;
dcs_i alias for $11;
esm_i alias for $12;
delivery_i alias for $13;
u_mess_id_i alias for $14;

r dajbre%rowtype;

prefixfound boolean;
prefixprice billing.price%TYPE;
dest_num_len int;
tmp_dest_number text;
tmp_user_bal numeric;
tmp_returnval int;
novi_status int;
tmp_his_id bigint;
tmp_u_mess_id_i text;


begin


dest_num_len := char_length(d_number);
tmp_dest_number := d_number;
prefixfound := false;


while dest_num_len > 0 loop

    select into prefixprice price from billing
        where u_id=uid and prefix=tmp_dest_number;

    if not found then
        tmp_dest_number := substring (tmp_dest_number from 1 for dest_num_len-1);
        dest_num_len := char_length(tmp_dest_number);
    else
        prefixfound := true;
        exit;
    end if;
end loop;


if prefixfound=false then
    tmp_returnval :=11;
    novi_status :=11;
else if prefixprice = 0 then
    tmp_returnval :=11;
    novi_status :=50;
     else select into tmp_user_bal maxsms-cursms from users where id=uid;
    if tmp_user_bal < prefixprice then
        tmp_returnval :=11;
        novi_status :=51;
    else
        tmp_returnval :=0;
    end if;
     end if;
end if;


if tmp_returnval = 0 then


insert into history (ip,source,dest,message,dcs,esm,s_ton,s_npi,d_ton,d_npi,u_id,delivery,price,p_id,u_mess_id) values

(ip_i,s_number,d_number,decode(mess,''base64''),dcs_i,esm_i,s_ton_i,s_npi_i,d_ton_i,d_npi_i,uid,delivery_i,prefixprice,pid,u_mess_id_i);

 tmp_his_id := currval(''history_id_seq'');



if pid = 2 then
    if u_mess_id_i = 0 then
          tmp_u_mess_id_i := '''';
    else
        tmp_u_mess_id_i := u_mess_id_i;
    end if;
else if pid = 3 then
      tmp_u_mess_id_i := tmp_his_id ;
     end if;
end if;

update history set u_mess_id = tmp_u_mess_id_i where id = tmp_his_id;


update users set cursms=cursms+ prefixprice where id=uid;


insert into inqueue(id, u_id) values (tmp_his_id, uid);

r.status := 0;
r.id := tmp_his_id;
return next r;


else


    insert into rejected (ip,source,dest,message,dcs,esm,s_ton,s_npi,d_ton,d_npi,status,u_id,delivery,u_mess_id) values

(ip_i,s_number,d_number,decode(mess,''base64''),dcs_i,esm_i,s_ton_i,s_npi_i,d_ton_i,d_npi_i,novi_status,uid,delivery_i,u_mess_id_i);

r.status := 11;
r.id := 0;
return next r;

end if;

    return;

end;

' language 'plpgsql';

Attachment

pgsql-hackers by date:

Previous
From: Gregory Stark
Date:
Subject: Re: There's random access and then there's random access
Next
From: Dragan Zubac
Date:
Subject: Re: [GENERAL] Stored procedure issue