I wanna lock one row by some user until he work with this row on indefinitely time and he must unlock it when done. So any others users will not be able to lock this row for yourself. It is possible to do on data base level?
PostgreSQL Lock Row on indefinitely time
2.2k Views Asked by Miles At
1
There are 1 best solutions below
Related Questions in ATOMIC
- pass a structure to gcc built-ins for atomic accesses
- Atomic variables other than c++11 and boost
- How to use std::atomic<>
- Should load-acquire see store-release immediately?
- Using compare and set against multiple expected values with AtomicReference
- Penalty of AtomicVariables over locks in Java
- Extremely fast access to an array used by multiple threads?
- Understanding Xcode crash message and @synchronized directive
- ConcurrentHashMap atomic get, increment and replace
- Atomic/not-atomic mix, any guarantees?
- Do we need to declare a property atomic if we use GCD?
- C++ atomic list container
- Is simple getter call on volatile variable atomic operation?
- Largest data type which can be fetch-ANDed atomically?
- Synchronizing access to data using a "got there first" flag, instead of a lock/mutex
Related Questions in POSTGRESQL-9.2
- If else Condition in PostgreSql
- How to display field if entry exists in join table?
- SQL CTE Syntax to DELETE / INSERT rows
- Get nearest datetime from another table
- intersection interval dates between two tables
- postgres Query update decimal increment + 1
- Postgres: getting the maximum and minimum values, and timestamps they occur
- What is the best way to replace a string with another string on entire table?
- How to CAST a value in PostgreSQL for use in WHERE with LIKE statement?
- editing postgresql.conf file on postgreSQL 9.2?
- How to replace a value in array
- postgresql find the average time between 2 or more date
- postgresql date diff between two date with two table and two fields
- Postgres Throws Error When using Two select statement in One query
- Postgresql format() function alternative
Related Questions in INTERLOCKED
- Best, thread safe way to access a field once i.e. for disposal
- InterlockedPushEntrySList's Performance
- Why as increase thread contetion Interlocked is slower?
- Are variables used by System.Threading.Timer subject to caching?
- Replacing a lock with an interlocked operation
- WaitForSingleObject vs Interlocked*
- Why does Interlocked.Increment give an incorrect result in a Parallel.ForEach loop?
- Meaning of "The sign is ignored" in _InterlockedCompareExchange documentation
- Possible to create AtomicReference that can be swapped atomically?
- WinAPI _Interlocked* intrinsic functions for char, short
- How does Interlocked work and why is it faster than lock?
- ConcurrentQueue one element is taken by two threads
- InterlockedExchange Visual Studio 2010 Intrinsic
- Interlocked.Increment vs lock in debug vs release mode
- Interlocked read on readonly memory page triggers access violation
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
You can do it with a long-lived transaction, but there'll be performance issues with that. This sounds like more of a job for optimistic concurrency control.
You can just open a transaction and do a
SELECT 1 FROM mytable WHERE clause to match row FOR UPDATE;. Then keep the transaction open until you're done. The problem with this is that it can cause issues with vacuum that result in table and index bloat, where tables get filled with deleted data and indexes fill up with entries pointing to obsolete blocks.It'd be much better to use an advisory lock. You still have to hold the connection the holds the lock open, but it doesn't have to keep an open idle transaction, so it's much lower impact. Transactions that wish to update the row must explicitly check for a conflicting advisory lock, though, otherwise they can just proceed as if it wasn't locked. This approach also scales poorly to lots of tables (due to limited advisory lock namespace) or lots of concurrent locks (due to number of connections).
You can use a trigger to check for the advisory lock and wait for it if you can't make sure your client apps will always get the advisory lock explicitly. However, this can create deadlock issues.
For that reason, the best approach is probably to have a
locked_byfield that records a user ID, and alocked_timefield that records when it was locked. Do it at the application level and/or with triggers. To deal with concurrent attempts to obtain the lock you can use optimistic concurrency control techniques, where theWHEREclause on theUPDATEthat setslocked_byandlocked_timewill not match if someone else gets there first, so the rowcount will be zero and you'll know you lost the race for the lock and have to re-check. ThatWHEREclause usually testslocked_byandlocked_time. So you'd write something like:(This is a simplified optimistic locking mode for grabbing a lock, where you don't mind if someone else jumped in and did an entire transaction. If you want stricter ordering, you use a row-version column or you check that a last_modified column hasn't changed.)