Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id E6A7011DF227 for ; Thu, 31 May 2012 14:17:02 -0300 (ADT) Received: from news.hub.org ([200.46.204.72]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Sa8zb-0007f8-VR for pgsql-sql@postgresql.org; Thu, 31 May 2012 17:17:02 +0000 Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.14.5/8.14.5) with ESMTP id q4VHGjdm023525 for ; Thu, 31 May 2012 14:16:45 -0300 (ADT) (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.14.5/8.14.5/Submit) id q4VGw46x006499 for pgsql-sql@postgresql.org; Thu, 31 May 2012 13:58:04 -0300 (ADT) (envelope-from news) From: lewbloch@gmail.com X-Newsgroups: pgsql.sql Subject: Re: Job control in sql Date: Thu, 31 May 2012 09:56:29 -0700 (PDT) Organization: http://groups.google.com Lines: 54 Message-ID: <3e7721c2-214e-4a12-ac86-36d64b4cac41@googlegroups.com> References: <4FBF4293.4090902@krap.dk> <4FC4A5B0.2070006@wp.pl> <4FC4E8F4.9070903@krap.dk> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Complaints-To: groups-abuse@google.com In-Reply-To: <4FC4E8F4.9070903@krap.dk> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=69.28.149.29; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T User-Agent: G2/1.0 X-Received-Bytes: 2997 To: pgsql-sql@postgresql.org X-Pg-Spam-Score: -1.0 (-) X-Archive-Number: 201206/17 X-Sequence-Number: 36671 Svenne Krap wrote: > On 29-05-2012 12:32, Ireneusz Pluta wrote: > > W dniu 2012-05-25 10:28, Svenne Krap pisze: > >> Hi. > >> > >> I am building a system, where we have jobs that run at different > >> times (and takes widely different lengths of time). > >> > >> Basically I have a jobs table: > >> > >> create table jobs( > >> id serial, > >> ready boolean, > >> job_begun timestamptz, > >> job_done timestamptz, > >> primary key (id) > >> ); > >> > >> This should run by cron, at it is my intention that the cronjob > >> (basically) consists of > >> / > >> psql -c "select run_jobs()"/ > >> > >> My problem is, that the job should ensure that it is not running > >> already, which would be to set job_begun when the job starts". That > >> can easily happen as jobs should be started every 15 minutes (to > >> lower latency from ready to done) but some jobs can run for hours.. > >> > >> The problem is that a later run of run_jobs() will not see the > >> job_begun has been set by a prior run (that is unfinished - as all > >> queries from the plpgsql-function runs in a single, huge transaction). > >> > >> My intitial idea was to set the isolation level to "read uncommitted" > >> while doing the is-somebody-else-running-lookup, but I cannot change > >> that in the plpgsql function (it complains that the session has to be > >> empty - even when I have run nothing before it). > >> > >> Any ideas on how to solve the issue? > >> > >> I run it on Pgsql 9.1. > >> > >> Svenne > > > > I think you might try in your run_jobs() > > SELECT job_begun FROM jobs WHERE id = job_to_run FOR UPDATE NOWAIT; > > This in case of conflict would throw the exception: > > 55P03 could not obtain lock on row in relation "jobs" > > and you handle it (or not, which might be OK too) in EXCEPTION block. > > > Hehe.. good idea... > > In the mean time I had thought about using advisory locks for the same > thing, but the old-fashioned locks work fine too.