pg_task

execute any sql command at any specific time at background

Overview

PackageVersionCategoryLicenseLanguage
pg_task1.0.0TIMEMITC
IDExtensionBinLibLoadCreateTrustRelocSchema
1080pg_taskNoYesYesNoNoNo-
Relatedtimescaledb pg_cron pg_later pg_background pg_partman timescaledb_toolkit timeseries periods

breaks on many systems

Version

TypeRepoVersionPG VerPackageDeps
EXTPGDG1.0.01817161514pg_task-
RPMPGDG2.1.71817161514pg_task_$v-
DEBPIGSTY2.1.121817161514postgresql-$v-pg-task-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
d13.x86_64
d13.aarch64
u22.x86_64
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
u24.aarch64
PIGSTY 2.1.12
PIGSTY 2.1.12
PIGSTY 2.1.12
PIGSTY 2.1.12
PIGSTY 2.1.12

Build

You can build the DEB packages for pg_task using pig build:

pig build pkg pg_task         # build DEB packages

Install

You can install pg_task directly. First, make sure the PGDG repository is added and enabled:

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

Install the extension using pig or apt/yum/dnf:

pig install pg_task;          # Install for current active PG version
pig ext install -y pg_task -v 18  # PG 18
pig ext install -y pg_task -v 17  # PG 17
pig ext install -y pg_task -v 16  # PG 16
pig ext install -y pg_task -v 15  # PG 15
pig ext install -y pg_task -v 14  # PG 14
dnf install -y pg_task_18       # PG 18
dnf install -y pg_task_17       # PG 17
dnf install -y pg_task_16       # PG 16
dnf install -y pg_task_15       # PG 15
dnf install -y pg_task_14       # PG 14
apt install -y postgresql-18-pg-task   # PG 18
apt install -y postgresql-17-pg-task   # PG 17
apt install -y postgresql-16-pg-task   # PG 16
apt install -y postgresql-15-pg-task   # PG 15
apt install -y postgresql-14-pg-task   # PG 14

Preload:

shared_preload_libraries = 'pg_task';

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 Modified 2026-03-12: add pg extension catalog (95749bf)