28
loading...
This website collects cookies to deliver better user experience
create table t ( x int, y int );
insert into t values ( -1, 11 );
insert into t values ( 1, 12 );
begin transaction;
update t set x=-x, y=y+100;
update t set y=y+1000 where x<0;
commit;
x | y
----+----
-1 | 11
1 | 12
x | y
----+-----
1 | 111
-1 | 112
set x=-x
from the first session is atomic. There will always be one row negative and one positive. I expect my Session 2 to update one row and only one row.Session 1: Jul 13 09:49:58 create table t ( x int, y int );
Session 1: Jul 13 09:49:58 CREATE TABLE
Session 1: Jul 13 09:49:58 insert into t values ( -1, 11 );
Session 1: Jul 13 09:49:58 INSERT 0 1
Session 1: Jul 13 09:49:58 insert into t values ( 1 , 12 );
Session 1: Jul 13 09:49:58 INSERT 0 1
Session 1: Jul 13 09:49:58 select * from t;
Session 1: Jul 13 09:49:58 x | y
Session 1: Jul 13 09:49:58 ----+----
Session 1: Jul 13 09:49:58 -1 | 11
Session 1: Jul 13 09:49:58 1 | 12
Session 1: Jul 13 09:49:58 (2 rows)
Session 1: Jul 13 09:49:58
Session 1: Jul 13 09:49:58 begin transaction;
Session 1: Jul 13 09:49:58 BEGIN
Session 1: Jul 13 09:49:58 update t set x=-x, y=y+100;
Session 1: Jul 13 09:49:58 UPDATE 2
Session 2: Jul 13 09:50:03 You are now connected to database "demo" as user "postgres".
Session 2: Jul 13 09:50:03 begin transaction;
Session 2: Jul 13 09:50:03 BEGIN
Session 2: Jul 13 09:50:03 set transaction isolation level read committed;
Session 2: Jul 13 09:50:03 SET
Session 2: Jul 13 09:50:03 select * from t;
Session 2: Jul 13 09:50:03 x | y
Session 2: Jul 13 09:50:03 ----+----
Session 2: Jul 13 09:50:03 -1 | 11
Session 2: Jul 13 09:50:03 1 | 12
Session 2: Jul 13 09:50:03 (2 rows)
Session 2: Jul 13 09:50:03
Session 2: Jul 13 09:50:03 update t set y=y+1000 where x<0;
Session 1: Jul 13 09:50:13 commit;
Session 1: Jul 13 09:50:13 COMMIT
Session 2: Jul 13 09:50:13 UPDATE 0
Session 2: Jul 13 09:50:13 select * from t;
Session 2: Jul 13 09:50:13 x | y
Session 2: Jul 13 09:50:13 ----+-----
Session 2: Jul 13 09:50:13 1 | 111
Session 2: Jul 13 09:50:13 -1 | 112
Session 2: Jul 13 09:50:13 (2 rows)
Session 2: Jul 13 09:50:13
Session 2: Jul 13 09:50:13 commit;
Session 2: Jul 13 09:50:13 COMMIT