Wednesday, October 11, 2023

.:: Simulate Deadlock in PostgreSQL & Solution :::.

 1. create table

CREATE TABLE ABC (ID INT PRIMARY KEY);
CREATE TABLE XYZ (ID INT PRIMARY KEY);


2. Open Session – 1 and execute below code:

BEGIN;
INSERT INTO ABC VALUES (1); select '1';

 


3. Open Session – 2 and execute below code:

BEGIN;
INSERT INTO XYZ VALUES (1);
INSERT INTO ABC VALUES (1);
select '2';

4. Again open Session – 1 and execute below Insert Statement:

INSERT INTO XYZ VALUES (1);
select '1 again';

 

5. Open PGadmin & run

select * from pg_stat_activity
 



6. for kill, run this query

select pg_terminate_backend(pid)
from pg_stat_activity
where pid = '1727';

 

No comments:

Post a Comment

Popular Posts