2010-12-18 78 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 $$; 
相關問題