Skip to main content

simplebroker-pg

Postgres backend plugin for SimpleBroker.

This package is intentionally separate from simplebroker itself. SimpleBroker remains SQLite-first. This package adds a Postgres backend through the public backend plugin hook.

Requirements

  • Python 3.11+
  • PostgreSQL
  • A dedicated schema for SimpleBroker tables

public is intentionally rejected.

Exact-target broadcast requires backend API v5: SimpleBroker 5.6.1 or newer and simplebroker-pg 3.3.1 or newer. Default selection intersects requested names with existing queues. Python create_missing=True instead inserts into the complete requested set, intentionally recreating a queue deleted before the broadcast lock is acquired. Selection and insertion occur in one PostgreSQL transaction.

SimpleBroker 7.1.0 and simplebroker-pg 3.6.0 are the first coordinated backend API v6 set, which adds terminal activity-waiter close. Package dependency floors are minimums; the exact runtime handshake remains authoritative for every installed pair.

SimpleBroker 7.3.0 and simplebroker-pg 3.8.0 are the first coordinated backend API v7 set. It adds the required monotone durable high-water advance used by persistence restore. Package dependency floors remain minimums; the exact runtime handshake remains authoritative for every installed pair.

SimpleBroker 8.0.0 and simplebroker-pg 4.0.0 are the first coordinated backend API v8 set. It adds bounded public-ID selection order and SQL storage schema v6. Package dependency floors remain minimums; the exact runtime handshake remains authoritative for every installed pair.

Timestamp resynchronization uses a guarded compare-and-advance update. If a concurrent allocator publishes a higher durable last_ts after repair begins, the repair preserves that winner and refreshes its local cache from the surviving value; it never moves PostgreSQL high-water backward.

Core Compatibility

This first-party extension declares a minimum supported SimpleBroker core version and its backend API version independently. SimpleBroker checks the exact API handshake when it resolves the plugin. An incompatible pair fails at backend resolution with upgrade-or-pin guidance instead of running against an unknown interface. Core and extension package version numbers do not match. The backend API version is separate from the PostgreSQL storage schema version and is not stored in the database. A breaking private-seam change requires a backend API version bump.

The package dependency is an install-time minimum; the runtime handshake is the authoritative interface check. Install the extension through the core release's pg extra. See the backend authoring guide for the handshake boundary.

Installation

# Fresh install through SimpleBroker's convenience extra
pipx install "simplebroker[pg]"

# Add to an existing pipx-installed simplebroker (recommended)
pipx inject simplebroker simplebroker-pg

# Or install through the convenience extra in a project
uv add "simplebroker[pg]"

# Or install the extension directly with uv
uv add simplebroker-pg

# Or install the extension directly with pip
pip install simplebroker-pg

simplebroker[pg] still installs this package as a separate distribution. Postgres support is not built into the default simplebroker install.

Python Usage

from simplebroker import Queue
from simplebroker_pg import PostgresRunner

runner = PostgresRunner(
    "postgresql://postgres@127.0.0.1:54329/simplebroker_test",
    schema="simplebroker_app",
)

queue = Queue("jobs", runner=runner, persistent=True)
try:
    queue.write("hello")
    print(queue.read())
finally:
    queue.close()
    runner.close()

Connection Pressure Inspection

Callers that have narrowed a Queue to PostgreSQL can inspect server-wide connection pressure through the public package-root helper:

from simplebroker_pg import get_connection_stats

if queue.backend_name == "postgres":
    stats = get_connection_stats(queue)

The returned dictionary has exactly numbackends, max_connections, superuser_reserved_connections, and reserved_connections integer fields. reserved_connections is zero on PostgreSQL versions where that setting does not exist.

numbackends is sum(pg_stat_database.numbackends) across the server. Stock catalog permissions let an ordinary role observe other established roles and databases without pg_monitor, pg_read_all_stats, a grant, or an installed function. It is intentionally conservative: autovacuum and other database-attached workers can be included even when they do not consume a max_connections client slot. The snapshot is not a reservation, so callers must retain a safety margin and tolerate concurrent change.

The helper executes one read-only statement through the Queue's normal connection lease, core lock, and retry path. It does not use sidecar or create database objects. A target-resolved persistent Queue reuses its existing process-session checkout on that thread; an ephemeral Queue may open one connection for the operation. Malformed result data raises ValueError; database and permission failures retain SimpleBroker's database exception types.

Multi-Queue Activity Waiters

Postgres supports simplebroker.create_activity_waiter_for_queues(...) with one process-local shared LISTEN/NOTIFY listener per DSN and schema. The waiter wakes when any watched queue receives activity, ignores unrelated queue notifications, and returns the same ActivityWaiter | None shape as the core API.

Wakeups are hints. After wait(timeout) returns True, callers should still drain queues through normal SimpleBroker reads or moves. Close the multi-queue waiter explicitly when the watcher lifecycle ends. Its first close() is terminal before cleanup; every later call is a no-op, including when the first call raised. The waiter owns registrations, not the runner or shared listener, and does not expose shutdown().

CLI Usage

Create .broker.toml in the project root, or use the configured BROKER_PROJECT_CONFIG_PATH / BROKER_PROJECT_CONFIG_NAME location:

version = 1
backend = "postgres"
target = "postgresql://postgres@127.0.0.1:54329/simplebroker_test"

[backend_options]
schema = "simplebroker_app"

Then use the normal CLI from any child directory with project scope enabled:

broker init
broker write jobs hello
broker read jobs

You can also run entirely from environment variables without a project config:

BROKER_BACKEND=postgres \
BROKER_BACKEND_TARGET='postgresql://postgres@127.0.0.1:54329/simplebroker_test' \
BROKER_BACKEND_SCHEMA='simplebroker_app' \
BROKER_BACKEND_PASSWORD='postgres' \
broker init

Notes:

  • In env-only backend configuration, BROKER_BACKEND_TARGET overrides the host/port/user/database parts.
  • BROKER_BACKEND_HOST, BROKER_BACKEND_PORT, BROKER_BACKEND_USER, BROKER_BACKEND_PASSWORD, and BROKER_BACKEND_DATABASE are only used when there is no target from project config or env.
  • When project TOML provides the target or schema, the project file wins. BROKER_BACKEND_PASSWORD can still be supplied from env and is never written to project TOML.
  • The Postgres database must already exist. broker init creates the managed schema/tables inside that database; it does not create the database itself.
  • Missing backend/plugin errors are distinct from target/auth errors. Invalid schema names, bad passwords, malformed targets, and missing databases are reported as validation or connection failures, not as "backend not available" errors.

Metadata

Release files for simplebroker-pg 4.0.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 simplebroker-pg 4.0.0
File Size Uploaded
simplebroker_pg-4.0.0.tar.gz 29.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for simplebroker-pg 4.0.0
File Interpreter ABI Platform
simplebroker_pg-4.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 68.5 kB

Release files / simplebroker_pg-4.0.0.tar.gz

Download URL simplebroker_pg-4.0.0.tar.gz
Size 29.9 kB
Tags Source
SHA-256 checksum
How to use checksums
12a26e83731546293957b11f3cd612c7eb1b7993391830075c9413cf155af65e
BLAKE2b-256 checksum
How to use checksums
c32170dd89ff9856b1182a530ae0bf7bcd4d269acff607b8fc988267d2828476
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 Aug 28, 2026.

Transparency log

Release files / simplebroker_pg-4.0.0-py3-none-any.whl

Download URL simplebroker_pg-4.0.0-py3-none-any.whl
Size 38.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
21810d87cabeda3eff0f8d0cd866030fda3c33f4c3b8a7b0ffc541bfd32e7279
BLAKE2b-256 checksum
How to use checksums
86f13218e09b9c34397b4f859fad9ef9b2f17080648b427c7e4abaf55af50494
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 Aug 28, 2026.

Transparency log

Release history Release notifications | RSS feed

4.5.1

2 release files

4.5.0

2 release files

4.4.0

2 release files

4.3.1

2 release files

4.3.0

2 release files

4.2.1

2 release files

4.2.0

2 release files

4.1.1

2 release files

4.1.0

2 release files

This release

4.0.0 This release

2 release files

3.10.0

2 release files

3.9.2

2 release files

3.9.1

2 release files

3.9.0

2 release files

3.8.0

2 release files

3.6.0

2 release files

3.5.2

2 release files

3.5.1

2 release files

3.5.0

2 release files

3.3.2

2 release files

3.3.1

2 release files

3.3.0

2 release files

3.2.2

2 release files

3.2.1

2 release files

3.2.0

2 release files

3.1.1

2 release files

3.1.0

2 release files

3.0.0

2 release files

2.5.0

2 release files

2.4.0

2 release files

2.3.0

2 release files

2.2.1

2 release files

2.2.0

2 release files

2.1.0

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.6.1

2 release files

1.6.0

2 release files

1.5.1

2 release files

1.5.0

2 release files

1.4.2

2 release files

1.4.1

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.9

2 release files

1.0.7

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.1

2 release files

1.0.0

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