liveBook · Manning

Pgmq

As explained in Just Use Postgres!

pgmq is a PostgreSQL extension that provides persistent message-queue functionality for application messages and events.

pgmq is a PostgreSQL extension for persistent application messages, events, and tasks. It supplies a ready-to-use lightweight message queue with an API aligned to AWS SQS, avoiding the implementation work of building queue tables, locking, and failure handling from scratch. section 11.6 section 11.8

Use the pgmq schema functions to:

Queue DMV visitors

Create a queue with SELECT pgmq.create('visitors_queue'); then enqueue a visitor with SELECT pgmq.send(queue_name => 'visitors_queue', msg => '{"service":"car_registration","visitor":"Olivia Morgan"}'). Use pgmq.pop('visitors_queue') when an officer is ready; it returns the message and removes it. For recoverable processing, call pgmq.read(queue_name => 'visitors_queue', vt => 120, qty => 1). The selected visitor remains hidden for two minutes, reappears if unprocessed, and can later be archived with pgmq.archive(queue_name => 'visitors_queue', msg_id => 2). section 11.7.1 section 11.7.2

Listing 11.6 Starting Postgres with pgmq on Unix
docker volume create postgres-pgmq-volume

docker run --name postgres-pgmq \
    -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=password \
    -p 5432:5432 \
    -v postgres-pgmq-volume:/var/lib/postgresql/data \
    -d ghcr.io/pgmq/pg17-pgmq:v1.5.1

Start a development PostgreSQL container using the pg17-pgmq:v1.5.1 image, then connect with psql and run CREATE EXTENSION pgmq;. section 11.6

How Just Use Postgres! covers it

How deep it goes
Explains installation, queue functions, JSONB records, immediate deletion, visibility timeouts, failover, and archiving without deep production administration.
Examples
Demonstrates a DMV visitor queue using SQL commands for creation, sending, popping, reading, timeout recovery, and archiving.
What it assumes
Assumes access to PostgreSQL and psql while teaching extension deployment, queue creation, message operations, and timeout behavior.
What you can do after
Operate/configure a pgmq queue in PostgreSQL, send and consume JSONB messages, and handle failed processing with visibility timeouts.
How it connects
Connects PostgreSQL, message queues, AWS SQS, JSONB, visibility timeouts, queue partitioning, and message-processing failover.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Just Use Postgres!. The section links point to the parts of the book it draws on.