Thread: RE: SELECT FOR UPDATE

RE: SELECT FOR UPDATE

From
"Willis, Ian (Ento, Canberra)"
Date:
You should handle this at the application level as well.
User 1 and 2 open and Work on page
User 1 commits changes
User 2 goes to commits change and gets error message pages updated during
editing.
    User 2 could be     Prompted overwrite current values, user1's
update would be lost.
                Save page under a different value
                Merge data ...



--
Ian Willis

-----Original Message-----
From: Oliver Elphick [mailto:olly@lfix.co.uk]
Sent: Friday, 24 August 2001 2:06 AM
To: Mike Castle
Cc: Postgres
Subject: Re: [GENERAL] SELECT FOR UPDATE


Mike Castle wrote:
  >On Thu, Aug 23, 2001 at 10:09:19AM -0400, Jan Wieck wrote:
  >> Oliver Elphick wrote:
  >> > I can see arguments to support this view, but consider this classic
  >> > scenario:
  >> >
  >> > User1: Read data into an interactive program
  >> > User1: Start to make changes
  >> > User2: Read data into an interactive program
  >> > User2: Start to make changes
  >> > User1: Save changes
  >> > User2: Save changes
  >
  >Consider replacing "Save changes" with:
  >
  >User1: Lock record, compare original with current record, save if same,
unlo
      >ck
  >User2: Lock record, compare original with current record, notice
difference,
      > abort.

Yes, but if User2 has done substantial editing changes to a field (after all
we could store whole books in a SQL field now), his changes will be rejected
and the program will have to throw them away or else try to integrate them
with the new field contents - in either case there is substantial wasted
effort.

I prefer Jan's solution: on first attempt to change, acquire a user-level
lock by creating a lock record; if you can't get the lock, don't allow
any change.

However, it would be convenient if the database would do this for me.  I
still don't understand why people think it undesirable for it to do so,
since
it is a problem universal to multi-user databases and the effort is
therefore more economically spent at the database rather than at the
application level.

--
Oliver Elphick                                Oliver.Elphick@lfix.co.uk
Isle of Wight                              http://www.lfix.co.uk/oliver
PGP: 1024R/32B8FAA1: 97 EA 1D 47 72 3F 28 47  6B 7E 39 CC 56 E4 C1 47
GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839  932A 614D 4C34 3E1D 0C1C
                 ========================================
     "For God hath not appointed us to wrath, but to obtain
      salvation by our Lord Jesus Christ, Who died for us,
      that, whether we wake or sleep, we should live
      together with him."
                         I Thessalonians 5:9,10



---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Re: SELECT FOR UPDATE

From
ok@mochamail.com (Cody)
Date:
Thank you for the considerate reply.  I have been trying to figure
this out for the last day or two.

I still wouldn't go near SELECT...FOR UPDATE with a ten-foot pole
anymore.  It acquires a ROW SHARE MODE lock, and I cannot quite see
how or why that works.  I'm trying out SERIALIZABLE, but I think I'll
just manually implement ROW EXCLUSIVE locks & then use the lock table.
 I can see that's not  app. level transactions, but it would seem to
require more overhead than app level transactions (because of the need
for the lock table).

glenebob@nwlink.com ("Glen Parker") wrote in message news:<000001c12f2c$83de3950$0b01a8c0@saturn>...
> > On 26 Aug 2001 13:50:16 -0700, Cody <ok@mochamail.com> wrote:
> > > I just finished reading Bruce M's book, so this thread confuses me,
> > > esp. Jan's posts.  I take full heed of the need for application level
> > > user/thread management, but I was interested in using a parallel
> > > set-up in PG (however redundant that might be).  Now that Jan has
> > > discounted "SELECT...FOR UPDATE," is the best alternative using a
> > > central locking table (perhaps in conjunction with LISTEN & NOTIFY)?
>
> It certainly does not discount SELECT...FOR UPDATE ("SFU").  You need some
> way to implement a mutex of sorts at the DB level, in order to insert new
> lock records into the lock table, and this is where SFU comes into play.
> But ANY long running DB level transaction is generally a bad thing.
>
> > > Ironically, anyone who suggested using application level transactions
> > > would be torn apart at any of the places I've worked at--but that
>
> This also is definately not app level transactions.  I've implemented a
> lock-table system on a non-transactional database (Paradox) as well, and
> it's not a pretty thing :-)  Generally two DB transactions take place to
> effect a checkout/checkin cycle, but what happens in between those two
> operations is completely outside the scope of any kind of transactioning.
>
> > > seems to be the gist of this thread.  I cannot see a way to avoid
> > > deadlocks without an application level transaction component, since
> > > the central locking table idea would similarily lock the record
> > > forever if the first transaction failed to COMMIT or ROLLBACK.
>
> If the first transaction fails, it is no different than any other
> transaction failing to end in a timely manor: problems :-)  This isn't a
> special case, the database can't ever completely compensate for a
> mis-behaved application, since it can't possibly know how the application is
> *intended* to work.
>
> Provided the app(s) are well-behaved, the common problem would be where the
> second transaction (either an update/unlock or abandon/unlock) never
> happens.  As I and others have mentioned, this can be handled by including
> some sort of timeout field in the lock table, a periodic process to clean
> stale lock records from the database, and a tool to explicitly remove locks
> that can be run by a privileged user.  In my experience, with a properly
> designed timeout system, stale locks rarely get in the way; with reliable
> client-side software, they don't even occur very often.
>
> Glen Parker
> glenebob@nwlink.com