in reply to Implementing rowlocking

PostgreSQL database server allows locking data per row. Before you do any coding, maybe you should consider installing it (if you have the permissons to do so).
About the algorithm: if I understand correctly, it seems to me that it will not prevent race conditions (because, as you said "the race conditon happens if two http requests manages to check the table before one of them inserts/acquires the lock" but you don't check for it in your algorithm).