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

Add support for row-level locking (via FOR UPDATE SKIP LOCKED) #5983

Open
milancermak opened this issue Mar 4, 2021 · 3 comments
Open

Add support for row-level locking (via FOR UPDATE SKIP LOCKED) #5983

milancermak opened this issue Mar 4, 2021 · 3 comments
Labels
domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. kind/feature A request for a new feature. topic: database-functionality topic: locking

Comments

@milancermak
Copy link

I have a table that acts as a job queue. I want to read the first row where processed_at is null. There can be multiple workers reading from the same table. To ensure only a single worker is processing one job, I can use row-level locking. In postgres, I can do:

begin;

-- 1)
select * from job_queue where processed_at is null order by created_at limit 1 for update skip locked;
-- returns a job with job_id = 42

-- 💻 ⏳ 

-- 2)
update job_queue set processed_at = now() where job_id = 42;

commit;

In 1), it returns the data associated with the job, but still keeps the transaction open. If there is another worker running the same select, it would not get back a job with job_id = 42.

Between 1) and 2), I want to do some work on the server and call update only when that work is done. I didn't find a way how this is currently possible with Prisma (I've read the Transaction guide).

I imagine this is a significant feature to implement, but is that something on the roadmap?

@pantharshit00 pantharshit00 added kind/feature A request for a new feature. domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. topic: database-functionality labels Mar 6, 2021
@meotimdihia
Copy link

meotimdihia commented May 13, 2021

Yes, this feature is missing on Prisma that I can't completely replace Sequelize for Prisma.

https://www.2ndquadrant.com/en/blog/what-is-select-skip-locked-for-in-postgresql-9-5/

@anonrig
Copy link

anonrig commented Jul 11, 2021

Any news on this issue? FOR UPDATE SKIP LOCKED is an important feature.

@rankjie
Copy link

rankjie commented Sep 22, 2022

I ran into a situation where I needed to gain an X lock at the start of a transaction with a find query since the default behavior from Prisma is to gain an S lock, which would cause deadlocks in race conditions.

I ended up with $queryRaw, hope there is an option for find methods to specify lockings, especially in transactions.

@janpio janpio changed the title Add support for row-level locking Add support for row-level locking (via FOR UPDATE SKIP LOCKED) Jun 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. kind/feature A request for a new feature. topic: database-functionality topic: locking
Projects
None yet
Development

No branches or pull requests

6 participants