|
| 1 | +# Getting started |
| 2 | + |
| 3 | +A variety of examples can be found in the [samples](samples.md). If you want to migrate from a different scheduler, you can use scripts from [migration](migration.md) chapter. |
| 4 | + |
| 5 | +## Add simple job |
| 6 | + |
| 7 | +In a real world usually it's enough to use simple jobs. Under this term we understand: |
| 8 | + |
| 9 | +* job is a chain with only one **task** (step) in it; |
| 10 | +* it doesn't use complicated logic, but rather simple **command**; |
| 11 | +* it doesn't require complex transaction handling, since one task is implicitely executed as a single transaction. |
| 12 | + |
| 13 | +For such a group of chains we've introduced a special function `timetable.add_job()`. |
| 14 | + |
| 15 | +### Function: `timetable.add_job()` |
| 16 | + |
| 17 | +Creates a simple one-task chain |
| 18 | + |
| 19 | +**Returns:** `BIGINT` |
| 20 | + |
| 21 | +#### Parameters |
| 22 | + |
| 23 | +| Parameter | Type | Description | Default | |
| 24 | +|-----------|------|-------------|---------| |
| 25 | +| `job_name` | `text` | The unique name of the **chain** and **command** | Required | |
| 26 | +| `job_schedule` | `timetable.cron` | Time schedule in сron syntax at Postgres server time zone | Required | |
| 27 | +| `job_command` | `text` | The SQL which will be executed | Required | |
| 28 | +| `job_parameters` | `jsonb` | Arguments for the chain **command** | `NULL` | |
| 29 | +| `job_kind` | `timetable.command_kind` | Kind of the command: *SQL*, *PROGRAM* or *BUILTIN* | `SQL` | |
| 30 | +| `job_client_name` | `text` | Specifies which client should execute the chain. Set this to `NULL` to allow any client | `NULL` | |
| 31 | +| `job_max_instances` | `integer` | The amount of instances that this chain may have running at the same time | `NULL` | |
| 32 | +| `job_live` | `boolean` | Control if the chain may be executed once it reaches its schedule | `TRUE` | |
| 33 | +| `job_self_destruct` | `boolean` | Self destruct the chain after execution | `FALSE` | |
| 34 | +| `job_ignore_errors` | `boolean` | Ignore error during execution | `TRUE` | |
| 35 | +| `job_exclusive` | `boolean` | Execute the chain in the exclusive mode | `FALSE` | |
| 36 | + |
| 37 | +**Returns:** the ID of the created chain |
| 38 | + |
| 39 | +## Examples |
| 40 | + |
| 41 | +1. Run `public.my_func()` at 00:05 every day in August Postgres server time zone: |
| 42 | + |
| 43 | + ```sql |
| 44 | + SELECT timetable.add_job('execute-func', '5 0 * 8 *', 'SELECT public.my_func()'); |
| 45 | + ``` |
| 46 | + |
| 47 | +2. Run `VACUUM` at minute 23 past every 2nd hour from 0 through 20 every day Postgres server time zone: |
| 48 | + |
| 49 | + ```sql |
| 50 | + SELECT timetable.add_job('run-vacuum', '23 0-20/2 * * *', 'VACUUM'); |
| 51 | + ``` |
| 52 | + |
| 53 | +3. Refresh materialized view every 2 hours: |
| 54 | + |
| 55 | + ```sql |
| 56 | + SELECT timetable.add_job('refresh-matview', '@every 2 hours', 'REFRESH MATERIALIZED VIEW public.mat_view'); |
| 57 | + ``` |
| 58 | + |
| 59 | +4. Clear log table after **pg_timetable** restart: |
| 60 | + |
| 61 | + ```sql |
| 62 | + SELECT timetable.add_job('clear-log', '@reboot', 'TRUNCATE timetable.log'); |
| 63 | + ``` |
| 64 | + |
| 65 | +5. Reindex at midnight Postgres server time zone on Sundays with [reindexdb](https://www.postgresql.org/docs/current/app-reindexdb.html) utility: |
| 66 | + |
| 67 | + - using default database under default user (no command line arguments) |
| 68 | + |
| 69 | + ```sql |
| 70 | + SELECT timetable.add_job('reindex', '0 0 * * 7', 'reindexdb', job_kind := 'PROGRAM'); |
| 71 | + ``` |
| 72 | + |
| 73 | + - specifying target database and tables, and be verbose |
| 74 | + |
| 75 | + ```sql |
| 76 | + SELECT timetable.add_job('reindex', '0 0 * * 7', 'reindexdb', |
| 77 | + '["--table=foo", "--dbname=postgres", "--verbose"]'::jsonb, 'PROGRAM'); |
| 78 | + ``` |
| 79 | + |
| 80 | + - passing password using environment variable through `bash` shell |
| 81 | + |
| 82 | + ```sql |
| 83 | + SELECT timetable.add_job('reindex', '0 0 * * 7', 'bash', |
| 84 | + '["-c", "PGPASSWORD=5m3R7K4754p4m reindexdb -U postgres -h 192.168.0.221 -v"]'::jsonb, |
| 85 | + 'PROGRAM'); |
| 86 | + ``` |
0 commit comments