Re: Selecting a non-locked row.

From: Josh Berkus <josh(at)agliodbs(dot)com>
To: Kurt Roeckx <Q(at)ping(dot)be>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Selecting a non-locked row.
Date: 2003-01-19 22:34:21
Message-ID: 200301191434.21913.josh@agliodbs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Kurt,

> Of course a second person doing the same thing will just wait for
> my commit.
>
> What I want is that he just gets the next non-locked record.

Well, there's two ways you can do this:

1) hack the system tables to find out which incomplete rows in the table are
currently locked, and select the lowest ID from those that aren't. You can
do this in 7.3 fairly easily throught the "pg_locks" table, but in 7.2.3 it
involves a rather annoying hack of the "hidden" tuple fields (which I don't
know very well, so don't ask).

2) (my preference) modify your done/not done field to accept 3 values: not
done, in progress, done. Then add this step between select ... for update
and the final update that updates the row as "in progress". Then you can
more easily select the first "not done" row. (actually, you would have to
set a tuple lock with something longer lasting than select for update, and
arrange to drop it if the connection dies. but it's still my preferred
solution)

--
-Josh Berkus
Aglio Database Solutions
San Francisco

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Joseph Healy 2003-01-19 22:36:41 Group By Error Text
Previous Message Kurt Roeckx 2003-01-19 22:03:48 Re: Selecting a non-locked row.