Re: Apache::Session::Store::Postgres FOR UPDATE problems [OT]

[prev] [thread] [next] [Date index for 2005/05/02]

From: Michael Schout
Subject: Re: Apache::Session::Store::Postgres FOR UPDATE problems [OT]
Date: 22:40 on 02 May 2005
Jeffrey W. Baker wrote:

> It is possible that the FOR UPDATE is spurious.  It signals to the
> database system that this transaction intends to write that row.  With
> PostgreSQL's MVCC transaction isolation system, it's probably not
> necessary and may be causing problems.

It definately *is* necessary if you want to ensure that only one process
has access to your session data at a time.  MVCC will prevent a second
client from WRITING to the same row, but it will not block it from
reading the row.

In other words, MVCC will not prevent the following scenario:

client 1: SELECT * FROM sessions WHERE id='1';
client 2: SELECT * FROM sessions WHERE id='1';

# at this point, client 1 and client 2 both have copies of the session.
 suppose client 1 makes changes to the session data and saves it:

client 1: UPDATE sessions SET data='...' WHERE id='1';

now suppose client 2 makes changes:

client 2: UPDATE sessions SET data='...' WHERE id='1';

Whoops, you just wiped out the changes that client 1 made!

FOR UPDATE prevents this because it tells the database that you intend
to change the row.  The database will not let anyone else select that
row FOR UPDATE until you either issue a COMMIT or ROLLBACK.  So in
otherwords:

client 1: SELECT * FROM sessions WHERE id='1' FOR UPDATE;
client 2: SELECT * FROM sessions WHERE id='1' FOR UPDATE;

at this point, client 2 will block until client 1 either does COMMIT or
ROLLBACK.

So if you want to ensure that only one client has the session data at a
time, you need FOR UPDATE.

Regards,
Michael Schout

Apache::Session::Store::Postgres FOR UPDATE problems
Kjetil Kjernsmo 14:50 on 29 Apr 2005

Re: Apache::Session::Store::Postgres FOR UPDATE problems [OT]
Michael Schout 22:40 on 02 May 2005

Generated at 10:25 on 04 May 2005 by mariachi v0.52