Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a locked row [in Postgres] still be read from?

If I SELECT... FOR UPDATE a row in a transaction, it will obviously block the row from being written to, but will it disallow reads as well? I'd prefer to still be able to read from the row, so if the answer is yes, can you provide a solution to work this?

like image 251
orokusaki Avatar asked Aug 19 '12 19:08

orokusaki


People also ask

Can you read a locked table SQL?

The session that holds the lock can read the table (but not write it). Multiple sessions can acquire a READ lock for the table at the same time. Other sessions can read the table without explicitly acquiring a READ lock.

Does Postgres transaction lock row?

PostgreSQL doesn't remember any information about modified rows in memory, so there is no limit on the number of rows locked at one time. However, locking a row might cause a disk write, e.g., SELECT FOR UPDATE modifies selected rows to mark them locked, and so will result in disk writes.

How do locks work in Postgres?

Locks or Exclusive Locks or Write Locks prevent users from modifying a row or an entire table. Rows modified by UPDATE and DELETE are then exclusively locked automatically for the duration of the transaction. This prevents other users from changing the row until the transaction is either committed or rolled back.

Does Postgres update lock the row?

According to most accounts Postgres only locks the affected rows of an UPDATE query and does not have lock escalation.


1 Answers

You can read just fine. There are lock modes that prevent reading but this isn't one of them.

http://www.postgresql.org/docs/current/static/explicit-locking.html

like image 140
Richard Huxton Avatar answered Oct 29 '22 07:10

Richard Huxton