Skip to content

Instantly share code, notes, and snippets.

@Gurpartap
Created February 3, 2018 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gurpartap/7f567bc921a4dcd1ffaf1e0c23fd9ac8 to your computer and use it in GitHub Desktop.
Save Gurpartap/7f567bc921a4dcd1ffaf1e0c23fd9ac8 to your computer and use it in GitHub Desktop.
SELECT
operation.*
FROM
operations operation
LEFT JOIN LATERAL (
SELECT
_r.state
FROM
operations_results _r
WHERE
_r.operation_id = operation.id
ORDER BY
_r.created_at DESC
LIMIT
1
) result ON TRUE
WHERE
operation.run_after <= now()::timestamptz
AND operation.name = $1::text
AND operation.queue = coalesce($2, '')::text
AND (result IS NULL OR result.state NOT IN ('completed', 'failed'))
ORDER BY
operation.priority DESC,
operation.run_after
LIMIT
1
FOR NO KEY UPDATE OF
operation
SKIP LOCKED
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment