pgbg
PostgreSQL-orchestrated background threads for Python
pgbg builds on bgt (a great way to reliably run background threads) and adds PostgreSQL integration:
-
A
NOTIFYdispatcher 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:
- Periodic cleanup duties for expired caches or sessions.
- Maintenance of eventually consistent read models.
- Lightweight background tasks with the transactional outbox pattern.
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 aSQLAlchemy>=2dependency.pool(uv pip install 'pgbg[pool]') installs psycopg-pool.
Documentation
Full documentation lives at https://pgbg.hynek.me/.
Release Information
Added
pgbg.NotifyDispatcher.wake()that makes a dispatch loop you run yourself notice its stop event at once. #3
Changed
-
Stopping a dispatcher, subscribing, and closing a subscription take effect at once instead of within one interval. A
LISTENconnection that PostgreSQL closes is replaced at once, too. interval now only sets how often an idle dispatcher checks that its connection is alive. #3 -
As such, the default interval of
pgbg.NotifyDispatcher,pgbg.SupervisedDispatcher.start(), andpgbg.sqlalchemy.start_dispatcher()went from 1 to 30 seconds. #3
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.
-
But it's useful for implementing worker queues ~ominous foreshadowing~. ↩
Metadata
Release files for pgbg 26.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pgbg-26.3.0.tar.gz | 144.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pgbg-26.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 168.1 kB
Release files / pgbg-26.3.0.tar.gz
| Download URL | pgbg-26.3.0.tar.gz |
|---|---|
| Size | 144.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
38d58a9c78becfd76562b0fe5c95352a2f09fade3d3ebb36d0bf9b45fd5e5719
|
|
BLAKE2b-256 checksum How to use checksums |
05225691c84d58739b81886c85794635ae2403f29c52a923f327a71d64beaca4
|
| 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 Oct 16, 2026.
Transparency logRelease files / pgbg-26.3.0-py3-none-any.whl
| Download URL | pgbg-26.3.0-py3-none-any.whl |
|---|---|
| Size | 23.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
07a479b2a1804289bd586f07effa18add72f1d78daa9da3f7797a7dcc7b88955
|
|
BLAKE2b-256 checksum How to use checksums |
62ebe115ed605c909f0c2caf6c73474c89e88967b4f9ed8ca98e486c836bd1bc
|
| 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 Oct 16, 2026.
Transparency log