Skip to main content

pgbg: PostgreSQL-orchestrated background threads for Python

License: MIT PyPI version No AI slop inside.

POV: you want a framework-agnostic way to reliably run a plain1 function or method in the background, repeatedly, but not all the time.

pgbg comes to the rescue with:

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

  • A supervisor that runs your code as a service: in a loop, in a background thread. If your code crashes, the supervisor restarts the loop. Write crash-only code, pgbg takes care of the rest.

    Your services can wake up on NOTIFYs, fixed time intervals, or both.

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

  • Framework and platform independence.

Background tasks are not a traditional worker queue2. Common use cases include:

  • Periodic cleanup duties for expired caches or sessions.
  • Maintenance of eventually consistent read models.
  • Lightweight transactional background tasks with the 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.

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 an optional extra sqlalchemy (uv pip install 'pgbg[sqlalchemy]'), but that currently only adds a SQLAlchemy>2 lower pin.

Documentation

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

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. No async. ↩

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

Metadata

Release files for pgbg 26.1.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.1.0
File Size Uploaded
pgbg-26.1.0.tar.gz 144.8 kB Details

Built distribution (wheel)

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

Total release size: 172.2 kB

Release files / pgbg-26.1.0.tar.gz

Download URL pgbg-26.1.0.tar.gz
Size 144.8 kB
Tags Source
SHA-256 checksum
How to use checksums
874646f128f7eb689312f03f09647af9cefffa2769246f5530b6af5b175c41f1
BLAKE2b-256 checksum
How to use checksums
71caceb54f84d023cf23cd787df35dc43eb7efab01203cf9bf1599dab0336137
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 2, 2026.

Transparency log

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

Download URL pgbg-26.1.0-py3-none-any.whl
Size 27.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0c0693afb2fbba31661da52630925a6784a6746febd55d3bbe66ac388d62952d
BLAKE2b-256 checksum
How to use checksums
d6ade68425de7f57405c058e6baf2b441921d3e2eba7af1f6c2402122418f5fd
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 2, 2026.

Transparency log

Release history Release notifications | RSS feed

26.3.0

2 release files

This release

26.1.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