marten icon indicating copy to clipboard operation
marten copied to clipboard

Investigate using "SELECT FOR UPDATE" for modes that do change tracking

Open CoreyKaylor opened this issue 8 years ago • 8 comments

This will help lock the row up front for updates if we already know updates are planned via change tracking. This may or may not be that big of a deal though.

CoreyKaylor avatar May 12 '16 14:05 CoreyKaylor

From a Gitter conversation today:

Another option for 4.) above is to add overloads of Load/LoadMany for LoadForUpdate(), and add a new Linq extension for IQuerySession.Query<T>().ForUpdates().Where(expression)that issues the SELECT with FOR UPDATE. Finest grained locking possible, maybe not the most user discoverable stuff.

Feedback from 2nd Quadrant is leading us to being more fine-grained about it

jeremydmiller avatar May 24 '16 23:05 jeremydmiller

I think this is good stuff! The finer lock looks better than using serializable. But this should be documented indeed!

tim-cools avatar May 25 '16 07:05 tim-cools

I took a stab at this earlier this week and it turned out to be way harder than I anticipated, just due to Marten's internals and how it generates SQL.

It would be relatively simple to do:

Load<T>(RowLock locking = RowLock.None);
LoadMany<T>(RowLock locking, int/long/Guid/string[] ids);

public enum RowLock
{
    None, 
    ForUpdate,
    ForNoKeyUpdate,
    ForShare,
    ForKeyShare
}

My question to everyone else is can we say that the above proposal is good enough for now and we'll worry about adding row locking to Linq queries later?

jeremydmiller avatar Jun 16 '16 14:06 jeremydmiller

Can we also include skip locked as an option.

PostgreSQL 9.5 comes to the rescue. There is a new way to obtain rows:

SELECT ...

FROM table

WHERE class = 'economy'

AND empty = true

LIMIT 1

FOR UPDATE SKIP LOCKED

http://highscalability.com/blog/2015/10/13/more-concurrency-improved-locking-in-postgresql.html

phillip-haydon avatar Jun 26 '16 13:06 phillip-haydon

@jeremydmiller So there is no way (hacky or otherwise) to do a row lock with Marten v3 or v4?

ghost avatar Nov 30 '21 09:11 ghost

Yes in the event store, no in the document db until this story is played. Wanna volunteer? :-) I think it'll be easier in v4 after all the changes to internals. I thought about doing this as part of v4 a couple times, but the release dragged on for too long as it was.

jeremydmiller avatar Nov 30 '21 11:11 jeremydmiller

Understood. I'll pass for now on volunteering. ;-)

And thanks for your great work! Marten has simplified quite a bit for us! So far it's the best way (I've used) to persist a DDD domain.

ghost avatar Nov 30 '21 14:11 ghost

And sadly enough, this gets kicked back yet again!

jeremydmiller avatar May 21 '22 21:05 jeremydmiller

Moving this to discussion, we can bring it back into issues when we actually look at this.

mysticmind avatar Nov 22 '23 17:11 mysticmind