]> git.8kb.co.uk Git - slony-i/pg_sched/blob - README.md
516bcc7ab39d12c1228802e0a272a40d9222dc0c
[slony-i/pg_sched] / README.md
1 #PostgreSQL Scheduler
2
3 Simple scheduler for running stored functions against databases in a 
4 PostgreSQL cluster, with support for selective execution based on the 
5 replication role of nodes in Slony-I clusters.
6
7 The script is designed to be run once a minute from cron or similar, and
8 as such has a maximum resolution of 1 minute.  It provides very simple 
9 scheduling functionality to execute individual stored functions at 
10 specified intervals and offsets (E.g. The most complex schedule would 
11 be of the form "once every 5 hours at 20 minutes past the hour").
12
13 If you require more complex schedules or multi-step tasks try 
14 <a href="http://www.postgresql.org/ftp/pgadmin3/release/pgagent/" target="_blank">pgAgent</a>
15
16 ## Command line
17
18 ```bash
19 $ pg_sched.pl -h <db host> -p <db port> -d <db name> -U <db user> -n <schema> -t <table> -cl <slony clustername> -m <master sets> -l <lock file>
20     -h      Hostname of database containing schedule table
21             DEFAULT = localhost
22     -p      Listening port of database containing schedule table
23             DEFAULT = 5432
24     -d      Name of database containing schedule table
25             DEFAULT = postgres
26     -U      User to connect to databse with, also used as default user for all tasks
27             DEFAULT = pg_sched
28     -n      Name of schema containing schedule table
29             DEFAULT = public
30     -t      Name of schedule table
31             DEFAULT = pg_sched
32     -cl     Name of slony cluster. 'auto' to autodetect, 'off' to disable
33             DEFAULT = auto
34     -m      Comma separated list of slony sets on the master. 'all' for all
35             DEFAULT = all
36     -l      Lockfile used to prevent concurrent execution of tasks.
37             DEFAULT = not used
38 ```
39
40 ##Example usage
41
42 Create a PostgreSQL user for schedule to run as, to run tasks under 
43 different roles create the user as a superuser:
44
45 ```postgresql
46 mydb=> CREATE USER pg_sched WITH PASSWORD 'my_password' SUPERUSER ; 
47 ```
48
49 Create a pgpass file for the OS user that will run the script from cron:
50
51 ```bash
52 $ echo *:*:*:pg_sched:my_password >> /home/myuser/.pgpass
53 chmod 600 /home/myuser/.pgpass
54 ```
55
56 Create the schedule table in your database of choice:
57
58 ```bash
59 $ psql -U pg_sched -d mydb -f pg_sched.sql
60 ```
61
62 Add an entry to run from cron something like:
63
64 ```cron
65         * * * * * myuser /home/myuser/pg_sched.pl -d mydb -U pg_sched >> /var/log/pg_sched.log 2>&1
66 ```
67
68 Insert a row into the table to create a task, e.g:
69
70 ```postgresql
71 mydb=> INSERT INTO pg_sched (usename, datname, pronamespace, proname, proargs, proargtypes, enabled, frequency, frequency_offset) 
72 VALUES ('test_user', 'mydb', 'pg_catalog', 'date_part', '{"hour", "2015-03-25 13:50:59"}', '{text, timestamp}', 'A', '1 hour', '1 minute');
73 ```