Re: Postgresql the right tool (queue using advisory_locks + long transactions)

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Dorian Hoxha <dorian(dot)hoxha(at)gmail(dot)com>
Cc: PostgreSql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Postgresql the right tool (queue using advisory_locks + long transactions)
Date: 2014-04-27 19:53:41
Message-ID: 24494.1398628421@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Dorian Hoxha <dorian(dot)hoxha(at)gmail(dot)com> writes:
> I am trying to use postgresql as a queue for long-jobs (max ~4 hours) using
> advisory_locks. I can't separate the long-job into sub-jobs.

> 1. At ultimate-best-case scenario there will be ~100 workers, so no
> web-scale performance required.
> Is there a problem with 100 open sessions (behind a connection pooler?)
> for hours like in my case?
> The session will commit the progress of the job, but will stay opened
> and hold the advisory_lock till the job is done or it expires (look 2).

Sitting on an open transaction for hours would be a bad idea. An idle
session isn't a problem though. So as long as you use session locks not
transaction locks, this should work fine.

> 2. Is it possible to set a time limit to auto-close an opened session
> that hasn't made a query in a while ?
> So if a worker fails,and the session time-outs, postgresql/pgbouncer
> will close the session and release the lock ?

There's no such thing in core postgres. Perhaps pgbouncer or another
connection pooler has such a feature; though I'm not sure if a pooler
might not cause problems of its own (it probably won't realize that the
connections aren't interchangeable if they're holding session-level
advisory locks). I'm a bit confused though as to why you'd want this for
your application. Have you got an a-priori hard limit as to how long your
"long jobs" could take? Do you really want to kill their database
connections if they take a bit longer than you thought? Seems like as
long as the client stays connected, it'd be better to assume it's still
working.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David G Johnston 2014-04-27 20:07:05 Re: Postgresql the right tool (queue using advisory_locks + long transactions)
Previous Message Dorian Hoxha 2014-04-27 19:31:42 Postgresql the right tool (queue using advisory_locks + long transactions)