Update if no lock in postgres?
I think what you want is NOWAIT


  To prevent the operation from waiting for other transactions to commit, use either the NOWAIT or SKIP LOCKED option. With NOWAIT, the statement reports an error, rather than waiting, if a selected row cannot be locked immediately. With SKIP LOCKED, any selected rows that cannot be immediately locked are skipped. Skipping locked rows provides an inconsistent view of the data, so this is not suitable for general purpose work, but can be used to avoid lock contention with multiple consumers accessing a queue-like table. Note that NOWAIT and SKIP LOCKED apply only to the row-level lock(s) — the required ROW SHARE table-level lock is still taken in the ordinary way (see Chapter 13). You can use LOCK with the NOWAIT option first, if you need to acquire the table-level lock without waiting.


That said, I'm not sure why you would want to do this. I've never had to do this. It sounds like a bad idea. It's quite normal to wait for a lock to update the rows. Retrying transactions is usually a longer-process. If you're afraid of issuing an update you don't want to a modified row, you don't have to worry about it. Dirty-writes are prevented.

UPDATE foo
SET baz = 1
WHERE bar = 2;


Will never update where a bar isn't 2. If it's waiting for a lock, after it gets the lock it'll check bar=2 and set baz=1 by rewriting the row.

I just can't see this as being more than trivia, or an extremely esoteric use-case.

I would keep it simple if you go the NOWAIT method becuase I don't think it matters anyway.

BEGIN;
  SELECT *
  FROM foo
  WHERE bar=2
  FOR UPDATE NOWAIT;

  UPDATE foo SET baz=1 WHERE bar=2;
END;


That whole transaction will rollback if it can't get the lock right away.