SELECT FOR UPDATE is pessimistic locking and does nothing to help in the situation you described. You can use it in a single request, and still data will be lost when operated on in multiple requests. Pessimistic lock only helps within bounds of a single transaction.
Optimistic locking is much simpler, the form you render for the user has an additional, hidden, version field, then for the update query, you do something like:
UPDATE foo SET ... WHERE id = $1 AND version = $2
If the version changed, no row will be matched.


















