Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TZl73-0006MZ-Ny for pgsql-sql@postgresql.org; Sat, 17 Nov 2012 16:19:21 +0000 Received: from marius.cruisefish.net ([178.63.80.84]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TZl71-0004md-Oe for pgsql-sql@postgresql.org; Sat, 17 Nov 2012 16:19:20 +0000 Received: by marius.cruisefish.net (Postfix, from userid 1000) id B1C66813458FA; Sat, 17 Nov 2012 17:19:17 +0100 (CET) Date: Sat, 17 Nov 2012 17:19:17 +0100 From: Louis-David Mitterrand To: pgsql-sql@postgresql.org Subject: organizing cron jobs in one function Message-ID: <20121117161916.GA9492@apartia.fr> Mail-Followup-To: pgsql-sql@postgresql.org MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201211/14 X-Sequence-Number: 36947 Hi, I'm planning to centralize all db maintenance jobs from a single pl/pgsql function called by cron every 15 minutes (highest frequency required by a list of jobs). In pseudo code: CREATE or replace FUNCTION cron_jobs() RETURNS void LANGUAGE plpgsql AS $$ DECLARE rec record; BEGIN /* update tbl1 every 15 minutes*/ select name, modified from job_last_update where name='tbl1' into rec; if not found or rec.modified + interval '15 minutes' < now() then perform tbl1_job(); update job_last_update set modified=now() where name='tbl1'; end if; /* update tbl2 every 2 hours */ select name, modified from job_last_update where name='tbl2' into rec; if not found or rec.modified + interval '2 hours' < now() then perform tbl2_job(); update job_last_update set modified=now() where name='tbl2'; end if; /* etc, etc.*/ END; $$; The 'job_last_update' table holds the last time a job was completed. Is this a good way to do it? Thanks,