Re: FIFO Queue Problems

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: cgg007(at)yahoo(dot)com
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: FIFO Queue Problems
Date: 2002-11-01 16:51:01
Message-ID: 26900.1036169461@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-sql

Chris Gamache <cgg007(at)yahoo(dot)com> writes:
> I have a program that claims a row for itself

> my $processid = $$;
> my $sql_update = <<EOS;
> UPDATE fifo
> set status=$processid
> WHERE id = (SELECT min(id) FROM fifo WHERE status=0);
> EOS

> The problem occurrs when two of the processes grab the exact same row at the
> exact same instant.

Probably the best fix is to do it this way:

BEGIN;
LOCK TABLE fifo IN EXCLUSIVE MODE;
UPDATE ... as above ...
COMMIT;

The exclusive lock will ensure that only one process claims a row
at a time (while not preventing concurrent SELECTs from the table).
This way you don't need to worry about retrying.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2002-11-01 16:51:48 Re: pg_restore error
Previous Message Neil Conway 2002-11-01 16:48:06 Re: my.cnf to postgresql.conf Conversion

Browse pgsql-sql by date

  From Date Subject
Next Message Bruno Wolff III 2002-11-01 16:54:07 Re: FIFO Queue Problems
Previous Message Ross J. Reedstrom 2002-11-01 16:37:35 Re: Selecting * from the base table but getting the inheriteds