Skip to content

Commit a13c75d

Browse files
Added Lab-DeadLock
1 parent a0acfcc commit a13c75d

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

Lab-DeadLock

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
## Open a Terminal we call this session-1
3+
connect to database server
4+
connect to world database
5+
6+
\c world
7+
8+
BEGIN ; # THIS WILL KEEP CONNECTION ALIVE
9+
BEGIN # will get this output
10+
11+
UPDATE country SET continent='Europe' WHERE code='USA';
12+
UPDATE 1 # will give this output - started query on row - USA , hence row - USA is in use
13+
14+
15+
16+
## Open another Terminal we call this session-2
17+
18+
connect to database server
19+
connect to world database
20+
21+
\c world
22+
23+
BEGIN ;
24+
BEGIN # will get this output
25+
26+
UPDATE country SET continent='Asia' WHERE code='GBR';
27+
UPDATE 1 # will give this output - started query on row - GBR , hence row - GBR is in use
28+
29+
UPDATE country SET indepyear=1111 WHERE code='USA'; # now this will go in waiting as row - USA is still in use by session 1
30+
31+
32+
## Go to session 1
33+
34+
UPDATE country SET indepyear=1112 WHERE code='GBR'; # row - GBR is in use by session 2 hence it will generate deadlock error
35+
36+
ERROR: deadlock detected
37+
DETAIL: Process 22208 waits for ShareLock on transaction 806; blocked by process 22256.
38+
Process 22256 waits for ShareLock on transaction 805; blocked by process 22208.
39+
HINT: See server log for query details.
40+
CONTEXT: while updating tuple (4,27) in relation "country"
41+
42+
postgresql automatically break the lock
43+
44+
45+
## Go to session 2
46+
47+
and now query is completed
48+
49+
50+
now end both session by executing below command in both session and notice the output
51+
52+
end;
53+
54+
session 2 will say - COMMIT
55+
but
56+
session 1 will say - ROLLBACK - that means - " UPDATE country SET indepyear=1112 WHERE code='GBR'; " was not successfully executed
57+
58+
For more details we can see the log in our PostgreSQL server

0 commit comments

Comments
 (0)