Skip to content

pg_swarm

Background-worker task scheduling with SQL executors.

pg_extensions · Background-worker task scheduling with SQL executors.

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
102100pg_swarmpg_extensions0.3.3FEATMatroid-SAL-1.0Rust

PostgreSQL · 14, 15, 16, 17

Usage

Sources:

pg_swarm 0.3.3 registers SQL executors and splits jobs into tasks managed by background workers in pgswarm, with status, retries and optional result tables.

Core Workflow

shared_preload_libraries = 'pg_swarm'
CREATE EXTENSION pg_swarm;
CREATE FUNCTION swarm_example(bigint, jsonb, integer, integer)
RETURNS jsonb LANGUAGE SQL AS $$ SELECT $2; $$;
SELECT pgswarm.register_executor('example', 'public.swarm_example');
SELECT pgswarm.submit_job('example', '{"message":"hello"}'::jsonb);
SELECT * FROM pgswarm.list_executors();

Operational Boundaries

The control requires superuser installation. Preload and restart; the node and scheduler use pg_swarm.database (default postgres). Create the extension and executor objects in that database. register_executor records a function receiving task ID, JSONB payload, chunk index and chunk count; submit_job queues work. Executors run inside a privileged service, so restrict registration and submission. pg_swarm.workers, timeout and retry settings control scheduling. Retried tasks require idempotent handling of external side effects; no cross-node exactly-once guarantee is implied. This is an unsupported proof of concept under Matroid Source Available License 1.0. APIs may change. Version 0.3.0 is the new upgrade baseline: earlier 0.2.0 installations require a rehearsed data migration/recreation, not ordinary ALTER EXTENSION UPDATE. Back up data and dependencies before following that destructive upstream path.

Current Release and Upgrade

Extension version 0.3.2 uses pg_swarm.database for background workers. Create the extension in that database; a worker now waits instead of repeatedly exiting when it is absent. Changing restart-sensitive worker configuration requires a restart. From extension 0.3.0 onward, install matching files and use ALTER EXTENSION UPDATE. Earlier versions still require the migration described above. Withdrawn repository 0.4.0 bottles must be replaced by 0.5.0; upstream bottles do not imply Pigsty package availability.

Worker Supervision

Version 0.3.2 adds pgswarm.worker_status() and the superuser-only pgswarm.reset_workers(). Status reports worker name, state, failures, restarts, PID, time of entry into the current state and last failure. pg_swarm.max_worker_failures defaults to 10 consecutive failures; 0 retries indefinitely. Restart delays back off from 5 to 60 seconds, and reaching the limit leaves a failed worker idle until reset.

Install matching libraries, restart PostgreSQL to initialize the new shared memory, and update the extension SQL in the configured worker database. Inspect the failure before resetting; a reset does not repair its cause.

ALTER EXTENSION pg_swarm UPDATE;
SELECT * FROM pgswarm.worker_status();

Idle Workers in 0.3.3

Repository release 0.7.2 contains extension 0.3.3. Read-only or empty-claim polls no longer allocate a transaction ID merely because they use the SPI bridge; an actual row write still does. This removes empty commit WAL from idle polling. Install matching libraries, restart the preloaded workers, and update the SQL extension. The existing privilege, external-service and pre-0.3.0 migration boundaries remain.

Table reader