Lost Update란?
ww-confict라고도 불리는 Lost Update는 transaction이 동시에 같은 rows를 업데이트할 때 발생
PostgreSQL에서 Lost Update가 발생할 수 있는 isolation level
- REPEATABLE READ
- SERIALIZABLE
READ COMMITED에서는 발생하지 않음.
Lost Update를 막는 방법
- concrruent transaction이 동일 row를 업데이트하고자 할 때
- 기다리던 transaction의 isolation level이 repeatable read나 serializable isolation level인 transaction을 중단시킴.
isolation level에 따라 달라지는 update 절차
| step 1.transaction에서 update할 row를 가져옴. step 2. 동일 row를 update하려는 모든 concurrent transaction은 아래 순서로 작업을 계속 수행. if row를 update한 transaction이 commit한 경우 if 동일 row를 update하려고 기다리던 다른 concurrent transaction이 update 수행 if update한 transaction의 isolation level = read commited target row를 update한 후 step1으로 이동 elif update한 transaction의 isolation level = repeatable read or serializable Lost update를 막기 위해 update한 transaction을 중지 else if row를 update한 transaction이 commit하지 않은 경우 transaction이 commit될 때까지 대기 if transaction이 commit되고 isolation level = read commited step2로 이동 if transaction이 commit되고 isolation level = repeatable read or serializable Lost update를 막기 위해 transaction 중지 |
아래 그림으로 더 쉽게 설명 가능함.
그림 (1)과 같은 상황
tx_A와 Tx_B가 동시에 트랜잭션을 시작한 상태
tx_A가 update를 먼저 수행하고 tx_B가 동일 row에 대한 update를 기다리고 있음.
이 때 tx_A가 commit을 하면
tx_B가 read committed일 때는 update 수행
tx_B가 repeatable read or serializable일 때는 aborted
그림 (2)와 같은 상황
tx_A와 Tx_B가 동시에 트랜잭션을 시작한 상태
tx_A가 update를 먼저 수행하고 tx_B가 동일 row에 대한 update를 기다리기 전에 commit함.
tx_B가 read committed일 때는 update 수행
tx_B가 repeatable read or serializable일 때는 aborted
그림 (3)과 같은 상황
isolation level과 관계 없이 transaction aborted 발생하지 않음.

repeatable read or serializable transaction을 중단해야하는 이유
앞 post를 봤다면 이해하기 쉬움.
09. transaction snapshot
transaction snapshot이란?특정 시점에 모든 transaction의 상태가 active 상태인 정보를 저장하는 것을 의미함.active상태란 transaction이 in_progress이거나 아직 시작되지 않은 것을 의미. 아래 쿼리의 결과
lsmdd.tistory.com
transaction snapshot과 연관이 있으며 아래 동작으로 인해 Lost Update 현상이 있을 수 있어 repeatable read or serializable isolation transaction에서는 중단이 필요함.
- read commited일 때 transaction snapshot이 SQL command 수행할 때 마다 변경됨.
- repeatable read or serializable일 때는 transaction snapshot이 transaction을 시작한 시점으로 유지됨.
example은 아래 링크 참고
'PostgreSQL' 카테고리의 다른 글
| 14. PostgreSQL - Visibility Map (1) | 2024.12.22 |
|---|---|
| 13. PostgreSQL - VACUUM 내부 동작 과정 (2) | 2024.12.21 |
| 10. visibility check rules (4) | 2024.12.19 |
| 09. transaction snapshot (1) | 2024.12.18 |
| 08. PostgreSQL - commit log (2) | 2024.12.18 |