sqlpostgresqllockingrowlockingmvcc

PostgreSql Row-Level Locks


I am trying to understand Postgresql Row-Level Locks could someone help me understand when to use each type of lock.

     FOR UPDATE; FOR NO KEY UPDATE; FOR SHARE; FOR KEY SHARE;

I tried to go over the documentation but still, I couldn't understand the difference and didn't know when to use what and how does it make a difference.

I am expecting someone could help me understand the use case of each lock and give me clarity on when to choose each.


Solution

  • It is difficult to come up with a use case that requires you to use FOR SHARE and FOR KEY SHARE explicitly; they are taken be the database internally to make sure that foreign key constraints are not violated.