Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support SKIP LOCKED for SelectStatement #142

Closed
Stumblinbear opened this issue Sep 26, 2021 · 2 comments · Fixed by #289
Closed

Support SKIP LOCKED for SelectStatement #142

Stumblinbear opened this issue Sep 26, 2021 · 2 comments · Fixed by #289

Comments

@Stumblinbear
Copy link

I'm working on implementing a job queue using Postgres, and I need to be able to add SKIP LOCKED to my select queries

@tyt2y3
Copy link
Member

tyt2y3 commented Sep 27, 2021

Is it an extension to #117 ?
But seriously, it seems to be difficult enough to done a queue right

@Stumblinbear
Copy link
Author

Stumblinbear commented Sep 27, 2021

Possibly. I'm unusure what the best route to implement it would be. With Postgres, all you need to do is add SKIP LOCKED onto a SELECT query, and it'll skip any row currently locked by a transaction. So all I have to do is: DELETE FROM "jobs" WHERE "jobs"."id" = (SELECT "id" FROM "jobs" WHERE "execute_at" < $1 ORDER BY "id" ASC FOR UPDATE SKIP LOCKED LIMIT 1) RETURNING "type_of", "data" inside of a transaction, do the work, then commit the transaction. Any other server looking for jobs will skip over any currently running jobs, and if the transaction fails or the client gets disconnected, the row gets unlocked. I'm currently using it without SKIP LOCKED (which means only one can only process one job at a time across servers, but its fine for now).

I'm currently using the transaction branch (even with the lifetime issue (which I haven't run into), it's working great).

Do let me know if this is more difficult than it seems on the surface to add support for!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants