Re: [SQL] inserts/updates problem under stressing !

From: Oleg Bartunov <oleg(at)sai(dot)msu(dot)su>
To: Vadim Mikheev <vadim(at)krs(dot)ru>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-hackers(at)postgreSQL(dot)org, pgsql-sql(at)postgreSQL(dot)org
Subject: Re: [SQL] inserts/updates problem under stressing !
Date: 1999-07-26 06:13:54
Message-ID: Pine.GSO.3.96.SK.990726100222.10884A-100000@ra
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-sql

On Mon, 26 Jul 1999, Vadim Mikheev wrote:

> Date: Mon, 26 Jul 1999 10:43:00 +0800
> From: Vadim Mikheev <vadim(at)krs(dot)ru>
> To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
> Cc: Oleg Bartunov <oleg(at)sai(dot)msu(dot)su>, pgsql-hackers(at)postgreSQL(dot)org,
> pgsql-sql(at)postgreSQL(dot)org
> Subject: Re: [SQL] inserts/updates problem under stressing !
>
> (Sorry for incomplete prev message).
>
> Tom Lane wrote:
> >
> > I wonder whether this doesn't have a problem with concurrent access:
> >
> > 1. Transaction A does 'Select count into cnt', gets (say) 200.
> > 2. Transaction B does 'Select count into cnt', gets 200.
> > 3. Transaction A writes 201 into hits record.
> > 4. Transaction B writes 201 into hits record.
> >
> > and variants thereof. (Even if A has already written 201, I don't think
> > B will see it until A has committed...)
>
> You're right, Tom.
>
> > I am not too clear on MVCC yet, but I think you need "SELECT FOR UPDATE"
> > or possibly an explicit lock on the hits table in order to avoid this
> > problem. Vadim, any comments?
>
> SELECT FOR UPDATE will not help: if there was no record for
> particular key then nothing will be locked and two records with
> the same key will be inserted.
>
> Oleg, use LOCK IN SHARE ROW EXCLUSIVE MODE.

Thanks Vadim. Just tried this, but still I see a difference between
count hits (accumulated) from db and access_log. In my test these numbers are:
95 and 109. So I lost 14 hits ! And no errors !
In my handler I have now:

my $sth = $dbh->do("LOCK TABLE hits IN SHARE ROW EXCLUSIVE MODE");
my $sth = $dbh->do("SELECT acc_hits($1)") || die $dbh->errstr;

am I right ?

I created hits table as:
create table hits (
msg_id int4 not null primary key,
count int4 not null,
first_access datetime default now(),
last_access datetime
);

and in error_log sometimes I see
ERROR: Cannot insert a duplicate key into a unique index
How this could be possible if I use
LOCK TABLE hits IN SHARE ROW EXCLUSIVE MODE ?

Oleg

PS.
I remind my functions is:

CREATE FUNCTION "acc_hits" (int4) RETURNS int4 AS '
Declare
keyval Alias For $1;
cnt int4;
curtime datetime;
Begin
curtime := ''now'';
Select count into cnt from hits where msg_id = keyval;
if Not Found then
cnt := 1;
-- first_access inserted on default, last_access is NULL
Insert Into hits (msg_id,count) values (keyval, cnt);
else
cnt := cnt + 1;
Update hits set count = cnt,last_access = curtime where msg_id = keyval
End If;
return cnt;
End;
' LANGUAGE 'plpgsql';

>
> Vadim
>

_____________________________________________________________
Oleg Bartunov, sci.researcher, hostmaster of AstroNet,
Sternberg Astronomical Institute, Moscow University (Russia)
Internet: oleg(at)sai(dot)msu(dot)su, http://www.sai.msu.su/~megera/
phone: +007(095)939-16-83, +007(095)939-23-83

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Oleg Bartunov 1999-07-26 06:15:46 Re: [HACKERS] don't lose me :)
Previous Message Peter Mount 1999-07-26 05:40:51 Hello (fwd)

Browse pgsql-sql by date

  From Date Subject
Next Message Vadim Mikheev 1999-07-26 06:26:06 Re: [SQL] inserts/updates problem under stressing !
Previous Message Dan Wilson 1999-07-26 05:16:52 escaping wildcard chars