Skip to content

Row update with DB Row Lock (SELECT FOR UPDATE) #1918

Closed

Description

@flybayer

Problem

There's an entire class of DB mutations that can be affected by race conditions. An example is incrementing a value by one.

So when updating a row, we need a way to acquire a DB row lock to prevent these types of race conditions.

Solution

I think this could be a good solution. If data is a function, then SELECT FOR UPDATE is used.

await prisma.counter.update({where: {id: 1}, data: counter => ({value: counter.value + 1})})

Alternatives

Or accept the above arguments with a new API like prisma.count.updateWithLock()

Activity

  1. schickling commented on Mar 24, 2020

    @schickling
    Member

    We've already drafted a Client API improved for this use case in this issue:

    await prisma.users.findMany().update({ version: u => u.version.inc(5) })
    await prisma.users.findMany().update({ version: u => u.version.dec(5) })
    await prisma.users.findMany().update({ version: u => u.version.mul(5) })
    await prisma.users.findMany().update({ version: u => u.version.div(5) })
    await prisma.users.findMany().update({ version: u => u.version.mod(5) })
    await prisma.users.findMany().update({ version: u => u.version.pow(5, 10) })
    await prisma.users.findMany().update({ version: u => u.version.min(5, 10) })
    await prisma.users.findMany().update({ version: u => u.version.max(5, 10) })

    This won't be implemented using a DB lock but by mapping to native atomic DB operations.

  2. added
    kind/feature
    on Mar 25, 2020
  3. yovanoc commented on Mar 29, 2020

    @yovanoc

    Ohhh look very nice! Any timeline for a v1 of this draft?

  4. steebchen commented on Apr 23, 2020

    @steebchen
    Contributor

    related for number-like types: prisma/prisma-client-js#655

  5. albertoperdomo commented on Jul 8, 2020

    @albertoperdomo
    Contributor

    Thanks for the feature request @flybayer!

    As mentioned earlier in the issue, we are planning to tackle atomic operations natively in prisma/prisma-client-js#655.

    If you still feel the need for acquiring DB row locks for other operations, please feel free to open a new feature request.

  6. Sytten commented on Jul 8, 2020

    @Sytten

    I kind of disagree with this closing this, row locking is important and one step in providing more control to dev since long running transactions are out of the question for now. That could allow chaining of a findOne and update where the update takes some data from the findOne.

  7. janpio commented on Jul 8, 2020

    @janpio
    Contributor

    @Sytten Then we need an issue about this set of problems. As this one was explicitly about what prisma/prisma-client-js#655 covers, it would be invalid to keep this around. We are happy to look into other problems that require this, and might even require this general solution.

  8. JoniBR commented on Dec 7, 2022

    @JoniBR

    Why was this closed? I don't see another solution providing this :(

  9. janpio commented on Dec 7, 2022

    @janpio
    Contributor
  10. leppaott commented on Apr 30, 2024

    @leppaott

    There should be more generic solution to using SELECT .. FOR UPDATE to lock rows before an update. Atomic number operations don't quite cut it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions