1

I try to understand, how that SQL command works:

BEGIN;
UPDATE post SET hits = hits + 1;
-- run from another session:  DELETE FROM post WHERE hits = 10;
COMMIT;

Let's say, we hahe a rows with hits = 9 and 10. Then we run that query, and then what? What (and why) will our rows look like?

4
  • 3
    Why don't you simply test? I mean - it takes 10 seconds to start 2 terminals, and test. And you'll get valuable information in a way that will teach you more. Commented Jun 19, 2013 at 11:54
  • But how to test it in that very small ammount of time, when transaction is being active (between BEGIN and COMMIT)? Commented Jun 19, 2013 at 11:57
  • Start transaction, issue update. then, in other psql, run the delete. Commented Jun 19, 2013 at 11:57
  • 1
    Just don't send the commit in the first session. Commented Jun 19, 2013 at 11:58

1 Answer 1

1

This depends on transaction isolation level in each session but by default
all changes made by UPDATE will be visible to other session only after executing COMMIT.
So your DELETE session will behave like there was no UPDATE at all.

Sign up to request clarification or add additional context in comments.

6 Comments

There is no isolation level in Postgres that will ever make non-committed data visible to other transactions.
So if I understand well - BEGIN - COMMIT puts a lock on affected rows, and until COMMIT that rows some kind of invisible for other queries?
@VitaliyG: you should read the manual carefully: "When you select the level Read Uncommitted you really get Read Committed" (emphasis mine) - there is no way to run un-committed day in Postgres
Yes it is not implemented for now
It will never be implemented. read uncommitted is simply a hack by other DBMS to allow for non-blocking concurrent reads and writes
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.