pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Incrimenting question
2+ messages / 2 participants
[nested] [flat]

* Incrimenting question
@ 1998-11-15 13:18  M Simms <grim@argh.demon.co.uk>
  0 siblings, 1 reply; 2+ messages in thread

From: M Simms @ 1998-11-15 13:18 UTC (permalink / raw)
  To: pgsql-sql

I have a question regarding incrimenting values. I have looked but cannot find
any definite answers in the documentation.
 
In an example in the documentation, it is shown:
 
update weather set temp_hi=temp_hi-1,temp_lo=temp_lo-2
  where date > '11/28/1994';

Now, this does NOT clearly tell me how safe this action is. I am looking
for a safe way to do that that ensures that if this action is performed
twice at the same time by different people, that it WILL be incrimented
twice.
 
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.
 
Otherwise I am going to have to create sequences for EVERY page and thats going
to really defeat the reason for using an RDB.

Thanks in advance



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: [SQL] Incrimenting question
@ 1998-11-15 15:02  Herouth Maoz <herouth@oumail.openu.ac.il>
  parent: M Simms <grim@argh.demon.co.uk>
  0 siblings, 0 replies; 2+ messages in thread

From: Herouth Maoz @ 1998-11-15 15:02 UTC (permalink / raw)
  To: M Simms <grim@argh.demon.co.uk>; +Cc: pgsql-sql

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





^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~1998-11-15 15:02 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1998-11-15 13:18 Incrimenting question M Simms <grim@argh.demon.co.uk>
1998-11-15 15:02 ` Herouth Maoz <herouth@oumail.openu.ac.il>

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