Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,21 @@ More flexible [PGMQ Postgres extension](https://github.com/tembo-io/pgmq) Python
* [Getting Started](#getting-started)
* [Postgres Setup](#postgres-setup)
* [Usage](#usage)
* [Transaction Usage](#transaction-usage)
* [FastAPI Pub/Sub Example with tests](#fastapi-pubsub-example)
* [Issue/ Contributing / Development](#issue-contributing--development)
* [TODO](#todo)


## Features

- Supports **async** and **sync** `engines` and `sessionmakers`, or built from `dsn`.
- **Automatically** creates `pgmq` (or `pg_partman`) extension on the database if not exists.
- Supports **all postgres DBAPIs supported by sqlalchemy**.
> e.g. `psycopg`, `psycopg2`, `asyncpg` .. <br>
> See [SQLAlchemy Postgresql Dialects](https://docs.sqlalchemy.org/en/20/dialects/postgresql.html)
- **Transaction-friendly operations** via the `op` module for combining PGMQ with your business logic in the same transaction.
- [Fully tested across all **supported DBAPIs** in both **async** and **sync** modes](https://github.com/jason810496/pgmq-sqlalchemy/actions/workflows/codecov.yml).
- Battle-tested with **[real-world FastAPI Pub/Sub examples](./examples/fastapi_pub_sub/README.md)** and **[corresponding tests](https://github.com/jason810496/pgmq-sqlalchemy/actions/workflows/examples.yml)**.

## Installation

Expand Down Expand Up @@ -153,7 +156,21 @@ with SessionLocal() as session:

> See [Transaction Usage Documentation](https://pgmq-sqlalchemy.readthedocs.io/en/latest/getting-started.html#using-transaction-friendly-operations) for more examples.

### FastAPI Pub/Sub Example with tests

See the [FastAPI Pub/Sub Example](./examples/fastapi_pub_sub/README.md) for a complete example of using `pgmq-sqlalchemy` in a FastAPI application with asynchronous message consumption and tests.

## Issue/ Contributing / Development

Welcome to open an issue or pull request ! <br>
See [`Development` on Online Document](https://pgmq-sqlalchemy.readthedocs.io/en/latest/) or [CONTRIBUTING.md](.github/CONTRIBUTING.md) for more information.

## TODO

- [ ] [Alembic](https://alembic.sqlalchemy.org/en/latest/) compatible migration scripts for PGMQ extension and schema setup, upgrade, downgrade.
- [ ] Compatibility tests with PGMQ across different PGMQ versions.
- [ ] More examples
- [ ] Smoothen contributing process with custom script for one step setup
- [ ] Mypy strict type checking
- [ ] Enable more ruff rules
- [ ] Drop Python 3.9 support in next minor release
178 changes: 178 additions & 0 deletions doc/example-with-fastapi-pub-sub.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
FastAPI Pub/Sub Example with PGMQ
=================================

.. note::

You can find the complete code for this example in `examples/fastapi_pub_sub <https://github.com/jason810496/pgmq-sqlalchemy/tree/main/examples/fastapi_pub_sub>`_.

This example demonstrates a real-world scenario of using PGMQ with
FastAPI for an order management system. It shows how to:

- Use PGMQ with FastAPI and sync SQLAlchemy sessions (psycopg2)
- Publish messages using ``PGMQOperation`` (``op``) in a web API
- Consume messages asynchronously using ``PGMQueue`` with ``asyncpg``


Scenario
--------

1. Client creates an order via the API.
2. The API saves the order to the database and publishes a message to
PGMQ within the same transaction.
3. The consumer polls the queue, processes messages, deletes the
message on success, or leaves it for retry on failure.

- We **simulate** that if ``msg_id % 6 == 0``, it will fail twice
before succeeding, and if ``msg_id % 2 == 0``, it will fail once
before succeeding.


Architecture
------------

- **API Server** (``api.py``): FastAPI application that creates orders
and publishes them to PGMQ

- Uses sync database driver (psycopg2)
- Uses ``PGMQOperation`` (imported as ``op``) for publishing messages
- Provides REST endpoints for creating and retrieving orders
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This line uses a tab character for indentation, while the surrounding lines use spaces. In reStructuredText, mixing tabs and spaces can lead to inconsistent rendering or errors. Please replace the tab with spaces to ensure consistent formatting.

Suggested change
- Provides REST endpoints for creating and retrieving orders
- Provides REST endpoints for creating and retrieving orders


- ``POST /orders`` – Create a new order
- ``GET /orders/{order_id}`` – Get order by ID
- ``GET /health`` – Health check endpoint

- **Consumer** (``consumer.py``): Async worker that processes orders
from the queue

- Uses async database driver (``asyncpg``)
- Uses ``PGMQueue`` class for reading messages
- Processes messages concurrently with ``asyncio``

- **Create Orders Script** (``create_orders_coordinator.py``): Script
to create multiple orders in parallel via the API for testing


Prerequisites
-------------

- PostgreSQL with PGMQ extension installed
- Python 3.10 or higher

Quick setup::

docker run -d --name postgres -e POSTGRES_PASSWORD=postgres -p 5432:5432 quay.io/tembo/pg16-pgmq:latest

Install dependencies from the project root with ``uv``::

cd /path/to/pgmq-sqlalchemy
uv ync --group dev
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There's a typo in the uv command. It should be uv sync instead of uv ync.

Suggested change
uv ync --group dev
uv sync --group dev



Running the Example with Pytest
--------------------------------

You can run the entire example (API server, consumer, and order
creation) using the provided pytest script

.. code-block:: bash

cd /path/to/pgmq-sqlalchemy
uv run pytest ./examples_tests/integration/test_fastapi_integration.py -ss

This command starts the API server and consumer, and creates orders
automatically. You will see **logs from all three processes in real time
in the terminal**

.. code-block:: text

========================================================================================================================= test session starts ==========================================================================================================================
platform darwin -- Python 3.12.12, pytest-7.4.4, pluggy-1.6.0
rootdir: /Users/jason/pgmq-sqlalchemy
configfile: pyproject.toml
plugins: asyncio-0.23.8, anyio-4.12.0, xdist-3.8.0, lazy-fixture-0.6.3, cov-7.0.0
asyncio: mode=Mode.AUTO
collected 1 item

examples_tests/integration/test_fastapi_integration.py
examples_tests/integration/test_fastapi_integration.py
Starting API process...
API process started with PID: 95531
Starting Consumer process...
Consumer process started with PID: 95532
Starting Create Orders process...
Create Orders process started with PID: 95533


╭───────────────────────────────── API process: 12653 ─────────────────────────────────╮╭────────────────────────────── Consumer process: 12654 ───────────────────────────────╮╭──────────────────────────── Create Orders process: 12655 ────────────────────────────╮
│ Starting API process... ││ Starting Consumer process... ││ Starting Create Orders process... │
│ INFO: 127.0.0.1:54358 - "GET /health HTTP/1.1" 200 OK ││ [2026-01-07 19:48:03][INFO] - Starting consumer for queue: order_queue ││ [2026-01-07 19:48:03][INFO] - API Server is not ready... │
│ INFO: Started server process [12653] ││ [2026-01-07 19:48:03][INFO] - Batch size: 30, Visibility timeout: 10s ││ [2026-01-07 19:48:03][INFO] - API Server is not ready... │
│ INFO: 127.0.0.1:54359 - "POST /orders HTTP/1.1" 201 Created ││ [2026-01-07 19:48:04][INFO] - Received 30 messages ││ [2026-01-07 19:48:03][INFO] - API Server is not ready... │
│ INFO: Waiting for application startup. ││ [2026-01-07 19:48:05][INFO] - Order 111 processed fail at first try ││ [2026-01-07 19:48:03][INFO] - API Server is not ready... │
│ INFO: 127.0.0.1:54360 - "POST /orders HTTP/1.1" 201 Created ││ [2026-01-07 19:48:05][INFO] - Order 102 processed fail at first try ││ [2026-01-07 19:48:03][INFO] - HTTP Request: GET http://localhost:8000/health │
│ INFO: Application startup complete. ││ [2026-01-07 19:48:05][INFO] - Order 116 processed fail at first try ││ "HTTP/1.1 200 OK" │
│ INFO: 127.0.0.1:54368 - "POST /orders HTTP/1.1" 201 Created ││ [2026-01-07 19:48:05][INFO] - Order 122 processed fail at first try ││ [2026-01-07 19:48:03][INFO] - API Server is ready! │
│ INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit) ││ [2026-01-07 19:48:05][INFO] - Order 130 processed fail at first try ││ [2026-01-07 19:48:03][INFO] - HTTP Request: POST http://localhost:8000/orders │
│ INFO: 127.0.0.1:54363 - "POST /orders HTTP/1.1" 201 Created ││ [2026-01-07 19:48:05][INFO] - Order 104 processed fail at first try ││ "HTTP/1.1 201 Created" │
│ INFO: 127.0.0.1:54362 - "POST /orders HTTP/1.1" 201 Created ││ [2026-01-07 19:48:05][INFO] - Order 119 processed fail at first try ││ [2026-01-07 19:48:03][INFO] - HTTP Request: POST http://localhost:8000/orders │
│ INFO: 127.0.0.1:54369 - "POST /orders HTTP/1.1" 201 Created ││ [2026-01-07 19:48:05][INFO] - Order 108 processed fail at first try ││ "HTTP/1.1 201 Created" │
│ INFO: 127.0.0.1:54364 - "POST /orders HTTP/1.1" 201 Created ││ [2026-01-07 19:48:05][INFO] - Order 118 processed fail at first try ││ [2026-01-07 19:48:03][INFO] - HTTP Request: POST http://localhost:8000/orders │
│ INFO: 127.0.0.1:54366 - "POST /orders HTTP/1.1" 201 Created ││ [2026-01-07 19:48:05][INFO] - Order 127 processed fail at first try ││ "HTTP/1.1 201 Created" │
│ INFO: 127.0.0.1:54372 - "POST /orders HTTP/1.1" 201 Created ││ [2026-01-07 19:48:05][INFO] - Order 109 processed fail at first try ││ [2026-01-07 19:48:03][INFO] - HTTP Request: POST http://localhost:8000/orders │
│ INFO: 127.0.0.1:54367 - "POST /orders HTTP/1.1" 201 Created ││ [2026-01-07 19:48:05][INFO] - Order 128 processed fail at first try ││ "HTTP/1.1 201 Created" │
│ INFO: 127.0.0.1:54370 - "POST /orders HTTP/1.1" 201 Created ││ [2026-01-07 19:48:05][INFO] - Order 112 processed fail at first try ││ [2026-01-07 19:48:03][INFO] - HTTP Request: POST http://localhost:8000/orders │
│ INFO: 127.0.0.1:54373 - "POST /orders HTTP/1.1" 201 Created ││ [2026-01-07 19:48:05][INFO] - Order 124 processed fail at first try ││ "HTTP/1.1 201 Created" │
│ INFO: 127.0.0.1:54365 - "POST /orders HTTP/1.1" 201 Created ││ [2026-01-07 19:48:05][INFO] - Order 113 processed fail at first try ││ [2026-01-07 19:48:03][INFO] - HTTP Request: POST http://localhost:8000/orders │
│ INFO: 127.0.0.1:54371 - "POST /orders HTTP/1.1" 201 Created ││ [2026-01-07 19:48:05][WARNING] - Message 110 processing failed, will retry later ││ "HTTP/1.1 201 Created" │
│ INFO: 127.0.0.1:54361 - "POST /orders HTTP/1.1" 201 Created ││ [2026-01-07 19:48:05][WARNING] - Message 102 processing failed, will retry later ││ [2026-01-07 19:48:03][INFO] - HTTP Request: POST http://localhost:8000/orders │
│ INFO: 127.0.0.1:54376 - "POST /orders HTTP/1.1" 201 Created ││ [2026-01-07 19:48:05][WARNING] - Message 116 processing failed, will retry later ││ "HTTP/1.1 201 Created" │
│ INFO: 127.0.0.1:54380 - "POST /orders HTTP/1.1" 201 Created ││ [2026-01-07 19:48:05][WARNING] - Message 122 processing failed, will retry later ││ [2026-01-07 19:48:03][INFO] - HTTP Request: POST http://localhost:8000/orders │
│ INFO: 127.0.0.1:54377 - "POST /orders HTTP/1.1" 201 Created ││ [2026-01-07 19:48:05][WARNING] - Message 130 processing failed, will retry later ││ "HTTP/1.1 201 Created" │
│ INFO: 127.0.0.1:54379 - "POST /orders HTTP/1.1" 201 Created ││ [2026-01-07 19:48:05][WARNING] - Message 104 processing failed, will retry later ││ [2026-01-07 19:48:03][INFO] - HTTP Request: POST http://localhost:8000/orders │
│ INFO: 127.0.0.1:54393 - "POST /orders HTTP/1.1" 201 Created ││ [2026-01-07 19:48:05][WARNING] - Message 120 processing failed, will retry later ││ "HTTP/1.1 201 Created" │
│ INFO: 127.0.0.1:54394 - "POST /orders HTTP/1.1" 201 Created ││ ││ [2026-01-07 19:48:03][INFO] - HTTP Request: POST http://localhost:8000/orders │
╰──────────────────────────────────────────────────────────────────────────────────────╯╰──────────────────────────────────────────────────────────────────────────────────────╯╰──────────────────────────────────────────────────────────────────────────────────────╯

Configuration
-------------

You can modify the following environment variables before running the
example:

- ``DATABASE_URL``: PostgreSQL connection string
- ``QUEUE_NAME``: Name of the PGMQ queue (default: ``"order_queue"``)
- ``BATCH_SIZE``: Number of messages to process in each batch
(``consumer.py``)
- ``VT``: Visibility timeout in seconds (``consumer.py``)
- ``API_PORT``: Port for the FastAPI server (default: ``8000``)


How It Works
------------

1. When an order is created via the API:

- The order is saved to the database and published to PGMQ **within
the same transaction** by using ``op.send()``.
- The message contains order details.

2. The consumer:

- Continuously polls the queue for new messages.
- Processes messages concurrently using ``asyncio``.
- Deletes successfully processed messages.
- Leaves failed messages in the queue for retry.


Testing
-------

This example is covered by an integration test located at
`examples_tests/integration/test_fastapi_integration.py <https://github.com/jason810496/pgmq-sqlalchemy/blob/main/examples_tests/integration/test_fastapi_integration.py>`_. It is run
end to end with ``pytest`` for every pull request to ensure correctness
and reliability. The GitHub Actions workflow configuration for running
the tests is located in `.github/workflows/examples.yml <https://github.com/jason810496/pgmq-sqlalchemy/blob/main/.github/workflows/examples.yml>`_.

You can refer to that workflow file for more details on how to set up
and run the tests.

9 changes: 8 additions & 1 deletion doc/getting-started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ Or using **Docker Compose** to start Postgres with ``PGMQ`` extension:
``docker-compose.yml``:
.. code-block:: yaml

version: '3.8'
services:
pgmq_postgres:
container_name: pgmq_postgres
Expand Down Expand Up @@ -264,4 +263,12 @@ Combining business logic with PGMQ operations in a single transaction:
See `API Reference <api-reference>`_ for the complete list of available operations in the ``op`` module.


FastAPI Pub/Sub Example
-----------------------

For a complete, real-world example that combines ``PGMQOperation`` (``op``)
with FastAPI and ``PGMQueue`` for asynchronous consumption, see
`FastAPI Pub/Sub Example with PGMQ <example-with-fastapi-pub-sub>`_.



6 changes: 4 additions & 2 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ Features
--------

* Supports **async** and **sync** ``engines``, ``sessionmakers``, or directly constructed from ``dsn``.
* **Automatically** creates ``pgmq`` extension on the database if not exists.
* Supports all Postgres DBAPIs supported by ``SQLAlchemy``.
* Examples: ``psycopg``, ``psycopg2``, ``asyncpg``
* See `SQLAlchemy Postgresql Dialects <https://docs.sqlalchemy.org/en/20/dialects/postgresql.html>`_

* **Transaction-friendly operations** via the `op` module for combining PGMQ with your business logic in the same transaction.
* `Fully tested across all supported DBAPIs in both async and sync modes <https://github.com/jason810496/pgmq-sqlalchemy/actions/workflows/codecov.yml>`_.
* Battle-tested with `real-world FastAPI Pub/Sub examples <https://github.com/jason810496/pgmq-sqlalchemy/tree/main/examples/fastapi_pub_sub/README.md>`_ and `corresponding tests <https://github.com/jason810496/pgmq-sqlalchemy/actions/workflows/examples.yml>`_.

Table of Contents
-----------------
Expand All @@ -41,6 +42,7 @@ Table of Contents
self
installation
getting-started
example-with-fastapi-pub-sub
api-reference
development
release
Loading