pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Robin Whitworth <rwhit@goldwiretech.com>
To: pgsql-interfaces@hub.org
Subject: Re: resolving locking conflicts
Date: Thu, 15 Jul 1999 09:33:32 -0400
Message-ID: <378DE32C.4C3BE7C2@goldwiretech.com> (raw)

An alternative strategy is to use logical locks, rather than
physical locks. To do this, add a column to your table to
indicate whether or not a given record is locked. You might, for
example, leave the column NULL when the record is not locked, and
set it to the id of the locking user when it is locked.

Then, to lock a record do something like:

BEGIN;
SELECT locked, ... FROM table WHERE ...;
if (locked is not NULL)
{
	Error("row locked by user %s\n", locked)
}
else
{
	UPDATE table SET locked = <current_user_id>;
}
COMMIT;

You can expand on this by adding a timestamp to indicate when
the lock was created and allow it to be broken if held "too long".
	-rwhit

> Date: Wed, 14 Jul 1999 13:04:06 +0100
> From: "Burgess, Trevor - HMS" <Trevor.Burgess@haysdx.co.uk>
> Subject: resolving locking conflicts
> 
> I am using Postgres 6.5 on Linux through the libpq interface
> 
> I have a situation where an existing record can be opened by one user,
> updated or rolled back. While the first user has the record I need to make
> sure no other user can open it. I use
> 
> SELECT .... FOR UPDATE
> 
> This works but the second process blocks (looking to the user like a hung
> program), until the first process releases the record. Since updating a
> record can take some time the blocking could be there for some time.
> 
> I would like the second SELECT .... FOR UPDATE to fail with an error I can
> catch telling me of a conflict (and hopefully which process has the lock),
> allowing me to backout gracefully with a consolatory message to the user. I
> would like normal SELECTs (without FOR UPDATE) to work an normal.
> 
> I think that this is an option in pg_options but I'm not sure. I have looked
> in the source (lock.c and lmgr.c) to try and figure out what to do. I found
> something called "user locks" which looks promising but I'm still not sure.
> 
> Help !
> 
> Trevor



view thread (3+ messages)

Message-ID: <378DE32C.4C3BE7C2@goldwiretech.com>
Permalink:  ../378DE32C.4C3BE7C2@goldwiretech.com/
Also on:    postgresql.org/message-id/378DE32C.4C3BE7C2@goldwiretech.com

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-interfaces@postgresql.org
  Cc: rwhit@goldwiretech.com, pgsql-interfaces@hub.org
  Subject: Re: resolving locking conflicts
  In-Reply-To: <378DE32C.4C3BE7C2@goldwiretech.com>

* 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