pgagent浅析
概述
本文主要对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编译工具
tar -zxvf cmake-3.10.0-rc1.tar.gz
cd cmake-3.10.0-rc1/
./configure --prefix=/opt/cmake3.10
gmake
gmake install
cmake --version
cmake version 3.10.0
CMake suite maintained and supported byKitware(kitware.com/cmake).
安装wxGTK
yum -y install gtk2-devel binutils-devel
wget https://excellmedia.dl.sourceforge.net/project/wxwindows/2.8.7/wxGTK-2.8.7.tar.gz --no-check-certificate
tar -zxvf wxGTK-2.8.7.tar
cd wxGTK-2.8.7/
修改文件
wxwidgets的/src/gtk/gsockgtk.cpp
#defineGSocketGlibGSocket//add
#include<gdk/gdk.h>
#include<glib.h>
#undefGSocket//add
编译安装
./configure --enable-shared=no --enable-unicode=yes --prefix=/opt/wxGTK-2.8.7
make && make install
下载boost
wget https://boostorg.jfrog.io/artifactory/main/release/1.82.0/source/boost_1_82_0.tar.gz
$tar -xf boost_1_82_0.tar.gz
$cd boost_1_82_0
$./bootstrap.sh
$./b2 --without-python stage debug
拷贝文件
将编译产生的boost目录拷贝至/usr/include/,将stage/lib/下的所有文件拷贝至/usr/lib64/下,如下:
$cp -rf boost /usr/include
$cp -rf stage/lib/* /usr/lib64
配置环境变量
vi ~/.bashrc
export PATH=/opt/wxGTK-2.8.7/bin:/opt/postgresql/bin:$PATH
export LD_LIBRARY_PATH=/opt/wxGTK-2.8.7/lib:/opt/postgresql/lib:$LD_LIBRARY_PATH
source ~/.bashrc
编译
执行 cmake ./
[wangzhibin@highgo pgAgent-4.0.0-Source]$ cmake ./
--The C compiler identification is GNU 4.8.5
--The CXX compiler identification is GNU 4.8.5
--Checkfor working C compiler:/bin/cc
--Checkfor working C compiler:/bin/cc -- works
--Detecting C compiler ABI info
--Detecting C compiler ABI info -done
--Detecting C compile features
--Detecting C compile features -done
--Checkfor working CXX compiler:/bin/c++
--Checkfor working CXX compiler:/bin/c++-- works
--Detecting CXX compiler ABI info
--Detecting CXX compiler ABI info -done
--Detecting CXX compile features
--Detecting CXX compile features -done
--Boost found.
--FoundBoost components:
filesystem;regex;date_time;thread;system
--
--================================================================================
--Configuration summary:
--
--Project: pgagent
--Description: pgAgent is a job scheduling engine forPostgreSQL
--Version:4.0.0
--
--PostgreSQL version string:PostgreSQL12.15
--PostgreSQL version parts :12
--PostgreSQL path :/opt/postgresql
--PostgreSQL config binary :/opt/postgresql/bin/pg_config
--PostgreSQL include path :/opt/postgresql/include
--PostgreSQL library path :/opt/postgresql/lib
--PostgreSQL share path :/opt/postgresql/share/postgresql
--
--Boost version :1.82.0
--Boost path :/
--Boost include directory :/
--Boost library directory :
--BoostStatic linking : NO
--================================================================================
--
--Configuringdone
--Generatingdone
--Build files have been written to:/home/wangzhibin/pgAgent-4.0.0-Source
然后编译安装 make && make install即可
使用
操作系统
版本:centos7
软件
数据库:Postgresql 12.15
pgagent:4.0.0
初始化
[wangzhibin@highgo ~]$ psql -U postgres -d test
psql (12.15)
Type"help"for help.
test=# create extension pgagent ;
CREATE EXTENSION
test=# \dn
List of schemas
Name|Owner
---------+----------
pgagent | postgres
public| postgres
(2 rows)
test=# \q
[wangzhibin@highgo ~]$ pgagent hostaddr=127.0.0.1 dbname=test user=postgres
[wangzhibin@highgo ~]$ pgagent --help
PostgreSQLSchedulingAgent
Version:4.0.0
Usage:
pgagent [options]<connect-string>
options:
-v (display version info andthenexit)
-f run in the foreground (donot detach from the terminal)
-t <poll time interval in seconds (default10)>
-r <retry period after connection abort in seconds (>=10,default30)>
-s <log file (messages are logged to STDOUT ifnot specified>
-l <logging verbosity (ERROR=0, WARNING=1, DEBUG=2,default0)>
脚本创建
脚本
通过脚本依次创建job、step、schedule,以下示例包括两个类型的的任务:
1、SQL
2、Batch
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 */
'insert into public.test (id) values(random()*(25-10)+10) ',/* the sql to run */
'test'/* 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 $$;
batch
DO $$
DECLARE
jid integer;
scid integer;
BEGIN
--Creating a new job
INSERT INTO pgagent.pga_job(
jobjclid, jobname, jobdesc, jobhostagent, jobenabled
) VALUES (
1::integer,'file'::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,'step'::text,true,'b'::character(1),
''::text,''::name,'f'::character(1),
E'e:\\1.bat'::text,''::text
);
--Schedules
--Inserting a schedule
INSERT INTO pgagent.pga_schedule(
jscjobid, jscname, jscdesc, jscenabled,
jscstart, jscend, jscminutes, jschours, jscweekdays, jscmonthdays, jscmonths
) VALUES (
jid,'schedule'::text,''::text,true,
'2023-08-11 12:11:00+08'::timestamp with time zone,'2024-08-11 12:11:00+08'::timestamp with time zone,
--Minutes
'{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[],
--Hours
'{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[],
--Week days
'{f,f,f,f,f,f,f}'::bool[]::boolean[],
--Month days
'{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[],
--Months
'{f,f,f,f,f,f,f,f,f,f,f,f}'::bool[]::boolean[]
) RETURNING jscid INTO scid;
END
$$;
数据库设计
表结构
pga_exception : 记录作业执行异常信息
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调度的定义在这个表里。
函数
FUNCTION pgagent.pgagent_schema_version:获取当前插件版本
FUNCTION pgagent.pga_next_schedule :计算下一次运行时间
FUNCTION pgagent.pga_is_leap_year :计算是否为闰年
FUNCTION pgagent.pga_job_trigger:更新作业的下一次调度时间
FUNCTION pgagent.pga_schedule_trigger:当调度发生变化时,更新下一次运行时间
FUNCTION pgagent.pga_exception_trigger:当出现异常时,更新下一次运行时间
触发器
TRIGGER pga_job_trigger:更新作业的下一次运行时间,结合pga_job_trigger使用
TRIGGER pga_schedule_trigger:当调度发生变化时,更新下一次运行时间,结合pga_schedule_trigger使用
TRIGGER pga_exception_trigger:当出现异常时,更新下一次运行时间,结合使用
调用
windows
pgagent.exe DEBUG hostaddr=127.0.0.1 dbname=test user=postgres port=5432 password=123456
linux
pgagent hostaddr=127.0.0.1 dbname=test user=postgres
示例
在我本机的postgres和test库上分别创建了pgagent,并且执行了两个pgagent程序,如图:
1、连接postgres数据库
2、连接test数据库(区别于第一个,pgagent启用了debug参数)
3、数据库可以查看到两个pgagent的运行状态