About this project

PGMB (Postgres Message Broker) is a message broker and event log built on PostgreSQL. It performs most computational work inside Postgres and leaves I/O to the JavaScript runtime, providing at-least-once delivery with mostly ordered messages. Core capabilities include queue/exchange-style behavior similar to AMQP systems with retries and reliable delivery, batch publishing, automatic insert/update/delete events for any table, HTTP Server-Sent Events (SSE) for arbitrary Postgres queries with resumption via Last-Event-ID, and webhooks with retry logic. The package ships as an ESM npm module with a type-safe TypeScript client. Events are stored in an insert-only pgmb.events table, while subscriptions define SQL conditions and JSONB parameters that determine matching events. Subscriptions are grouped by group_id, and identical condition SQL is automatically shared to reduce duplicate work. Reliable handlers support retry schedules, expiry intervals, and partitioning for scaling consumers. Fire-and-forget handlers provide a simpler async iterator API. Automatic table events are enabled through pgmb.push_table_mutations(), which creates triggers that publish insert, update, and delete events transactionally. A custom serialise_record_for_event function can filter or transform emitted payloads. SSE support is framework-agnostic and includes replay limits and dummy event IDs for safe reconnection. Webhook support includes configurable retries, timeouts, gzip compression, idempotency keys, and optional Standard Webhooks HMAC-SHA256 signing. Operationally, PGMB can use pg_cron for periodic poll_for_events() and maintain_events_table() jobs, or run them in-process. Advisory locks prevent duplicate delivery when multiple instances share a group_id. The README includes benchmarks comparing PGMB with PGMQ and RabbitMQ, and notes compatibility with PGLite and limited environments without superuser access.