Skip to main content

consume_message

SQL 0.1.1 · Source cbff27b8a34e1384384018680e46c20d5c610cc9.

Signature​

postgremq.consume_message(p_queue_name character varying, p_vt integer, p_limit integer DEFAULT 1, p_generation uuid DEFAULT NULL::uuid)
RETURNS TABLE(queue_name character varying, message_id bigint, payload jsonb, consumer_token character varying, delivery_attempts integer, vt timestamp with time zone, published_at timestamp with time zone, group_key character varying, group_seq bigint)

The signature, defaults, and result above were extracted from the installed PostgreSQL catalog.

Claim eligible deliveries​

Returns up to the batch limit, skipping locked candidates. Each row becomes processing with a fresh ownership token, incremented attempt count, and deadline now + p_vt seconds. Only the earliest unsettled member of a group can be claimed; ungrouped delivery is not strict FIFO. Return no rows when nothing is eligible. Pass a generation to bind the call to one queue incarnation.

Transactions and errors​

PMQ02: missing/expired queue or generation mismatch. PMQ03: negative timeout or nonpositive batch size. NULL batch is unbounded; avoid it. A NULL VT can raise 23502 when a row is claimed. A zero SQL timeout is allowed but immediately eligible for recovery. Claim locks last to transaction end. Commit promptly before external work. Does not renew Exclusive lifetime.

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);
SELECT * FROM postgremq.consume_message('reference-jobs', 30, 1);
ROLLBACK;

The first claim returns attempt 1 with payload, token, and deadline. This inspection example rolls back; a working consumer commits the claim and retains its token.

Related: Using SQL, SQL conventions.