2010-12-18 80 views
4

是否有一种方法可以以编程方式创建和管理pgagent中的作业/计划,即不使用pgAdmin?使用pgagent以编程方式创建作业和计划

我怀疑有可能通过使用libpq编写postgres客户端来实现这一点(如果pgagent不支持这种开箱即用的行为) - 但我不知道如何去做 - 如果我需要去写作我自己的API的作业/时间表CRUD功能。

所以基本上我问两个问题:

  • 有没有一种方法来创建/管理任务和计划在PAGENT编程?
  • 如果不是,对于上述问题,我需要勾选哪些部分的pagagent代码才能提供我自己的工作/计划CRUD功能?

回答

2

pgAdmin只是创建一些SQL语句,就是这样。任何可以连接到数据库“postgres”并具有使用pgAgent模式和表的权限的应用程序都可以管理pgAgent的作业和时间表。这只是SQL。

3

下面将创建一个运行的每一分钟,与调用一些SQL的一个步骤的工作:

do $$ 
declare 
    job_id int; 
begin 

    /* add a job and get its id: */ 
    insert into 
     pgagent.pga_job (jobjclid, jobname) 
    values 
     (1 /*1=Routine Maintenance*/, 'my job name') 
    returning 
     jobid 
    into 
     job_id; 


    /* add a step to the job: */ 
    insert into 
     pgagent.pga_jobstep (jstjobid, jstname, jstkind, jstcode, jstdbname) 
    values 
     (
      job_id, 
      'my step name', 
      's',     /* sql step */ 
      'select * from thing', /* the sql to run */ 
      'mydb'     /* the name of the database to run the step against */ 
     ); 


    /* add a schedule to the job. This one runs every minute: */ 
    insert into 
     pgagent.pga_schedule (jscjobid, jscname) 
    values 
     (job_id, 'my schedule name'); 

end $$; 
相关问题