Say I have:
- T1:
SELECT id FROM x WHERE timestamp < y
(returnsid = [1, 2, 3]
) - T2:
DELETE FROM x WHERE id = 1
- T1:
SELECT timestamp, value FROM x WHERE id = 1
with READ-COMMITTED
isolation.
Does step 3 run the risk of returning an empty result, or does step 1 acquire some sort of lock/snapshot that prevents step 2 from altering the result? (I assume REPEATABLE-READ
will do what I want, but this question is about READ-COMMITTED
).
I am using postgresql, but I am interested in a DB-independent answer. For example, if some databases block the delete while others do not, I'd like to know that. Thank you.
SNAPSHOT ISOLATION
. In the absence of specific hints simpleSELECT
at step 1 should not prevent T2 from deleting the row. (I assume step 2 runs after step 1 has finished, the fact that transaction T1 is not over yet doesn't matter) – Vladimir BaranovREPEATABLE-READ
would prevent T2 from deleting the row (or at least hide it from my snapshot), correct? – GiliSERIALIZABLE
should definitely be enough. Different DBMS may implement isolation levels slightly differently, so you'd better check the docs of your DBMS – Vladimir BaranovREPEATABLE READ
looks life enough. "Specifies that statements cannot read data that has been modified but not yet committed by other transactions and that no other transactions can modify data that has been read by the current transaction until the current transaction completes. Shared locks are placed on all data read by each statement in the transaction and are held until the transaction completes. This prevents other transactions from modifying any rows that have been read by the current transaction." For Postgres, see its docs – Vladimir Baranov