delete from task
where task_id in
( select task_id
from task
order by random() -- use tablesample for better performance
for update
skip locked
limit 1
)
returning task_id, task_type, params::jsonb as params
You can avoid that by using a materialized Common Table Expression.
https://stackoverflow.com/questions/73966670/select-for-upda...