Queues: add a pgmq_public.set_vt wrapper so Data API consumers can extend or delay a message #54363
Replies: 1 comment
|
Until a wrapper ships, you can add one without touching create schema if not exists queue_ext;
grant usage on schema queue_ext to service_role;
create or replace function queue_ext.set_vt(queue_name text, message_id bigint, vt_offset integer)
returns setof pgmq.message_record
language sql
set search_path = ''
as $$
select * from pgmq.set_vt(queue_name, message_id, vt_offset);
$$;
-- new functions are executable by PUBLIC unless you revoke it
revoke execute on function queue_ext.set_vt(text, bigint, integer) from public;
grant execute on function queue_ext.set_vt(text, bigint, integer) to service_role;
grant execute on function pgmq.set_vt(text, bigint, integer) to service_role;Then add // heartbeat: keep the message hidden for another 30 s
await supabase.schema('queue_ext').rpc('set_vt', {
queue_name: 'jobs', message_id: msgId, vt_offset: 30,
})Use a larger Notes:
Tested against the pgmq 1.4.4 and 1.5.1 SQL on a local throwaway Postgres (PGlite) with roles mimicking Supabase, not on a hosted project: the offset applied, +1 to adding |
Uh oh!
There was an error while loading. Please reload this page.
The
pgmq_publicwrappers that Studio installs when Queues are exposed through the Data API (getExposeQueuesSQLinpackages/pg-meta/src/sql/studio/database/queues.ts) coversend,send_batch,read,pop,archiveanddelete. There is no wrapper forpgmq.set_vt, so a consumer that reads through PostgREST orsupabase.schema('pgmq_public').rpc(...)cannot change a message's visibility timeout after it has read it.That rules out two common worker patterns:
sleep_seconds := 30, and the job takes longer. Withoutset_vtit cannot extend its lease, so the message becomes visible again and a second worker picks it up while the first is still running. The only choice is a long timeout up front, which delays the retry when a worker really dies.set_vt(queue, msg_id, delay)to retry after 30 s, then 2 min, and so on. Without it, the message reappears when the original timeout runs out, so every retry runs at the same interval.We maintain an open source job queue on top of pgmq. Over a direct Postgres connection it uses
pgmq.set_vtfor both. Over the Data API with onlypgmq_public, we have to document that heartbeats and backoff do nothing (limitations.mdx), or ask users to install their own wrappers in another exposed schema.Proposal
Add one wrapper next to the others, with the same grants and
search_path:It should also be added to
HIDE_QUEUES_FROM_POSTGREST_SQLso disabling exposure drops it, and the Queues API docs would list it with the others. Since the wrappers run as the caller,set_vtwould need the same update privilege and RLS policy on the queue table thatreadalready needs, because both update the message row.All reactions