pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Oleg Bartunov <oleg@sai.msu.su>
Cc: pgsql-hackers@postgreSQL.org
Cc: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] inserts/updates problem under stressing !
Date: Sat, 24 Jul 1999 12:29:06 -0400
Message-ID: <1696.932833746@sss.pgh.pa.us> (raw)
In-Reply-To: <Pine.GSO.3.96.SK.990724121543.18633v-100000@ra>

Oleg Bartunov <oleg@sai.msu.su> writes:
> I did some benchmarks of my Web site and notice I lost some hits
> which I accumulate in postgres (6.5.1) database on Linux 2.0.36 system

> 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';

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...)

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?

			regards, tom lane



view thread (11+ messages)  latest in thread

Message-ID: <1696.932833746@sss.pgh.pa.us>
Permalink:  ../1696.932833746@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/1696.932833746@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, oleg@sai.msu.su, pgsql-hackers@postgreSQL.org
  Subject: Re: [SQL] inserts/updates problem under stressing !
  In-Reply-To: <1696.932833746@sss.pgh.pa.us>
  References: <Pine.GSO.3.96.SK.990724121543.18633v-100000@ra> <1696.932833746@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox