Skip to content

Commit 77a3295

Browse files
committed
new doc
1 parent e0d153c commit 77a3295

File tree

7 files changed

+1534
-1211
lines changed

7 files changed

+1534
-1211
lines changed

201305/20130531_01.md

Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
## PostgreSQL Oracle 兼容性之 - JOB - Daily Maintenance - Timing Tasks(pgagent)
2+
3+
### 作者
4+
digoal
5+
6+
### 日期
7+
2013-05-31
8+
9+
### 标签
10+
PostgreSQL , pgagent , Oracle兼容性 , job , dbms_jobs
11+
12+
----
13+
14+
## 背景
15+
在PostgreSQL中跑JOB任务的方法。
16+
17+
## 定时任务管理
18+
PostgreSQL 定时任务可以通过操作系统的定时任务功能来实现, 例如Linux 的crontab.
19+
20+
也可以通过pgagent插件来实现.
21+
22+
pgagent的调度信息存储在PostgreSQL数据库的postgres库中, 可以通过pgadmin可以在图形化界面来配置调度表.
23+
24+
pgagent是一个后台进程, 时刻读取配置好的调度信息, 根据调度信息执行job, 并记录执行日志.
25+
26+
一般来说建议将pgagent安装在本地数据库服务器中, 调度信息也存放在本地数据库的postgres库中, 那么在执行job时, pgagent连接本地数据库即可. 否则的话还需要配置目标库的密码, 建议用.pgpass来配置, 如果配置在命令行中容易被ps进程发现.
27+
28+
## pgagent的安装
29+
首先下载pgagent :
30+
31+
http://git.postgresql.org/gitweb/?p=pgagent.git;a=summary
32+
33+
34+
阅读README, 编译pgagent需要先安装以下软件 :
35+
36+
```
37+
- A C/C++ compiler, such as GCC or Microsoft Visual C++ on Windows.
38+
- CMake 2.6 (from www.cmake.org)
39+
- A wxWidgets 2.8.x installation, configured per the requirements for
40+
pgAdmin:
41+
http://git.postgresql.org/gitweb/?p=pgadmin3.git;a=blob_plain;f=INSTALL;hb=HEAD
42+
- A PostgreSQL 8.3 or higher installation
43+
```
44+
45+
安装gcc
46+
47+
```
48+
rpm -ivh gcc-4.1.2-51.el5
49+
```
50+
51+
安装cmake
52+
53+
```
54+
tar -zxvf cmake-2.8.8.tar.gz
55+
cd cmake-2.8.8
56+
./bootstrap --prefix=/opt/cmake2.8.8
57+
gmake
58+
gmake install
59+
```
60+
61+
安装好后将cmake的bin目录放到PATH变量.
62+
63+
```
64+
vi ~/.bash_profile
65+
export PATH=/opt/cmake2.8.8/bin:$PATH
66+
. ~/.bash_profile
67+
```
68+
69+
安装wxWidgets
70+
71+
```
72+
wget http://prdownloads.sourceforge.net/wxwindows/wxGTK-2.8.12.tar.gz
73+
tar -zxvf wxGTK-2.8.12.tar.gz
74+
cd wxGTK-2.8.12
75+
./configure --enable-shared=no --enable-unicode=yes --prefix=/opt/wxGTK-2.8.12
76+
make
77+
make install
78+
```
79+
80+
将bin加入PATH
81+
82+
```
83+
vi ~/.bash_profile
84+
export PATH=/opt/wxGTK-2.8.12/bin:$PATH
85+
export LD_LIBRARY_PATH=/opt/wxGTK-2.8.12/lib:$LD_LIBRARY_PATH
86+
. ~/.bash_profile
87+
```
88+
89+
安装PostgreSQL, 略.
90+
91+
PostgreSQL 10 on CentOS 6.x x64
92+
93+
```
94+
yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
95+
yum install -y https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-6-x86_64/pgdg-centos10-10-2.noarch.rpm
96+
yum install -y postgresql10*
97+
```
98+
99+
PostgreSQL 10 on CentOS 7.x x64
100+
101+
```
102+
yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
103+
yum install -y https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm
104+
yum install -y postgresql10*
105+
```
106+
107+
将pg_config加入PATH
108+
109+
```
110+
vi ~/.bash_profile
111+
export PATH=/opt/pgsql9.3/bin:$PATH
112+
. ~/.bash_profile
113+
```
114+
115+
安装pgagent
116+
117+
```
118+
tar -zxvf pgagent-d6c5a80.tar.gz
119+
cd pgagent-d6c5a80
120+
ccmake ./
121+
生成配置
122+
CMAKE_BUILD_TYPE
123+
CMAKE_INSTALL_PREFIX 修改为此项 /opt/pgagent
124+
PG_CONFIG_PATH /opt/pgsql9.3/bin/pg_config
125+
STATIC_BUILD ON
126+
WX_CONFIG_PATH /opt/wxGTK-2.8.12/bin/wx-config
127+
WX_DEBUG OFF
128+
129+
按c配置,然后按g产生配置
130+
131+
接下来就可以编译安装了
132+
133+
make
134+
make install
135+
```
136+
137+
配置.bash_profile
138+
139+
```
140+
vi ~/.bash_profile
141+
export PATH=/opt/pgagent/bin:$PATH
142+
. ~/.bash_profile
143+
```
144+
145+
在本地库的postgres库中创建调度信息, 表, 函数 :
146+
147+
```
148+
psql -h 127.0.0.1 -p 1999 -U postgres -d postgres -f /opt/pgagent/share/pgagent.sql
149+
```
150+
151+
安装好后, 使用pgadmin连接到本地库就可以看到jobs选项 :
152+
153+
![pic](20130531_01_pic_001.png)
154+
155+
## pgagent配置举例
156+
启动pgagent.
157+
158+
pgagent是一个后台进程, 时刻要去读取存储在数据库中的调度信息以执行定时任务. 所以首先要启动pgagent.
159+
160+
```
161+
[root@db-172-16-3-33 ~]# pgagent --help
162+
Usage:
163+
pgagent [options] <connect-string>
164+
options:
165+
-f run in the foreground (do not detach from the terminal)
166+
-t <poll time interval in seconds (default 10)>
167+
-r <retry period after connection abort in seconds (>=10, default 30)>
168+
-s <log file (messages are logged to STDOUT if not specified>
169+
-l <logging verbosity (ERROR=0, WARNING=1, DEBUG=2, default 0)>
170+
```
171+
172+
例如, -t 1表示每秒读取一次调度数据 :
173+
174+
```
175+
pgagent -t 1 -l 2 -s /var/log/pgagent.log hostaddr=127.0.0.1 port=1999 dbname=postgres user=postgres
176+
```
177+
178+
不要把password配置在命令行中, 尽量配置在密码文件中 :
179+
180+
```
181+
vi ~/.pgpass
182+
127.0.0.1:1999:postgres:postgres:postgres
183+
chmod 400 ~/.pgpass
184+
```
185+
186+
启动好pgagent后就可以配置job了.
187+
188+
## 案例1 :
189+
需求 : 在本地数据库中定时执行sql
190+
191+
job配置
192+
193+
1\. 在pgadmin的jobs上右键新建job.
194+
195+
2\. 在属性栏配置job name, 例如j1.
196+
197+
3\. 在steps中新建并配置step name, 本地库名;
198+
199+
在定义栏配置要执行的sql :
200+
201+
```
202+
create table if not exists test_sum(sdate date, cnt int8);
203+
insert into test_sum select current_date,count(*) from pg_class group by current_date;
204+
```
205+
206+
可以配置多个step, 执行时按名称以及step id排序执行 :
207+
208+
```
209+
// job.cpp - pgAgent job
210+
211+
int Job::Execute()
212+
{
213+
int rc = 0;
214+
bool succeeded = false;
215+
DBresult *steps = threadConn->Execute(
216+
wxT("SELECT * ")
217+
wxT(" FROM pgagent.pga_jobstep ")
218+
wxT(" WHERE jstenabled ")
219+
wxT(" AND jstjobid=") + jobid +
220+
wxT(" ORDER BY jstname, jstid"));
221+
```
222+
223+
4\. 在schedule栏中新建并配置调度名, 开始时间, 如果有结束时间的话还需要配置结束时间.
224+
225+
days中可以配置周, 月, 日
226+
227+
times中可以配置小时, 分钟.
228+
229+
exceptions中配置排他.
230+
231+
配置完后, 在steps以及jos的统计信息栏可以看到每次JOB或者step执行的情况.
232+
233+
## 案例2 :
234+
需求 : 定时执行脚本(batch)
235+
236+
脚本配置
237+
238+
```
239+
[root@db-172-16-3-33 ~]# cat /root/b.sh
240+
#!/bin/bash
241+
psql -h 127.0.0.1 -p 1999 -U postgres postgres <<EOF
242+
create table if not exists t(id int);
243+
insert into t values (1);
244+
EOF
245+
exit $?
246+
chmod 500 b.sh
247+
```
248+
249+
以上假设pgagent是以root用户执行的. 否则要考虑脚本的可执行权限.
250+
251+
job配置, 同上
252+
253+
schedule配置, 同上
254+
255+
step配置, 选择batch, 不需要选择数据库. 定义中输入/root/b.sh .其他同上.
256+
257+
## 案例3 :
258+
需求 : 在远程数据库中定时执行SQL
259+
260+
job配置, 同上.
261+
262+
schedule配置, 同上.
263+
264+
step配置, 需要输入远程数据库的连接信息. 密码配置在conn string中. 所以不安全.
265+
266+
例如 :
267+
268+
```
269+
hostaddr=172.16.3.150 port=1000 user=postgres dbname=postgres password=pwd
270+
```
271+
272+
这里支持.pgpass. 所以建议不要使用远程定时任务.
273+
274+
连接信息参考 :
275+
276+
http://www.postgresql.org/docs/devel/static/libpq-connect.html
277+
278+
## 参考
279+
1\. http://www.postgresql.org/docs/devel/static/libpq-connect.html
280+
281+
2\. http://pgadmin.org/

201305/20130531_01_pic_001.png

23.3 KB
Loading

201305/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### 文章列表
22
----
3+
##### 20130531_01.md [《PostgreSQL Oracle 兼容性之 - JOB - Daily Maintenance - Timing Tasks(pgagent)》](20130531_01.md)
34
##### 20130527_01.md [《PostgreSQL backup and recovery - online backup & Point-In-Time-Recovery》](20130527_01.md)
45
##### 20130523_01.md [《PostgreSQL performance test use ssh tunnel》](20130523_01.md)
56
##### 20130522_02.md [《PostgreSQL ssl ciphers performance 比较》](20130522_02.md)

201711/20171125_01.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,13 @@ PostgreSQL , 生命周期管理
4646
## 健康报告
4747
## 诊断报告
4848
## 优化报告
49+
## 数据库管理规范
50+
## 数据库开发规范
4951
## 应急响应管理
5052
## 节假日管理
5153
## 节假日复盘
54+
## 如何与用户社区互动
55+
## 如何与开发者社区互动
5256
## 开发实践
5357
## 案例实践
5458
## 优化实践

0 commit comments

Comments
 (0)