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
- Thread-safe lock-free min where both operands can change c++
- In Rust, what is the lock-free alternative of Arc<RefCell<T>> if T is already Sync & lock-free?
- Prevent reordering of prefetch instruction in c++
- What can be inferred according to the result of atomic operations?
- How can atomicModifyIORef cause leaks? And why does atomicModifyIORef' solve the problem?
- Critical section control with atomics
- Handling Concurrency, Overflow, and Periodic Draining in a Rust HashMap Collection
- Atomic Operations do not provide updated value to other threads
- memory order with multiple stores
- Why std::mutex of c++11 has no memory order?
- Pausing threads for synchronized operation
- EJB transactions behaving differently on Wildfly 8 between Windows and Linux deployments
- Atomically reorder huge list of documents in firestore
- Atomic increment does not work as expected in interrupt
- Most efficient way to signal consumer thread from multiple producer threads using condition variables
Related Questions in POSTGRESQL-9.2
- Alter entries of Primary key in table to match number of rows from COUNT Postgresql
- Merge two rows with both possibly being null
- postgres database recovery - version 9.2
- Upgrading PostgreSQL 9.2.15 to 16.1 - Incremental Version Updates?
- How to optimize this postgresql query which is running for more than 24 hours.it process 60,000 data inside the loop
- Need help configuring PostgreSQL connection to behave like MySQL: Connect to server without auto-selecting default database
- unable to drop ERROR: unknown compresstype "quicklz" unable to drop
- PGSQL 9.2 / OpenErp 7 / Restore database from plain directory
- XML validation using XSD in postgres procedure
- User's Online Time and Period with no Users Online
- How to configure Postgres to never rotate or truncate a log file?
- insert if update subquery returns 0 rows
- Pre-define groups for PostgreSQL query
- Upgraded pgAdmin shows warning, not displaying Tables list, queries working without problem
- pg_ctl: directory "C:/Program Files/PostgreSQL/13/data" does not exist The reload command returned an error (1)
Related Questions in INTERLOCKED
- Are there any thread safety issues in volatile reading value written with Interlocked.Exchange in this code?
- C# Interlocked Increment/Decrement on Native Int
- Why Interlocked.Read uses Interlocked.CompareExchange internally?
- Inconsistency between Volatile and Interlocked
- How does Interlocked.MemoryBarrierProcessWide() change the execution order in multicore?
- How to use Interlocked.CompareExchange as an inter-process lock?
- InterlockedAdd not modifying buffer
- Interlocked Class: read before write race condition problem
- Interlocked.Exchange influence on following instructions
- Is Interlock.CompareExchange atomic inside an if statement?
- Is it safe to use the Interlocked.CompareExchange as a lock, only for enter, not for exit?
- Thread safe mechanism to swap immutable references
- Thread safe id generation with Interlocked in c#
- What is the best way to access an integer value from multiple threads?
- Read int value in thread safe way
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 # Hahtags
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.)