Skip site navigation (1) Skip section navigation (2)

Peripheral Links

Header And Logo

PostgreSQL
| The world's most advanced open source database.

Site Navigation

Search for
  Advanced Search

pgAgent job limit


  • From: "Roberts, Jon" <Jon(dot)Roberts(at)asurion(dot)com>
  • To: <pgadmin-hackers(at)postgresql(dot)org>
  • Subject: pgAgent job limit
  • Date: Tue, 26 Feb 2008 08:14:03 -0600
  • Message-id: <1A6E6D554222284AB25ABE3229A92762715658(at)nrtexcus702(dot)int(dot)asurion(dot)com>

In pgAgent.cpp, I would like to add LIMIT as shown below:

LogMessage(_("Checking for jobs to run"), LOG_DEBUG);
DBresult *res=serviceConn->Execute(
 wxT("SELECT J.jobid ")
 wxT("  FROM pgagent.pga_job J ")
 wxT(" WHERE jobenabled ")
 wxT("   AND jobagentid IS NULL ")
 wxT("   AND jobnextrun <= now() ")
 wxT("   AND (jobhostagent = '' OR jobhostagent = '") + hostname +
wxT("')")
 wxT(" ORDER BY jobnextrun")
 wxT(" LIMIT pgagent.pga_job_limit('") + hostname + wxT("')"));


This requires two new objects:
create table pgagent.pga_job_throttle (jobmax int);

insert into pgagent.pga_job_throttle values (2);

create or replace function pgagent.pga_job_limit(p_hostname varchar)
returns int as
$$
declare
  v_limit int;
  v_running int;
begin

  select jobmax
    into v_limit
    from pgagent.pga_job_throttle;

  if v_limit < 0 or v_limit is null then
      select count(*)
        into v_limit
        from pgagent.pga_job j
       where jobenabled
         and jobagentid is null
         and jobnextrun <= now()
         and (jobhostagent = '' or jobhostagent = p_hostname);
  else
      select count(*)
        into v_running
        from pgagent.pga_job j
        join pgagent.pga_joblog l
          on j.jobid = l.jlgjobid
       where j.jobenabled
         and (j.jobhostagent = '' or j.jobhostagent = p_hostname)
         and l.jlgstatus = 'r';  --Status of job: r=running,
s=successfully finished, f=failed, i=no steps to execute, d=aborted
  
      v_limit := v_limit - v_running;

  end if;

  return v_limit;
    
end;
$$
language 'plpgsql';


This function allow pgAgent to be throttled dynamically by managing the
pgagent.pga_job_throttle table.  If you want to disable all jobs from
running, you set the value to 0.  If you want to let as many jobs run at
once (like the default) to run at a time, you either delete the record
from the table or you can set the value to a negative number.

pgAgent scales much better without having excessive number of
connections to the database with one line change to the C++ code.


What do you guys think?


Jon



Home | Main Index | Thread Index

Privacy Policy | PostgreSQL Archives hosted by Command Prompt, Inc. | Designed by tinysofa
Copyright © 1996 – 2008 PostgreSQL Global Development Group