Re: Multithreaded queue in PgSQL - Mailing list pgsql-general

From Csaba Nagy
Subject Re: Multithreaded queue in PgSQL
Date
Msg-id 1213178426.14929.112.camel@PCD12478
Whole thread Raw
In response to Re: Multithreaded queue in PgSQL  (Stevo Slavić <s.slavic@levi9.com>)
List pgsql-general
We also have such a queue here, and our solution is an algorithm like
this:
 1. get the next processor_count * 2 queue ids which are not marked as
taken;
 2. choose randomly one of these ids;
 3. lock for update with nowait;
 4. if locking succeeds:
     4.1. check again the item, as it could have been processed in the
meantime - if not available, go to 5.;
     4.2. update the DB row to mark the id as taken, and process the
item;
 5. there are more ids to try: loop to 2.
 6. sleep a small random interval, and loop to 1.

This algorithm should have small enough collision rate on a busy queue
due to the random chosen ids and random sleep (it will have high
collision rate on an almost empty queue, but than you don't really
care), while still allowing all processors to access all entries.

Cheers,
Csaba.



pgsql-general by date:

Previous
From: Stevo Slavić
Date:
Subject: Re: Multithreaded queue in PgSQL
Next
From: valgog
Date:
Subject: Re: "connect by"