pg_task
pg_task · execute any sql command at any specific time at background
Overview
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
| --sL--- | No | Yes | Yes | No | no | no |
| Relationships | |
|---|---|
| See Also | pg_cron pg_durable pg_background pgagent pg_dispatch pg_dbms_job pg_later pgmq ulak pg_retry |
Packages
| Linux / PG | PG19 | PG18 | PG17 | PG16 | PG15 |
|---|---|---|---|---|---|
| el8.x86_64 | N/A | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 |
| el8.aarch64 | N/A | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 |
| el9.x86_64 | PIGSTY 3.0.0 | PGDG 3.0.0 | PGDG 3.0.0 | PGDG 3.0.0 | PGDG 3.0.0 |
| el9.aarch64 | PIGSTY 3.0.0 | PGDG 3.0.0 | PGDG 3.0.0 | PGDG 3.0.0 | PGDG 3.0.0 |
| el10.x86_64 | PIGSTY 3.0.0 | PGDG 3.0.0 | PGDG 3.0.0 | PGDG 3.0.0 | PGDG 3.0.0 |
| el10.aarch64 | PIGSTY 3.0.0 | PGDG 3.0.0 | PGDG 3.0.0 | PGDG 3.0.0 | PGDG 3.0.0 |
| d12.x86_64 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 |
| d12.aarch64 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 |
| d13.x86_64 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 |
| d13.aarch64 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 |
| u22.x86_64 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 |
| u22.aarch64 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 |
| u24.x86_64 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 |
| u24.aarch64 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 |
| u26.x86_64 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 |
| u26.aarch64 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 | PIGSTY 3.0.0 |
Source
github.com/RekGRpth/pg_task
pg_task-3.0.0.tar.gz
Install
Make sure PGDG and PIGSTY repo available:
Install this extension with pig:
Config this extension to shared_preload_libraries:
This extension does not need CREATE EXTENSION DDL command
Usage
Sources:
pg_task is a background-worker scheduler for running SQL asynchronously at a planned time. Upstream documents PostgreSQL, Greenplum, and Greengage support.
Add pg_task to the existing shared_preload_libraries setting and restart PostgreSQL. On its first connection the worker creates the schema and task table it needs.
Schedule Tasks
Schedule work by inserting rows into the configured task table, which defaults to public.task in database postgres unless changed with GUCs.
Task Table
The task table is meant to be user-visible. Upstream notes that users may add columns or partition it.
Key columns:
| Name | Type | Default | Description |
|---|---|---|---|
| id | bigserial | autoincrement | Primary key |
| parent | bigint | pg_task.id | Parent task id |
| plan | timestamptz | statement_timestamp() | Planned start time |
| start | timestamptz | Actual start time | |
| stop | timestamptz | Actual stop time | |
| active | interval | 1 hour | Period after plan time when task is active |
| live | interval | 0 sec | Max lifetime of background worker |
| repeat | interval | 0 sec | Auto repeat interval |
| timeout | interval | 0 sec | Allowed time for task run |
| count | int | 0 | Max task count before worker exit |
| max | int | 0 | Additional concurrent tasks in the group. Zero runs tasks serially. A negative value sets a delay in milliseconds |
| pid | int | Process id executing task | |
| state | enum | PLAN | PLAN, GONE, TAKE, WORK, DONE, FAIL, STOP |
| delete | bool | true | Auto delete when output and error are null |
| drift | bool | false | Compute next repeat by stop time |
| header | bool | true | Show column headers in output |
| group | text | ‘group’ | Task grouping name |
| input | text | SQL command(s) to execute | |
| output | text | Received result(s) | |
| error | text | Caught error | |
| remote | text | Remote database connection string |
Configuration
Key settings:
| Name | Type | Default | Description |
|---|---|---|---|
| pg_task.delete | bool | true | Auto delete completed tasks |
| pg_task.drift | bool | false | Compute repeat by stop time |
| pg_task.header | bool | true | Show column headers in task output |
| pg_task.string | bool | true | Quote only strings in output |
| pg_task.count | int | 0 | Default maximum number of tasks per worker before exit |
| pg_task.fetch | int | 100 | Number of task rows fetched at once |
| pg_task.limit | int | 1000 | Limit task rows at once |
| pg_task.max | int | 0 | Default number of additional concurrent tasks in a group |
| pg_task.run | int | 2147483647 | Maximum concurrently executing tasks in work |
| pg_task.sleep | int | 1000 | Check tasks every N milliseconds |
| pg_task.active | interval | 1 hour | Period after plan time when a task remains active for execution |
| pg_task.live | interval | 0 sec | Maximum lifetime of a worker process |
| pg_task.repeat | interval | 0 sec | Default repeat interval |
| pg_task.timeout | interval | 0 sec | Default task timeout |
| pg_task.data | text | postgres | Database name for tasks table |
| pg_task.user | text | postgres | User name for tasks table |
| pg_task.schema | text | public | Schema name for tasks table |
| pg_task.table | text | task | Table name for tasks table |
| pg_task.json | json | [{"data":"postgres"}] | Multi-database configuration |
| pg_task.id | bigint | 0 | Current task id, read-only session setting |
Multi-Database Configuration
To run tasks on multiple databases, configure via JSON:
If the specified database, user, schema or table does not exist, pg_task will create them.
Local tasks run with the privileges of the role that inserted the row. Grant write access to the task table accordingly and restrict access to stored remote connection parameters.