organizing cron jobs in one function - Mailing list pgsql-sql

From Louis-David Mitterrand
Subject organizing cron jobs in one function
Date
Msg-id 20121117161916.GA9492@apartia.fr
Whole thread Raw
Responses Re: organizing cron jobs in one function
List pgsql-sql
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 $$
DECLARErec record;
BEGIN
/*    update tbl1 every 15 minutes*/select name, modified from job_last_update where name='tbl1' into rec;if not found
orrec.modified + interval '15 minutes' < now() then    perform tbl1_job();    update job_last_update set modified=now()
wherename='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';endif;
 

/*    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,



pgsql-sql by date:

Previous
From: Igor Neyman
Date:
Subject: Re: How to compare two tables in PostgreSQL
Next
From: Craig Ringer
Date:
Subject: Re: organizing cron jobs in one function