publish_message
SQL 0.1.1 · Source cbff27b8a34e1384384018680e46c20d5c610cc9.
Signature
postgremq.publish_message(p_topic character varying, p_payload jsonb, p_deliver_after timestamp with time zone DEFAULT clock_timestamp(), p_group_key character varying DEFAULT NULL::character varying)
RETURNS bigint
The signature, defaults, and result above were extracted from the installed PostgreSQL catalog.
Publish with atomic fan-out
Stores one JSONB payload and deliveries to every selected live queue on the topic in the same transaction. Queues created later receive no history. A topic with no live queues still accepts a payload. deliver_after is the earliest claim time, not execution time; NULL means now. An omitted/NULL group key is ungrouped. A nonempty key establishes FIFO order within that group per queue.
Transactions and errors
PMQ02: topic missing. PMQ03: empty or over-255-character group key. SQL NULL payload raises 23502; the JSON value null is a valid JSONB payload. Grouped publishing can raise 40001 or 40P01; retry the entire transaction. A group lock lasts until commit; fan-out also holds queue references that can delay Exclusive renewal. A transport error can have an uncertain outcome.
All calls require the applicable caller privileges. See SQL errors for driver failures and uncertain outcomes.
Example
Run in psql with ON_ERROR_STOP and fresh reference-* names. ROLLBACK removes the fixtures.
BEGIN;
SELECT postgremq.create_topic('reference-events');
SELECT postgremq.create_queue('reference-jobs', 'reference-events', 5, false);
SELECT postgremq.publish_message('reference-events', '{"number":1}'::jsonb,
p_group_key => 'sequence-a');
SELECT * FROM postgremq.get_queue_statistics('reference-jobs');
ROLLBACK;
Within this transaction the queue has one new pending delivery. Other sessions do not see it unless the transaction commits.
Related: Publishing, SQL conventions.