Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id C6A03FDFFE8 for ; Tue, 29 May 2012 07:32:34 -0300 (ADT) Received: from mx3.wp.pl ([212.77.101.7]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SZJj6-0003eY-SQ for pgsql-sql@postgresql.org; Tue, 29 May 2012 10:32:34 +0000 Received: (wp-smtpd smtp.wp.pl 4560 invoked from network); 29 May 2012 12:32:18 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wp.pl; s=1024a; t=1338287538; bh=6jrL3dFnxltlnh1mlHlUBOy2fB5ifWJJlvoME6mhbj4=; h=From:To:CC:Subject; b=KTgXDZSM3LiH/tZX/mkbTL4DlbBwnIQuEILlP8vM/WkUUlapxMjjSagfRAvUKLekg UfgqKuffJbj0Wkh/Ijtv66vuODC2IHJQcq+oc0ZkM3LsrpSqNE/ImXn/dpsBhAlbxM hqYnW1ypr0XJC7Zv1JE7S5RwFf42np/TSUo3sDbk= Received: from 82-210-167-137.home.aster.pl (HELO [192.168.1.144]) (ipluta@[82.210.167.137]) (envelope-sender ) by smtp.wp.pl (WP-SMTPD) with AES256-SHA encrypted SMTP for ; 29 May 2012 12:32:18 +0200 Message-ID: <4FC4A5B0.2070006@wp.pl> Date: Tue, 29 May 2012 12:32:16 +0200 From: Ireneusz Pluta User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20120428 Thunderbird/12.0.1 MIME-Version: 1.0 To: Svenne Krap CC: "pgsql-sql@postgresql.org" Subject: Re: Job control in sql References: <4FBF4293.4090902@krap.dk> In-Reply-To: <4FBF4293.4090902@krap.dk> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-WP-AV: skaner antywirusowy poczty Wirtualnej Polski S. A. X-WP-SPAM: NO 0000000 [AUGA] X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201205/100 X-Sequence-Number: 36648 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.