PostgreSQL: How to generate a DeadLock in a Database (ERROR: deadlock detected)

The DeadLock in a database system is a very common thing and all Database Administrator responsible to detect and manage a DeadLock situation.

In this post, I am sharing one small demonstration t to generate a DeadLock situation in a PostgreSQL.

Database Theory: What is Timeout, Lock, Block, and Deadlock in a Database System?

Most of the time, We are facing DeadLock problem in our Production Database Server.
But What happen, when Database Administrator wants to generate DeadLock situation and wants to create a script for detecting a DeadLock.

First create two sample tables:

Open Session – 1 and execute below code:

Open Session – 2 and execute below code:

Again open Session – 1 and execute below Insert Statement:

You will get a below deadlock message:

Please visit other related articles:

PostgreSQL 9.5: Using FOR UPDATE SKIP LOCKED Option SELECT only Committed Records

PostgreSQL: Script to find information about the Locks held by Open Transactions (pg_locks)

Script to find sessions that are blocking other sessions in PostgreSQL

Please share your ideas and opinions about this topic with me, your contribution will add true value to this topic.
If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.

If you like this post, then please share it with others.
Please follow, I will share my experience towards the success of Database Research and Development Activity.

I put up a post every day, please keep reading and learning.
Discover Yourself, Happy Blogging !
Anvesh M. Patel.

More from

Leave a Reply

Be the First to Comment!

Notify of