I have I good understanding of what happens in regard to the update of a row when SELECT FOR UPDATE is used and another SELECT/UPDATE takes place. But what happens when two requests take place with SELECT FOR UPDATE.
For example:
I do not care what will happen once the request returns and the time to update the table comes. The second that goes for the update will either throw or update the last possible data on the row.
But will the actual HTTP request be done by both of them? In other words can in this scenario SELECT FOR UPDATE be used (abused) as a thread synchronization mechanism?
You are mixing up layers. PostgreSQL doesn't do HTTP. SELECT ... FOR UPDATE
has nothing to do with HTTP.
Here's how it works:
BEGIN
BEGIN
SELECT ... FOR UPDATE
and gets one or more rowsSELECT ... FOR UPDATE
and matches one of the same rows, so it blocks, not returning anything, until...COMMIT
or ROLLBACK
SELECT ... FOR UPDATE
In other words, lock duration is controlled by transaction boundaries. Where the transaction boundaries lie depends on your application and framework, stuff way above the database layer that you haven't identified in any way.
(Also, this has nothing to do with threads).