README.md

September 8, 2025 ยท View on GitHub

PostgreSQL, Greenplum and Greengage job scheduler pg_task allows to execute any sql command at any specific time at background asynchronously

first

shared_preload_libraries = 'pg_task' # add pg_task to shared_preload_libraries

second

INSERT INTO task (input) VALUES ('SELECT now()'); -- to run sql more quickly use only input
INSERT INTO task (plan, input) VALUES (now() + '5 min':INTERVAL, 'SELECT now()'); -- to run sql after 5 minutes point plan(ned time)
INSERT INTO task (plan, input) VALUES ('2029-07-01 12:51:00', 'SELECT now()'); -- to run sql at specific time point it as plan(ned time)
INSERT INTO task (repeat, input) VALUES ('5 min', 'SELECT now()'); -- to repeat sql every 5 minutes point repeat( interval)
INSERT INTO task (input) VALUES ('SELECT 1/0'); -- exception is catched and writed in error as text
INSERT INTO task (group, max, input) VALUES ('group', 1, 'SELECT now()'); -- if some group needs concurently run only 2 parallel sqls then use max = 1
INSERT INTO task (group, max, input) VALUES ('group', 2, 'SELECT now()'); -- if in this group there are more sqls and they are executing concurently by 2 then passing max = 2 will execute sql as more early in this group (it is like priority)
INSERT INTO task (input, remote) VALUES ('SELECT now()', 'user=user host=host'); -- to run sql on remote database use remote

pg_task creates the following GUCs:

NameTypeDefaultLevelDescription
pg_task.deletebooltrueconfig, database, user, sessionAuto delete task when both output and error are nulls
pg_task.driftboolfalseconfig, database, user, sessionCompute next repeat time by stop time instead by plan time
pg_task.headerbooltrueconfig, database, user, sessionShow columns headers in output
pg_task.stringbooltrueconfig, database, user, sessionQuote only strings
pg_conf.closeint60 * 1000config, database, superuserClose conf, milliseconds
pg_conf.fetchint10config, database, superuserFetch conf rows at once
pg_conf.restartint60config, database, superuserRestart conf interval, seconds
pg_task.countint0config, database, user, sessionNon-negative maximum count of tasks, are executed by current background worker process before exit
pg_task.fetchint100config, database, userFetch task rows at once
pg_task.idbigint0sessionCurrent task id (for read only)
pg_task.limitint1000config, database, userLimit task rows at once
pg_task.maxint0config, database, user, sessionMaximum count of concurrently executing tasks in group, negative value means pause between tasks in milliseconds
pg_task.runint2147483647config, database, user, sessionMaximum count of concurrently executing tasks in work
pg_task.sleepint1000config, database, userCheck tasks every sleep milliseconds
pg_work.closeint60 * 1000config, database, superuserClose work, milliseconds
pg_work.fetchint100config, database, superuserFetch work rows at once
pg_work.restartint60config, database, superuserRestart work interval, seconds
pg_task.activeinterval1 hourconfig, database, user, sessionPositive period after plan time, when task is active for executing
pg_task.datatextpostgresconfigDatabase name for tasks table
pg_task.delimiterchar\tconfig, database, user, sessionResults columns delimiter
pg_task.escapecharconfig, database, user, sessionResults columns escape
pg_task.grouptextgroupconfig, database, user, sessionTask grouping by name
pg_task.idleint60config, database, userIdle task count
pg_task.jsonjson[{"data":"postgres"}]configJson configuration, available keys: data, reset, schema, table, sleep and user
pg_task.liveinterval0 secconfig, database, user, sessionNon-negative maximum time of live of current background worker process before exit
pg_task.nulltext\Nconfig, database, user, sessionNull text value representation
pg_task.plantimestamptzstatement_timestamp()config, database, user, sessionDefault value for now timestamp
pg_task.quotecharconfig, database, user, sessionResults columns quote
pg_task.repeatinterval0 secconfig, database, user, sessionNon-negative auto repeat tasks interval
pg_task.resetinterval1 hourconfig, database, userInterval of reset tasks
pg_task.schematextpublicconfig, database, userSchema name for tasks table
pg_task.tabletexttaskconfig, database, userTable name for tasks table
pg_task.timeoutinterval0 secconfig, database, user, sessionNon-negative allowed time for task run
pg_task.usertextpostgresconfigUser name for tasks table

pg_task creates table with the following columns:

NameTypeNullable?DefaultDescription
idbigserialNOT NULLautoincrementPrimary key
parentbigintNULLpg_task.idParent task id (if exists, like foreign key to id, but without constraint, for performance)
plantimestamptzNOT NULLpg_task.planPlanned date and time of start
starttimestamptzNULLActual date and time of start
stoptimestamptzNULLActual date and time of stop
activeintervalNOT NULLpg_task.activePositive period after plan time, when task is active for executing
liveintervalNOT NULLpg_task.liveNon-negative maximum time of live of current background worker process before exit
repeatintervalNOT NULLpg_task.repeatNon-negative auto repeat tasks interval
timeoutintervalNOT NULLpg_task.timeoutNon-negative allowed time for task run
countintNOT NULLpg_task.countNon-negative maximum count of tasks, are executed by current background worker process before exit
maxintNOT NULLpg_task.maxMaximum count of concurrently executing tasks in group, negative value means pause between tasks in milliseconds
pidintNULLId of process executing task
stateenum state (PLAN, TAKE, WORK, DONE, STOP)NOT NULLPLANTask state
deleteboolNOT NULLpg_task.deleteAuto delete task when both output and error are nulls
driftboolNOT NULLpg_task.driftCompute next repeat time by stop time instead by plan time
headerboolNOT NULLpg_task.headerShow columns headers in output
stringboolNOT NULLpg_task.stringQuote only strings
delimitercharNOT NULLpg_task.delimiterResults columns delimiter
escapecharNOT NULLpg_task.escapeResults columns escape
quotecharNOT NULLpg_task.quoteResults columns quote
datatextNULLSome user data
errortextNULLCatched error
grouptextNOT NULLpg_task.groupTask grouping by name
inputtextNOT NULLSql command(s) to execute
nulltextNOT NULLpg_task.nullNull text value representation
outputtextNULLReceived result(s)
remotetextNULLConnect to remote database (if need)

but you may add any needed colums and/or make partitions

by default pg_task runs on default database with default user with default schema with default table with default sleep

to run specific database and/or specific user and/or specific schema and/or specific table and/or specific sleep, set config (in json format)

pg_task.json = '[{"data":"database1"},{"data":"database2","user":"username2"},{"data":"database3","schema":"schema3"},{"data":"database4","table":"table4"},{"data":"database5","sleep":100}]'

if database and/or user and/or schema and/or table does not exist then pg_task create it/their