If you need to SELECT from a table and protect those rows from being updated until your transaction has completed, you would specify FOR UPDATE, but if some rows are locked, you can specify SKIP LOCKED to tell it to simply ignore those rows and just perform the operation on whichever rows it can access.

For example:
In session 1:
 # BEGIN; BEGIN
 # SELECT * FROM colours;  id | name ----+------- 1 | red 2 | green 3 | blue (3 rows)
 # UPDATE colours SET name = 'scarlet' WHERE name = 'red'; CREATE TABLE
In session 2:
 # SELECT * FROM colours FOR UPDATE NOWAIT; ERROR:  could not obtain lock on row in relation "colours"
 # SELECT * FROM colours FOR UPDATE SKIP LOCKED;  id | name ----+------- 2 | green 3 | blue (2 rows)

0 comments:

Post a Comment

 
Top