Skip to content
pg_task

pg_task

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

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
1080
pg_task
pg_task
2.1.29
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
PIGSTY
2.1.29
18
17
16
15
14
pg_task-
RPM
PIGSTY
2.1.29
18
17
16
15
14
pg_task_$v-
DEB
PIGSTY
2.1.29
18
17
16
15
14
postgresql-$v-pg-task-
Linux / PGPG18PG17PG16PG15PG14
el8.x86_64
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
el8.aarch64
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
el9.x86_64
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
el9.aarch64
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
el10.x86_64
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
el10.aarch64
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
d12.x86_64
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
d12.aarch64
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
d13.x86_64
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
d13.aarch64
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
u22.x86_64
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
u22.aarch64
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
u24.x86_64
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
u24.aarch64
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
u26.x86_64
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
u26.aarch64
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
PIGSTY 2.1.29
PackageVersionOSORGSIZEFile URL
pg_task_182.1.29el8.x86_64pigsty54.8 KiBpg_task_18-2.1.29-1PIGSTY.el8.x86_64.rpm
pg_task_182.1.7el8.x86_64pgdg72.4 KiBpg_task_18-2.1.7-3PGDG.rhel8.x86_64.rpm
pg_task_182.1.29el8.aarch64pigsty49.8 KiBpg_task_18-2.1.29-1PIGSTY.el8.aarch64.rpm
pg_task_182.1.7el8.aarch64pgdg63.3 KiBpg_task_18-2.1.7-3PGDG.rhel8.aarch64.rpm
pg_task_182.1.29el9.x86_64pigsty54.7 KiBpg_task_18-2.1.29-1PIGSTY.el9.x86_64.rpm
pg_task_182.1.7el9.x86_64pgdg63.5 KiBpg_task_18-2.1.7-3PGDG.rhel9.x86_64.rpm
pg_task_182.1.29el9.aarch64pigsty52.7 KiBpg_task_18-2.1.29-1PIGSTY.el9.aarch64.rpm
pg_task_182.1.7el9.aarch64pgdg54.5 KiBpg_task_18-2.1.7-3PGDG.rhel9.aarch64.rpm
pg_task_182.1.29el10.x86_64pigsty54.9 KiBpg_task_18-2.1.29-1PIGSTY.el10.x86_64.rpm
pg_task_182.1.7el10.x86_64pgdg59.7 KiBpg_task_18-2.1.7-3PGDG.rhel10.x86_64.rpm
pg_task_182.1.29el10.aarch64pigsty52.7 KiBpg_task_18-2.1.29-1PIGSTY.el10.aarch64.rpm
pg_task_182.1.7el10.aarch64pgdg56.3 KiBpg_task_18-2.1.7-3PGDG.rhel10.aarch64.rpm
postgresql-18-pg-task2.1.29d12.x86_64pigsty38.5 KiBpostgresql-18-pg-task_2.1.29-2PIGSTY~bookworm_amd64.deb
postgresql-18-pg-task2.1.29d12.aarch64pigsty35.3 KiBpostgresql-18-pg-task_2.1.29-2PIGSTY~bookworm_arm64.deb
postgresql-18-pg-task2.1.29d13.x86_64pigsty38.8 KiBpostgresql-18-pg-task_2.1.29-2PIGSTY~trixie_amd64.deb
postgresql-18-pg-task2.1.29d13.aarch64pigsty35.6 KiBpostgresql-18-pg-task_2.1.29-2PIGSTY~trixie_arm64.deb
postgresql-18-pg-task2.1.29u22.x86_64pigsty42.5 KiBpostgresql-18-pg-task_2.1.29-2PIGSTY~jammy_amd64.deb
postgresql-18-pg-task2.1.29u22.aarch64pigsty41.0 KiBpostgresql-18-pg-task_2.1.29-2PIGSTY~jammy_arm64.deb
postgresql-18-pg-task2.1.29u24.x86_64pigsty41.1 KiBpostgresql-18-pg-task_2.1.29-2PIGSTY~noble_amd64.deb
postgresql-18-pg-task2.1.29u24.aarch64pigsty39.7 KiBpostgresql-18-pg-task_2.1.29-2PIGSTY~noble_arm64.deb
postgresql-18-pg-task2.1.29u26.x86_64pigsty41.1 KiBpostgresql-18-pg-task_2.1.29-2PIGSTY~resolute_amd64.deb
postgresql-18-pg-task2.1.29u26.aarch64pigsty39.6 KiBpostgresql-18-pg-task_2.1.29-2PIGSTY~resolute_arm64.deb

Source

pig build pkg pg_task;		# build rpm/deb

Install

Make sure PGDG and PIGSTY repo available:

pig repo add pgsql -u   # add both 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

Sources: pg_task upstream README, PGXN pg_task.

pg_task is a background-worker scheduler for running SQL asynchronously at a planned time. Upstream documents PostgreSQL, Greenplum, and Greengage support.

Enable the worker at server start, then create the extension in the database that will own the task table:

shared_preload_libraries = 'pg_task'
CREATE EXTENSION pg_task;

Schedule Tasks

Schedule work by inserting rows into the configured task table, which defaults to public.task in database postgres unless changed with GUCs.

-- 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.
-- max = 1 means one task at a time for this group.
INSERT INTO task ("group", max, input) VALUES ('billing', 1, 'SELECT refresh_billing_cache()');

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

Task Table

The task table is meant to be user-visible. Upstream notes that users may add columns or partition it.

Key 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; negative values mean pause between tasks in milliseconds
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

Configuration

Key settings:

NameTypeDefaultDescription
pg_task.deletebooltrueAuto delete completed tasks
pg_task.driftboolfalseCompute repeat by stop time
pg_task.headerbooltrueShow column headers in task output
pg_task.stringbooltrueQuote only strings in output
pg_task.countint0Default maximum number of tasks per worker before exit
pg_task.fetchint100Number of task rows fetched at once
pg_task.limitint1000Limit task rows at once
pg_task.maxint0Default max concurrent tasks in group
pg_task.runint2147483647Maximum concurrently executing tasks in work
pg_task.sleepint1000Check tasks every N milliseconds
pg_task.activeinterval1 hourPeriod after plan time when a task remains active for execution
pg_task.liveinterval0 secMaximum lifetime of a worker process
pg_task.repeatinterval0 secDefault repeat interval
pg_task.timeoutinterval0 secDefault task timeout
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.jsonjson[{"data":"postgres"}]Multi-database configuration
pg_task.idbigint0Current task id, read-only session setting

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.

The local metadata marks this package as headless, so it needs shared_preload_libraries rather than a user-facing SQL-only install path. Validate background scheduling behavior on the target PostgreSQL version before relying on it for critical jobs.

Last updated on