Buggy SELEC ... FROM UPDATE ? - Mailing list pgsql-novice

From Patrice Espié
Subject Buggy SELEC ... FROM UPDATE ?
Date
Msg-id 008101c0c1ca$c47d8990$0400a8c0@univlyon2.fr
Whole thread Raw
Responses Re: Buggy SELEC ... FROM UPDATE ?  (Helge Bahmann <bahmann@math.tu-freiberg.de>)
Re: Buggy SELEC ... FROM UPDATE ?  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-novice
Hello, (bis)

I try to use SELECT FOR UPDATE, but it seems to be buggy, hum ? Perhaps it's
my algorythm...

My syslock table:
---------------
id   integer
used boolean
---------------

Here is the algo:
---------------
boolean Got = false
while Got == false
    Start transaction
    SELECT * FROM syslock WHERE id=%d AND used='f' FOR UPDATE
    if SELECT returns a row
    then
        UPDATE syslock SET used='t' WHERE id=%d
        Commit transaction
        Got = true
    else
        Rollback transaction
        continue // yes, active wait ... I'm testing !
    end if
end while
Trace "I get the syslock number %d !!"
Do something with the syslock %d which is now reserved
UPDATE syslock SET used='f' WHERE id=%d
---------------

When a lot of threads (well, I tried 20, 5 and 2) used this algo, more than
one thread get the syslock %d at the same time.

What is the problem ?

Thanks
Patrice Espié
--
Le Monde, c'est comme un éléphant
qui va à la pêche



pgsql-novice by date:

Previous
From: Patrice Espié
Date:
Subject: Lock : How to know if a row is locked ?
Next
From: Helge Bahmann
Date:
Subject: Re: Buggy SELEC ... FROM UPDATE ?