Will "select for share" take no effective without transaction?

134 views Asked by At

To understand "select for share", I just read https://shiroyasha.io/selecting-for-share-and-update-in-postgresql.html.

I just want to confirm that, if I do "select for share" without a transaction, like:

select * from table_a where id = 1 for share

As the transaction has only this single "select" statement, though "for share" acquires a lock, but the lock will be released immediately, thus it roughly equals to "no lock", right?

1

There are 1 answers

3
Laurenz Albe On BEST ANSWER

It is true that the lock won't be held any longer than the statement executes, so it is pretty useless. Still, SELECT ... FOR SHARE will modify the locked row just like any other row lock, thus causing a write and blocking all concurrent modifications, and the statement will acquire a transaction ID.

So you are paying the price for a row lock without any benefit.