开源软件联盟PostgreSQL分会

pgagent浅析

Image

概述

本文主要对pgagent功能进行简要说明,pgagent属于多线程架构,是Postgres数据库的作业调度代理,能够在复杂的时间表上运行多步骤批处理或shell脚本和SQL任务,可以运行在linux或者是windows平台。

功能概要

pgagent从整体结构上来说分为守护进程和插件,其中守护进行负责连接提供的数据库并且调度存储在数据库中的相关任务;插件则负责创建一系列与pagent功能相关的元数据表(参加数据库设计),并支持用户通过SQL方式或者pgadmin可视化方式创建调度任务。

使用场景

pgagent本质来说是一个任务调度系统,通过不同的调度策略来实现SQL和Shell脚本的任务调度,例如
1、本地或者远程调度SQL脚本,实现某些统计汇总功能,例如T+1的汇总统计等等;
2、本地或者远程调度Shell脚本,实现某些业务操作,比如通过第三方程序进行数据处理的应用。
另外pgagent可以通过jobhostagent来区分是否为当前pgagent可执行的调度任务。

名词

英文名说明
job作业
step步骤
schedule调度

安装cmake编译工具

  1. tar -zxvf cmake-3.10.0-rc1.tar.gz

  2. cd cmake-3.10.0-rc1/

  3. ./configure --prefix=/opt/cmake3.10

  4. gmake

  5. gmake install

  6. cmake --version

  7. cmake version 3.10.0

  8. CMake suite maintained and supported byKitware(kitware.com/cmake).

安装wxGTK

  1. yum -y install gtk2-devel binutils-devel

  2. wget https://excellmedia.dl.sourceforge.net/project/wxwindows/2.8.7/wxGTK-2.8.7.tar.gz --no-check-certificate

  3. tar -zxvf wxGTK-2.8.7.tar

  4. cd wxGTK-2.8.7/

修改文件

  1. wxwidgets的/src/gtk/gsockgtk.cpp

  2. #defineGSocketGlibGSocket//add

  3. #include<gdk/gdk.h>

  4. #include<glib.h>

  5. #undefGSocket//add

编译安装

  1. ./configure --enable-shared=no --enable-unicode=yes --prefix=/opt/wxGTK-2.8.7

  2. make && make install

下载boost

  1. wget https://boostorg.jfrog.io/artifactory/main/release/1.82.0/source/boost_1_82_0.tar.gz

  2. $tar -xf boost_1_82_0.tar.gz

  3. $cd boost_1_82_0

  4. $./bootstrap.sh

  5. $./b2 --without-python stage debug

拷贝文件

将编译产生的boost目录拷贝至/usr/include/,将stage/lib/下的所有文件拷贝至/usr/lib64/下,如下:

  1. $cp -rf boost /usr/include

  2. $cp -rf stage/lib/* /usr/lib64

配置环境变量

  1. vi ~/.bashrc

  2. export PATH=/opt/wxGTK-2.8.7/bin:/opt/postgresql/bin:$PATH

  3. export LD_LIBRARY_PATH=/opt/wxGTK-2.8.7/lib:/opt/postgresql/lib:$LD_LIBRARY_PATH

  4. source ~/.bashrc

编译

执行 cmake ./

  1. [wangzhibin@highgo pgAgent-4.0.0-Source]$ cmake ./

  2. --The C compiler identification is GNU 4.8.5

  3. --The CXX compiler identification is GNU 4.8.5

  4. --Checkfor working C compiler:/bin/cc

  5. --Checkfor working C compiler:/bin/cc -- works

  6. --Detecting C compiler ABI info

  7. --Detecting C compiler ABI info -done

  8. --Detecting C compile features

  9. --Detecting C compile features -done

  10. --Checkfor working CXX compiler:/bin/c++

  11. --Checkfor working CXX compiler:/bin/c++-- works

  12. --Detecting CXX compiler ABI info

  13. --Detecting CXX compiler ABI info -done

  14. --Detecting CXX compile features

  15. --Detecting CXX compile features -done

  16. --Boost found.

  17. --FoundBoost components:

  18. filesystem;regex;date_time;thread;system

  19. --

  20. --================================================================================

  21. --Configuration summary:

  22. --

  23. --Project: pgagent

  24. --Description: pgAgent is a job scheduling engine forPostgreSQL

  25. --Version:4.0.0

  26. --

  27. --PostgreSQL version string:PostgreSQL12.15

  28. --PostgreSQL version parts :12

  29. --PostgreSQL path :/opt/postgresql

  30. --PostgreSQL config binary :/opt/postgresql/bin/pg_config

  31. --PostgreSQL include path :/opt/postgresql/include

  32. --PostgreSQL library path :/opt/postgresql/lib

  33. --PostgreSQL share path :/opt/postgresql/share/postgresql

  34. --

  35. --Boost version :1.82.0

  36. --Boost path :/

  37. --Boost include directory :/

  38. --Boost library directory :

  39. --BoostStatic linking : NO

  40. --================================================================================

  41. --

  42. --Configuringdone

  43. --Generatingdone

  44. --Build files have been written to:/home/wangzhibin/pgAgent-4.0.0-Source

然后编译安装 make && make install即可

使用

操作系统

版本:centos7

软件

数据库:Postgresql 12.15
pgagent:4.0.0

初始化

  1. [wangzhibin@highgo ~]$ psql -U postgres -d test

  2. psql (12.15)

  3. Type"help"for help.

  4. test=# create extension pgagent ;

  5. CREATE EXTENSION

  6. test=# \dn

  7. List of schemas

  8. Name|Owner

  9. ---------+----------

  10. pgagent | postgres

  11. public| postgres

  12. (2 rows)

  13. test=# \q

  14. [wangzhibin@highgo ~]$ pgagent hostaddr=127.0.0.1 dbname=test user=postgres

  15. [wangzhibin@highgo ~]$ pgagent --help

  16. PostgreSQLSchedulingAgent

  17. Version:4.0.0

  18. Usage:

  19. pgagent [options]<connect-string>

  20. options:

  21. -v (display version info andthenexit)

  22. -f run in the foreground (donot detach from the terminal)

  23. -t <poll time interval in seconds (default10)>

  24. -r <retry period after connection abort in seconds (>=10,default30)>

  25. -s <log file (messages are logged to STDOUT ifnot specified>

  26. -l <logging verbosity (ERROR=0, WARNING=1, DEBUG=2,default0)>

脚本创建

脚本

通过脚本依次创建job、step、schedule,以下示例包括两个类型的的任务:
1、SQL
2、Batch

  1. do $$

  2. declare

  3. job_id int;

  4. begin

  5. /* add a job and get its id: */

  6. insert into

  7. pgagent.pga_job (jobjclid, jobname)

  8. values

  9. (1/*1=Routine Maintenance*/,'my job name')

  10. returning

  11. jobid

  12. into

  13. job_id;

  14. /* add a step to the job: */

  15. insert into

  16. pgagent.pga_jobstep (jstjobid, jstname, jstkind, jstcode, jstdbname)

  17. values

  18. (

  19. job_id,

  20. 'my step name',

  21. 's',/* sql step */

  22. 'insert into public.test (id) values(random()*(25-10)+10) ',/* the sql to run */

  23. 'test'/* the name of the database to run the step against */

  24. );

  25. /* add a schedule to the job. This one runs every minute: */

  26. insert into

  27. pgagent.pga_schedule (jscjobid, jscname)

  28. values

  29. (job_id,'my schedule name');

  30. end $$;

batch

  1. DO $$

  2. DECLARE

  3. jid integer;

  4. scid integer;

  5. BEGIN

  6. --Creating a new job

  7. INSERT INTO pgagent.pga_job(

  8. jobjclid, jobname, jobdesc, jobhostagent, jobenabled

  9. ) VALUES (

  10. 1::integer,'file'::text,''::text,''::text,true

  11. ) RETURNING jobid INTO jid;

  12. --Steps

  13. --Inserting a step (jobid: NULL)

  14. INSERT INTO pgagent.pga_jobstep (

  15. jstjobid, jstname, jstenabled, jstkind,

  16. jstconnstr, jstdbname, jstonerror,

  17. jstcode, jstdesc

  18. ) VALUES (

  19. jid,'step'::text,true,'b'::character(1),

  20. ''::text,''::name,'f'::character(1),

  21. E'e:\\1.bat'::text,''::text

  22. );

  23. --Schedules

  24. --Inserting a schedule

  25. INSERT INTO pgagent.pga_schedule(

  26. jscjobid, jscname, jscdesc, jscenabled,

  27. jscstart, jscend, jscminutes, jschours, jscweekdays, jscmonthdays, jscmonths

  28. ) VALUES (

  29. jid,'schedule'::text,''::text,true,

  30. '2023-08-11 12:11:00+08'::timestamp with time zone,'2024-08-11 12:11:00+08'::timestamp with time zone,

  31. --Minutes

  32. '{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,t,t,t,t,t,t,t,t,t}'::bool[]::boolean[],

  33. --Hours

  34. '{f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f}'::bool[]::boolean[],

  35. --Week days

  36. '{f,f,f,f,f,f,f}'::bool[]::boolean[],

  37. --Month days

  38. '{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}'::bool[]::boolean[],

  39. --Months

  40. '{f,f,f,f,f,f,f,f,f,f,f,f}'::bool[]::boolean[]

  41. ) RETURNING jscid INTO scid;

  42. END

  43. $$;

数据库设计

表结构

  1. pga_exception : 记录作业执行异常信息

  2. pga_job: 作业定义的基本信息,作业起止时间,最后运行时间等

  • jobagentid :当前执行作业代理

  • pga_jobagent:pgAgent的配置信息,服务器上pgAgent的地址和启动时间

  • pga_jobclass: pgAgent的配置信息,定义作业类型

  • pga_joblog:每个作业的运行日志,包含启动时间、执行时长。

    • jlgstatus: 作业状态 r=running, s=successfully finished, f=failed, i=no steps to execute, d=aborted

  • pga_jobstep:每个job步骤的定义在这个表里。

    • jstkind:步骤类型 s=sql, b=batch

    • jstonerror:步骤返回状态: f=fail the job, s=mark step as succeeded and continue, i=mark as fail but ignore it and proceed

  • pga_jobsteplog:每个job步骤的执行日志,包含步骤的开始时间,执行时长。

    • jslstatus :作业步骤状态: r=running, s=successfully finished, f=failed stopping job, i=ignored failure, d=aborted

    • jslresult :返回作业步骤编码

  • pga_schedule:job调度的定义在这个表里。

  • Image

    函数

    1. FUNCTION pgagent.pgagent_schema_version:获取当前插件版本

    2. FUNCTION pgagent.pga_next_schedule :计算下一次运行时间

    3. FUNCTION pgagent.pga_is_leap_year :计算是否为闰年

    4. FUNCTION pgagent.pga_job_trigger:更新作业的下一次调度时间

    5. FUNCTION pgagent.pga_schedule_trigger:当调度发生变化时,更新下一次运行时间

    6. FUNCTION pgagent.pga_exception_trigger:当出现异常时,更新下一次运行时间

    触发器

    1. TRIGGER pga_job_trigger:更新作业的下一次运行时间,结合pga_job_trigger使用

    2. TRIGGER pga_schedule_trigger:当调度发生变化时,更新下一次运行时间,结合pga_schedule_trigger使用

    3. TRIGGER pga_exception_trigger:当出现异常时,更新下一次运行时间,结合使用

    调用

    windows

    1. pgagent.exe DEBUG hostaddr=127.0.0.1 dbname=test user=postgres port=5432 password=123456

    linux

    1. pgagent hostaddr=127.0.0.1 dbname=test user=postgres

    示例

    在我本机的postgres和test库上分别创建了pgagent,并且执行了两个pgagent程序,如图:
    1、连接postgres数据库
    Image
    2、连接test数据库(区别于第一个,pgagent启用了debug参数)
    Image
    3、数据库可以查看到两个pgagent的运行状态
    Image

    ImageImage