Skip to main content

pgbg

PostgreSQL-orchestrated background threads for Python

pgbg builds on bgt (a great way to reliably run background threads) and adds PostgreSQL integration:

  • A NOTIFY dispatcher that takes one database connection per process and wakes up an arbitrary number of background threads.

  • A table-based leader election with automatic failover. Make sure only one process runs work at a time.

Even with database backing, background tasks are not a job queue like Celery or RQ1. Common use cases include:


The core needs and supports only Psycopg 3 for database access. pgbg comes with optional support for SQLAlchemy in the pgbg.sqlalchemy module that connects everything to a SQLAlchemy Engine. If you use psycopg-pool, you don't need an adapter at all: pass your pool's connection method wherever pgbg asks for a connection provider.

Check out our step-by-step tutorial to get an instant feel for the features!

Installation

The package is available on PyPI under the pgbg name. It comes with two optional extras:

  • sqlalchemy (uv pip install 'pgbg[sqlalchemy]') currently only adds a SQLAlchemy>=2 dependency.
  • pool (uv pip install 'pgbg[pool]') installs psycopg-pool.

Documentation

Full documentation lives at https://pgbg.hynek.me/.

Release Information

Removed

  • The supervision layer moved to the new bgt package, which pgbg now depends on. pgbg.Supervisor, pgbg.Service, pgbg.SupervisedService, pgbg.IntervalOnlyWakeup, pgbg.as_work_factory, pgbg.exceptions.SuppressedCrashError, and the pgbg.typing.Loop, pgbg.typing.Wakeup, pgbg.typing.DoWork, and pgbg.typing.WorkFactory protocols are gone. Import them from bgt instead; their behavior is unchanged.

Added

  • pgbg.sqlalchemy.init_db() that takes a SQLAlchemy Engine or Connection. #1

Changed

  • The service.notified log event is now called service.woken. It fires on any wakeup, not only on notifications.

  • The restart counter is now called bgt_supervisor_restarts_total, because bgt's supervisor drives every loop. The metrics of elected services keep their pgbg_ prefix. Supervision log events now go to the bgt logger; everything else stays on pgbg.


Full Changelog →

Credits

pgbg is written by Hynek Schlawack and distributed under the terms of the MIT license.

The development is kindly supported by my employer Variomedia AG and all my fabulous GitHub Sponsors.

  1. But it's useful for implementing worker queues ~ominous foreshadowing~. ↩

Metadata

Release files for pgbg 26.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pgbg 26.2.0
File Size Uploaded
pgbg-26.2.0.tar.gz 137.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pgbg 26.2.0
File Interpreter ABI Platform
pgbg-26.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 159.9 kB

Release files / pgbg-26.2.0.tar.gz

Download URL pgbg-26.2.0.tar.gz
Size 137.1 kB
Tags Source
SHA-256 checksum
How to use checksums
2b98cfb3d01ecbca7fead358577fe236eebfb3ece8f16271999b094029f0c9f5
BLAKE2b-256 checksum
How to use checksums
419846f996c28c11bc3afaf688f3e3d20f66eb2653b2b62635e63f79701275a7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 6, 2026.

Transparency log

Release files / pgbg-26.2.0-py3-none-any.whl

Download URL pgbg-26.2.0-py3-none-any.whl
Size 22.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
74d7a3c599ee9cc294f18f6d77d9e0ce3dab027579f94981fd72da8273057897
BLAKE2b-256 checksum
How to use checksums
c15531e2778dea31027e3dc0371369628a0f989d746399a36fc7651140cc4ea6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 6, 2026.

Transparency log

Release history Release notifications | RSS feed

26.3.0

2 release files

This release

26.2.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page