From: | "Dawid Kuroczko" <qnex42(at)gmail(dot)com> |
---|---|
To: | "Mark Harrison" <mh(at)pixar(dot)com>, "PostgreSQL general" <pgsql-general(at)postgresql(dot)org> |
Subject: | Re: queueing via database table? |
Date: | 2007-01-03 09:14:54 |
Message-ID: | 758d5e7f0701030114t4a1b590ds319f534c2793545@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On 1/3/07, Mark Harrison <mh(at)pixar(dot)com> wrote:
> Does the following sound like a reasonable approach?
> If not, what's a good way to go?
>
> The processes generating the requests will insert into
> a queue table. They may add a priority and timestamp.
>
> The several processes servicing the requests will do a
> SELECT FOR UPDATE where ... limit 1, generate thumbnail,
> delete the record and commit.
Well, this will block. So it will mean that only one thumbnail
will be processed while running the transaction.
You may want to rather use SELECT FOR UPDATE NOWAIT,
probably "wrapped" into a PL/PgSQL function. I did that and
I'm quite satisfied with this approach.
A simple implementation would be something like this:
CREATE OR REPLACE FUNCTION get_next() RETURNS int AS $$
DECLARE
r RECORD;
BEGIN
FOR r IN SELECT id FROM foo_table LIMIT 100 LOOP
BEGIN
PERFORM id FROM foo_table WHERE id=r.id FOR UPDATE NOWAIT;
RETURN r.id;
EXCEPTION
WHEN lock_not_available THEN -- do nothing
END;
END LOOP;
RETURN NULL;
END;
$$ LANGUAGE PLpgSQL;
Of course you should customize the query, and use better tuned limit.
I think good rule of the thumb size of LIMIT is twice the number of
simultaneous processing nodes working. An ORDER BY might be
worh it or not, etc, etc.
Other approach might be using something like
LOOP
BEGIN
SELECT id INTO i FROM foo_table LIMIT 1 OFFSET n FOR UPDATE NOWAIT;
RETURN i;
EXCEPTION
WHEN lock_not_avaibale THEN -- do nothing;
END;
n := n + 1;
END LOOP;
But I feel it will be slower most of the time.
From | Date | Subject | |
---|---|---|---|
Next Message | Gregory S. Williamson | 2007-01-03 09:52:08 | Re: queueing via database table? |
Previous Message | Richard Huxton | 2007-01-03 08:37:30 | Re: queueing via database table? |