Please help me understand the use-case behind SELECT ... FOR UPDATE.
Question 1: Is the following a good example of when SELECT ... FOR UPDATE should be used?
Given:
- rooms[id]
- tags[id, name]
- room_tags[room_id, tag_id]
- room_id and tag_id are foreign keys
The application wants to list all rooms and their tags, but needs to differentiate between rooms with no tags versus rooms that have been removed. If SELECT ... FOR UPDATE is not used, what could happen is:
- Initially:
- rooms contains
[id = 1] - tags contains
[id = 1, name = 'cats'] - room_tags contains
[room_id = 1, tag_id = 1]
- rooms contains
- Thread 1:
SELECT id FROM rooms;returns [id = 1]
- Thread 2:
DELETE FROM room_tags WHERE room_id = 1; - Thread 2:
DELETE FROM rooms WHERE id = 1; - Thread 2: [commits the transaction]
- Thread 1:
SELECT tags.name FROM room_tags, tags WHERE room_tags.room_id = 1 AND tags.id = room_tags.tag_id;- returns an empty list
Now Thread 1 thinks that room 1 has no tags, but in reality the room has been removed. To solve this problem, Thread 1 should SELECT id FROM rooms FOR UPDATE, thereby preventing Thread 2 from deleting from rooms until Thread 1 is done. Is that correct?
Question 2: When should one use SERIALIZABLE transaction isolation versus READ_COMMITTED with SELECT ... FOR UPDATE?
Answers are expected to be portable (not database-specific). If that's not possible, please explain why.
REPEATABLE_READandREAD_COMMITTEDeven portable options? The only results I get for those are for MSSQL server - Billy ONealREAD COMMITTEDmode does not define whether or not you will actually see records committed by another transaction: it only makes sure you will never see uncommitted records. - Quassnoiselect ... for updateonroomswill still allowroom_tagsto be deleted because they are separate tables. Did you mean to ask whether thefor updateclause will prevent deletions fromrooms? - Chris Saxon