pg_task

pg_task

pg_task : execute any sql command at any specific time at background

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
1080
pg_task
pg_task
1.0.0
TIME
MIT
C
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--sL---
No
Yes
Yes
No
no
no
Relationships
See Also
timescaledb
pg_cron
pg_later
pg_background
pg_partman
timescaledb_toolkit
timeseries
periods

breaks on many systems

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
EXT
PGDG
1.0.0
18
17
16
15
14
pg_task-
RPM
PGDG
2.1.7
18
17
16
15
14
pg_task_$v-
DEB
PIGSTY
2.1.12
18
17
16
15
14
postgresql-$v-pg-task-
Linux / PGPG18PG17PG16PG15PG14
el8.x86_64
PGDG 2.1.7
PGDG 2.1.7
PGDG 2.1.7
PGDG 2.1.7
PGDG 2.1.7
el8.aarch64
PGDG 2.1.7
PGDG 2.1.7
PGDG 2.1.7
PGDG 2.1.7
PGDG 2.1.7
el9.x86_64
PGDG 2.1.7
PGDG 2.1.7
PGDG 2.1.7
PGDG 2.1.7
PGDG 2.1.7
el9.aarch64
PGDG 2.1.7
PGDG 2.1.7
PGDG 2.1.7
PGDG 2.1.7
PGDG 2.1.7
el10.x86_64
PGDG 2.1.7
PGDG 2.1.7
PGDG 2.1.7
PGDG 2.1.7
PGDG 2.1.7
el10.aarch64
PGDG 2.1.7
PGDG 2.1.7
PGDG 2.1.7
PGDG 2.1.7
PGDG 2.1.7
d12.x86_64
PIGSTY 2.1.12
PIGSTY 2.1.12
PIGSTY 2.1.12
PIGSTY 2.1.12
PIGSTY 2.1.12
d12.aarch64
PIGSTY 2.1.12
PIGSTY 2.1.12
PIGSTY 2.1.12
PIGSTY 2.1.12
PIGSTY 2.1.12
d13.x86_64
PIGSTY 2.1.12
PIGSTY 2.1.12
PIGSTY 2.1.12
PIGSTY 2.1.12
PIGSTY 2.1.12
d13.aarch64
PIGSTY 2.1.12
PIGSTY 2.1.12
PIGSTY 2.1.12
PIGSTY 2.1.12
PIGSTY 2.1.12
u22.x86_64
PIGSTY 2.1.12
PIGSTY 2.1.12
PIGSTY 2.1.12
PIGSTY 2.1.12
PIGSTY 2.1.12
u22.aarch64
PIGSTY 2.1.12
PIGSTY 2.1.12
PIGSTY 2.1.12
PIGSTY 2.1.12
PIGSTY 2.1.12
u24.x86_64
PIGSTY 2.1.12
PIGSTY 2.1.12
PIGSTY 2.1.12
PIGSTY 2.1.12
PIGSTY 2.1.12
u24.aarch64
PIGSTY 2.1.12
PIGSTY 2.1.12
PIGSTY 2.1.12
PIGSTY 2.1.12
PIGSTY 2.1.12
PackageVersionOSORGSIZEFile URL
pg_task_162.1.7el8.x86_64pgdg72.3 KiBpg_task_16-2.1.7-1PGDG.rhel8.x86_64.rpm
pg_task_162.1.5el8.x86_64pgdg72.2 KiBpg_task_16-2.1.5-1PGDG.rhel8.x86_64.rpm
pg_task_162.1.7el8.aarch64pgdg63.1 KiBpg_task_16-2.1.7-1PGDG.rhel8.aarch64.rpm
pg_task_162.1.5el8.aarch64pgdg63.0 KiBpg_task_16-2.1.5-1PGDG.rhel8.aarch64.rpm
pg_task_162.1.7el9.x86_64pgdg62.8 KiBpg_task_16-2.1.7-1PGDG.rhel9.x86_64.rpm
pg_task_162.1.5el9.x86_64pgdg62.8 KiBpg_task_16-2.1.5-1PGDG.rhel9.x86_64.rpm
pg_task_162.1.7el9.aarch64pgdg53.8 KiBpg_task_16-2.1.7-1PGDG.rhel9.aarch64.rpm
pg_task_162.1.5el9.aarch64pgdg53.7 KiBpg_task_16-2.1.5-1PGDG.rhel9.aarch64.rpm
pg_task_162.1.7el10.x86_64pgdg58.8 KiBpg_task_16-2.1.7-3PGDG.rhel10.x86_64.rpm
pg_task_162.1.7el10.aarch64pgdg55.0 KiBpg_task_16-2.1.7-3PGDG.rhel10.aarch64.rpm
postgresql-16-pg-task2.1.12d12.x86_64pigsty192.4 KiBpostgresql-16-pg-task_2.1.12-1PIGSTY~bookworm_amd64.deb
postgresql-16-pg-task2.1.12d12.aarch64pigsty183.5 KiBpostgresql-16-pg-task_2.1.12-1PIGSTY~bookworm_arm64.deb
postgresql-16-pg-task2.1.12d13.x86_64pigsty191.6 KiBpostgresql-16-pg-task_2.1.12-1PIGSTY~trixie_amd64.deb
postgresql-16-pg-task2.1.12d13.aarch64pigsty184.8 KiBpostgresql-16-pg-task_2.1.12-1PIGSTY~trixie_arm64.deb
postgresql-16-pg-task2.1.12u22.x86_64pigsty226.9 KiBpostgresql-16-pg-task_2.1.12-1PIGSTY~jammy_amd64.deb
postgresql-16-pg-task2.1.12u22.aarch64pigsty216.6 KiBpostgresql-16-pg-task_2.1.12-1PIGSTY~jammy_arm64.deb
postgresql-16-pg-task2.1.12u24.x86_64pigsty193.1 KiBpostgresql-16-pg-task_2.1.12-1PIGSTY~noble_amd64.deb
postgresql-16-pg-task2.1.12u24.aarch64pigsty183.2 KiBpostgresql-16-pg-task_2.1.12-1PIGSTY~noble_arm64.deb
PackageVersionOSORGSIZEFile URL
pg_task_152.1.7el8.x86_64pgdg73.3 KiBpg_task_15-2.1.7-1PGDG.rhel8.x86_64.rpm
pg_task_152.1.5el8.x86_64pgdg73.2 KiBpg_task_15-2.1.5-1PGDG.rhel8.x86_64.rpm
pg_task_152.1.7el8.aarch64pgdg64.0 KiBpg_task_15-2.1.7-1PGDG.rhel8.aarch64.rpm
pg_task_152.1.5el8.aarch64pgdg63.9 KiBpg_task_15-2.1.5-1PGDG.rhel8.aarch64.rpm
pg_task_152.1.7el9.x86_64pgdg75.3 KiBpg_task_15-2.1.7-1PGDG.rhel9.x86_64.rpm
pg_task_152.1.5el9.x86_64pgdg75.2 KiBpg_task_15-2.1.5-1PGDG.rhel9.x86_64.rpm
pg_task_152.1.7el9.aarch64pgdg68.3 KiBpg_task_15-2.1.7-1PGDG.rhel9.aarch64.rpm
pg_task_152.1.5el9.aarch64pgdg68.2 KiBpg_task_15-2.1.5-1PGDG.rhel9.aarch64.rpm
pg_task_152.1.7el10.x86_64pgdg72.3 KiBpg_task_15-2.1.7-3PGDG.rhel10.x86_64.rpm
pg_task_152.1.7el10.aarch64pgdg69.5 KiBpg_task_15-2.1.7-3PGDG.rhel10.aarch64.rpm
postgresql-15-pg-task2.1.12d12.x86_64pigsty193.0 KiBpostgresql-15-pg-task_2.1.12-1PIGSTY~bookworm_amd64.deb
postgresql-15-pg-task2.1.12d12.aarch64pigsty183.3 KiBpostgresql-15-pg-task_2.1.12-1PIGSTY~bookworm_arm64.deb
postgresql-15-pg-task2.1.12d13.x86_64pigsty192.1 KiBpostgresql-15-pg-task_2.1.12-1PIGSTY~trixie_amd64.deb
postgresql-15-pg-task2.1.12d13.aarch64pigsty184.7 KiBpostgresql-15-pg-task_2.1.12-1PIGSTY~trixie_arm64.deb
postgresql-15-pg-task2.1.12u22.x86_64pigsty236.4 KiBpostgresql-15-pg-task_2.1.12-1PIGSTY~jammy_amd64.deb
postgresql-15-pg-task2.1.12u22.aarch64pigsty229.2 KiBpostgresql-15-pg-task_2.1.12-1PIGSTY~jammy_arm64.deb
postgresql-15-pg-task2.1.12u24.x86_64pigsty202.5 KiBpostgresql-15-pg-task_2.1.12-1PIGSTY~noble_amd64.deb
postgresql-15-pg-task2.1.12u24.aarch64pigsty195.6 KiBpostgresql-15-pg-task_2.1.12-1PIGSTY~noble_arm64.deb
PackageVersionOSORGSIZEFile URL
pg_task_142.1.7el8.x86_64pgdg73.0 KiBpg_task_14-2.1.7-1PGDG.rhel8.x86_64.rpm
pg_task_142.1.5el8.x86_64pgdg72.9 KiBpg_task_14-2.1.5-1PGDG.rhel8.x86_64.rpm
pg_task_142.1.7el8.aarch64pgdg63.8 KiBpg_task_14-2.1.7-1PGDG.rhel8.aarch64.rpm
pg_task_142.1.5el8.aarch64pgdg63.7 KiBpg_task_14-2.1.5-1PGDG.rhel8.aarch64.rpm
pg_task_142.1.7el9.x86_64pgdg74.9 KiBpg_task_14-2.1.7-1PGDG.rhel9.x86_64.rpm
pg_task_142.1.5el9.x86_64pgdg74.9 KiBpg_task_14-2.1.5-1PGDG.rhel9.x86_64.rpm
pg_task_142.1.7el9.aarch64pgdg68.2 KiBpg_task_14-2.1.7-1PGDG.rhel9.aarch64.rpm
pg_task_142.1.5el9.aarch64pgdg68.1 KiBpg_task_14-2.1.5-1PGDG.rhel9.aarch64.rpm
pg_task_142.1.7el10.x86_64pgdg72.1 KiBpg_task_14-2.1.7-3PGDG.rhel10.x86_64.rpm
pg_task_142.1.7el10.aarch64pgdg69.4 KiBpg_task_14-2.1.7-3PGDG.rhel10.aarch64.rpm
postgresql-14-pg-task2.1.12d12.x86_64pigsty192.6 KiBpostgresql-14-pg-task_2.1.12-1PIGSTY~bookworm_amd64.deb
postgresql-14-pg-task2.1.12d12.aarch64pigsty182.7 KiBpostgresql-14-pg-task_2.1.12-1PIGSTY~bookworm_arm64.deb
postgresql-14-pg-task2.1.12d13.x86_64pigsty191.8 KiBpostgresql-14-pg-task_2.1.12-1PIGSTY~trixie_amd64.deb
postgresql-14-pg-task2.1.12d13.aarch64pigsty183.9 KiBpostgresql-14-pg-task_2.1.12-1PIGSTY~trixie_arm64.deb
postgresql-14-pg-task2.1.12u22.x86_64pigsty232.1 KiBpostgresql-14-pg-task_2.1.12-1PIGSTY~jammy_amd64.deb
postgresql-14-pg-task2.1.12u22.aarch64pigsty224.7 KiBpostgresql-14-pg-task_2.1.12-1PIGSTY~jammy_arm64.deb
postgresql-14-pg-task2.1.12u24.x86_64pigsty201.8 KiBpostgresql-14-pg-task_2.1.12-1PIGSTY~noble_amd64.deb
postgresql-14-pg-task2.1.12u24.aarch64pigsty195.2 KiBpostgresql-14-pg-task_2.1.12-1PIGSTY~noble_arm64.deb

Source

pig build pkg pg_task;		# build deb

Install

Make sure PGDG repo available:

pig repo add pgdg -u    # add pgdg repo and update cache

Install this extension with pig:

pig install pg_task;		# install via package name, for the active PG version

pig install pg_task -v 18;   # install for PG 18
pig install pg_task -v 17;   # install for PG 17
pig install pg_task -v 16;   # install for PG 16
pig install pg_task -v 15;   # install for PG 15
pig install pg_task -v 14;   # install for PG 14

Config this extension to shared_preload_libraries:

shared_preload_libraries = 'pg_task';

This extension does not need CREATE EXTENSION DDL command

Usage

pg_task: PostgreSQL job scheduler

pg_task allows executing any SQL command at any specific time in the background asynchronously. It works with PostgreSQL, Greenplum and Greengage.

First, add to postgresql.conf:

shared_preload_libraries = 'pg_task'

Then schedule tasks by inserting into the task table:

-- Run SQL immediately
INSERT INTO task (input) VALUES ('SELECT now()');

-- Run SQL after 5 minutes
INSERT INTO task (plan, input) VALUES (now() + '5 min'::INTERVAL, 'SELECT now()');

-- Run SQL at a specific time
INSERT INTO task (plan, input) VALUES ('2029-07-01 12:51:00', 'SELECT now()');

-- Repeat SQL every 5 minutes
INSERT INTO task (repeat, input) VALUES ('5 min', 'SELECT now()');

-- Exceptions are caught and written to the error column
INSERT INTO task (input) VALUES ('SELECT 1/0');

-- Limit concurrent tasks in a group
INSERT INTO task (group, max, input) VALUES ('group', 1, 'SELECT now()');

-- Run SQL on a remote database
INSERT INTO task (input, remote) VALUES ('SELECT now()', 'user=user host=host');

Task Table Columns

NameTypeDefaultDescription
idbigserialautoincrementPrimary key
parentbigintpg_task.idParent task id
plantimestamptzstatement_timestamp()Planned start time
starttimestamptzActual start time
stoptimestamptzActual stop time
activeinterval1 hourPeriod after plan time when task is active
liveinterval0 secMax lifetime of background worker
repeatinterval0 secAuto repeat interval
timeoutinterval0 secAllowed time for task run
countint0Max task count before worker exit
maxint0Max concurrent tasks in group
pidintProcess id executing task
stateenumPLANPLAN, TAKE, WORK, DONE, STOP
deletebooltrueAuto delete when output and error are null
driftboolfalseCompute next repeat by stop time
headerbooltrueShow column headers in output
grouptext‘group’Task grouping name
inputtextSQL command(s) to execute
outputtextReceived result(s)
errortextCaught error
remotetextRemote database connection string

You may add any needed columns and/or make partitions on this table.

Configuration (GUCs)

Key settings:

NameTypeDefaultDescription
pg_task.datatextpostgresDatabase name for tasks table
pg_task.usertextpostgresUser name for tasks table
pg_task.schematextpublicSchema name for tasks table
pg_task.tabletexttaskTable name for tasks table
pg_task.sleepint1000Check tasks every N milliseconds
pg_task.deletebooltrueAuto delete completed tasks
pg_task.driftboolfalseCompute repeat by stop time
pg_task.repeatinterval0 secDefault repeat interval
pg_task.timeoutinterval0 secDefault task timeout
pg_task.maxint0Default max concurrent tasks in group
pg_task.runint2147483647Max concurrent tasks in work
pg_task.jsonjson[{“data”:“postgres”}]Multi-database configuration

Multi-Database Configuration

To run tasks on multiple databases, configure via JSON:

pg_task.json = '[{"data":"database1"},{"data":"database2","user":"username2"},{"data":"database3","schema":"schema3"}]'

If the specified database, user, schema or table does not exist, pg_task will create them.

Last updated on