Re: Processing a work queue

Поиск
Список
Период
Сортировка
От John D. Burger
Тема Re: Processing a work queue
Дата
Msg-id 2AFE3D3E-D825-407B-8871-0E456DBA8D00@mitre.org
обсуждение исходный текст
Ответ на Processing a work queue  (Steve Crawford <scrawford@pinpointresearch.com>)
Ответы Re: Processing a work queue
Список pgsql-general
Steve Crawford wrote:

> Anyone have any ideas on how to handle a work queue? I've been
> thinking
> about optimizing this process for quite a while.

I use a variant of The Tom Lane Solution previously pointed to, your
Plan 1 is very similar.

> This does not produce desirable results. In the case where requests
> for
> work overlap, the first query will complete. The second query will
> block
> until the first completes and then apparently re-evaluate the
> condition
> and toss the record thus returning zero-rows.

I have no experience with this, but I think you can do SELECT FOR
UPDATE NOWAIT to avoid the blocking.

> Plan 1a:
>
> Check for tuples returned and re-run query if zero. This will go
> into an
> infinite loop whenever there is nothing in the queue and cause
> undesirable thrashing if there is too much contention.

So either sleep a bit, as in Tom's solution, or use NOTIFY/LISTEN,
which is what I do.  I have a trigger like this on my queue:

create or replace function notify_new_work() returns trigger as
'
BEGIN
NOTIFY WORK;
RETURN NULL;
END;
' language 'plpgsql';

create trigger notify_new_work
        after insert on work_queue
        for each statement execute procedure notify_new_work();

My workers do LISTEN WORK after connecting, and then do a (UNIX)
select on the connection socket when they get zero results from the
(SQL) select.  This puts them to sleep until the next NOTIFY fires.
How to get the socket and do the (UNIX) select will depend on your
client library and language.

- John Burger
   MITRE

В списке pgsql-general по дате отправления:

Предыдущее
От: Tom Lane
Дата:
Сообщение: Re: DIfferent plans for explicit versus implicit join using link table
Следующее
От: Oleg Bartunov
Дата:
Сообщение: Re: PostgreSQL upgrade server A -> server Bx