【问题标题】:how to schedule a pgagent job through scripts/commandLine如何通过脚本/命令行安排 pgagent 作业
【发布时间】:2018-07-16 20:40:17
【问题描述】:

我想通过 PgAgent 运行作业。

我可以通过 PgAdmin UI 在 PgAgentJob 中创建一个作业来做到这一点,如此处所述https://www.pgadmin.org/docs/pgadmin4/dev/pgagent_jobs.html

但我想使用一个可以创建 PgAgent 作业的 sql 脚本,就像我们在 Oracle 中所做的那样。请建议我如何实现这一目标。

【问题讨论】:

  • 请分享你目前拥有的SQL代码。
  • 从测试中删除 user_name='test';是我每天都想跑的东西

标签: postgresql scheduler pgagent


【解决方案1】:

要在 pgAgent 中创建作业,请使用类似以下的 INSERT STATEMENTS(对于 Routine Maintenance 作业):

INSERT INTO pgagent.pga_job (jobid, jobjclid, jobname, jobdesc, jobenabled, jobhostagent)
SELECT jcl.jclid, 'MyJob', '', true, ''
FROM pgagent.pga_jobclass jcl WHERE jclname='Routine Maintenance';

要向此作业添加一个执行 SQL 命令 ('delete from test where user_name=''test'';) 的步骤,请使用以下命令:

INSERT INTO pgagent.pga_jobstep (jstjobid, jstname, jstdesc, jstenabled, jstkind, jstonerror, jstcode, jstdbname, jstconnstr)
 SELECT (SELECT jobid 
         FROM pgagent.pga_job
         WHERE jobname = 'MyJob'), 'MyStep', '', true, 's', 'f', 'delete from test where user_name=''test'';', 'postgres', '';

要为该作业创建计划(每天 08:45),请使用以下命令:

INSERT INTO pgagent.pga_schedule (jscjobid, jscname, jscdesc, jscminutes, jschours, jscweekdays, jscmonthdays, jscmonths, jscenabled, jscstart, jscend)
VALUES((SELECT jobid 
FROM pgagent.pga_job
WHERE jobname = 'MyJob'), 'MySchedule', '', '{f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,t,f,f,f,f,f,f,f,f,f,f,f,f,f,f}', 
'{f,f,f,f,f,f,f,f,t,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f}', '{t,t,t,t,t,t,t}', '{t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t}', 
'{t,t,t,t,t,t,t,t,t,t,t,t}', true, '2018-07-16 00:00:00', NULL);

这里是这个时间表的图形表示:

您可以在此处查看匿名块(由 pgAdmin 生成)内这些命令的完整摘要:

DO $$
DECLARE
    jid integer;
    scid integer;
BEGIN
-- Creating a new job
INSERT INTO pgagent.pga_job(
    jobjclid, jobname, jobdesc, jobhostagent, jobenabled
) VALUES (
    1::integer, 'MyJob'::text, ''::text, ''::text, true
) RETURNING jobid INTO jid;

-- Steps
-- Inserting a step (jobid: NULL)
INSERT INTO pgagent.pga_jobstep (
    jstjobid, jstname, jstenabled, jstkind,
    jstconnstr, jstdbname, jstonerror,
    jstcode, jstdesc
) VALUES (
    jid, 'MyStep'::text, true, 's'::character(1),
    ''::text, 'postgres'::name, 'f'::character(1),
    'delete from test where user_name=''test'';'::text, ''::text
) ;

-- Schedules
-- Inserting a schedule
INSERT INTO pgagent.pga_schedule(
    jscjobid, jscname, jscdesc, jscenabled,
    jscstart,     jscminutes, jschours, jscweekdays, jscmonthdays, jscmonths
) VALUES (
    jid, 'MySchedule'::text, ''::text, true,
    '2018-07-16 00:00:00+02'::timestamp with time zone, 
    -- Minutes
    ARRAY[false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false]::boolean[],
    -- Hours
    ARRAY[false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false]::boolean[],
    -- Week days
    ARRAY[true, true, true, true, true, true, true]::boolean[],
    -- Month days
    ARRAY[true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true]::boolean[],
    -- Months
    ARRAY[true, true, true, true, true, true, true, true, true, true, true, true]::boolean[]
) RETURNING jscid INTO scid;
END
$$;

【讨论】:

  • 记录已插入 pgagent.pga_job、pgagent.pga_jobstep 和 pgagent.pga_schedule 表中,但作业未按计划时间执行。
  • pgagent 进程是否正在运行?
  • 可以手动启动吗?只是为了确定问题出在哪里
  • 我给的数据库名称是错误的。 Atfer 提供正确的数据库名称 Job 工作正常。
  • @JimJones 我怎么知道 pgagent 进程正在运行?我可以手动运行它吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-16
  • 1970-01-01
  • 2014-04-27
相关资源
最近更新 更多