[prev in list] [next in list] [prev in thread] [next in thread] 

List:       postgresql-sql
Subject:    Re: [SQL] Incrimenting question
From:       Herouth Maoz <herouth () oumail ! openu ! ac ! il>
Date:       1998-11-15 15:02:55
[Download RAW message or body]

At 15:18 +0200 on 15/11/98, M Simms wrote:


> I am writing a counter for a webpage and need to know that when I do
>
> update pagestat set hitcount=hitcount+1 where page='main';
>
> that it will count every hit and not just drop hits if 2 people access at
> the same moment. It is a very busy page.

Any statement is inside a transaction. If you have not defined the
transaction explicitly, then each statement is a transaction in its own
right.

Any table accessed from within a transaction is locked for the duration of
the transaction. If the statement was a SELECT statement, the lock is
shared. If it is an UPDATE/INSERT statement, the lock is exclusive.

All this boils down to: If one user enters an update statement on one
table, no other user may "start" the update until the first one has
finished and released the lock. This implies that any simple arithmetic
calculations within a single update statement are, indeed, atomic.

Things to watch out for: SELECTING some data, making a calculation in the
frontend, then updating it. This sort of thing requires a transaction, and
it had better have a LOCK statement in the beginning. But that's not what
you are doing.

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic