首页 > 数据库 >PostgreSQL数据库事务系统——phenomena

PostgreSQL数据库事务系统——phenomena

时间:2023-01-14 11:08:19浏览次数:56  
标签:事务 transaction PostgreSQL T2 数据库 phenomena T1 SQL row


读写并发操作引发的数据异常现象

PostgreSQL数据库事务系统——phenomena_postgresql


PostgreSQL数据库事务系统——phenomena_数据库_02


PostgreSQL数据库事务系统——phenomena_postgresql_03


The isolation level specifies the kind of phenomena that can occur during the execution of concurrent SQL-transcations. The following phenomena are possible:

  1. P1 (“Dirty read”): SQL-transaction T1 modifiesa row. SQL-transaction T2 then reads that row before T1 performs a COMMIT. If T1 then performs a ROLLBACK, T2 will have read a row that was never committed and that may thus be considered to have never existed.
  2. P2 (“Non-repeatable read”): SQL-transaction T1 reads a row. SQL-transaction T2 then modifies or deletes that row and performs a COMMIT. If T1 then attempts to reread the row, it may receive the modified value or discover that the row has been deleted.
  3. P3 (“Phantom”): SQL-transaction T1 reads the set of rows N that satisfy some <search condition>. SQL-transaction T2 then executes SQL-statements that generate one or more rows that satisfy the <search condition> used by SQL-transaction T1. If SQL-transactionT1 then repeats the initial read with the same <search condition>, it obtains a different collection of rows.
    读写操作:如果读写操作都存在,因写在前读在后(如脏读现象)、读在前写在后(如不可重复读现象),或者读在前写在后然后又读(如幻读现象),就可能因数据被写而导致另外一个读操作的会话读到错误的数据。这个操作可以根据动作发生的先后顺序被细分为读-写操作、写-读操作。

写写并发操作引发的数据异常现象

写写操作:如果同时存在多个写操作,写写操作直接改变了数据在同一时刻的语义,这就更不被允许,所以写写操作通常不允许被并发执行。但是,如果不做并发控制,写写并发操作也会带来数据异常现象。
脏写现象:按照时间顺序,事务T1在t0时刻对row进行了修改(更新),事务T2在t1时刻对row进行了修改(更新),如果没有并发控制,T2对row的修改会生成新值,但是T1在t3时刻回滚使得T2对row的修改失效,而T1的语义是:T1自身对row的修改失效,这就把T2修改的值回滚掉。对于事务T1而言,回滚掉了不是自己修改的数据,即事务T1上发生了脏写现象。
丢失更新现象:按照时间顺序,事务T2在t1时刻对row进行了修改(更新),事务T1在t2时刻对row进行了修改(更新),如果没有并发控制,T1对row的修改会生成新值,但是T1在t3时刻提交使得T2对row的修改失效。对于事务T1而言,覆盖掉了不是自己修改的数据,即事务T1上引发了丢失更新现象(t3时刻如果是事务T2提交而不是事务T1提交,也是丢失更新,只是事务T2上引发了丢失更新现象)。

精准定义

PostgreSQL数据库事务系统——phenomena_SQL_04

PostgreSQL数据库事务系统——phenomena_sql_05


标签:事务,transaction,PostgreSQL,T2,数据库,phenomena,T1,SQL,row
From: https://blog.51cto.com/feishujun/6007518

相关文章