Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2016 November PostgreSQL: How to generate a DeadLock in a Database (ERROR: deadlock detected)

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

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

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:

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

Open Session – 1 and execute below code:

1
2
BEGIN;
INSERT INTO ABC VALUES (1);

Open Session – 2 and execute below code:

1
2
3
BEGIN;
INSERT INTO XYZ VALUES (1);
INSERT INTO ABC VALUES (1);

Again open Session – 1 and execute below Insert Statement:

1
INSERT INTO XYZ VALUES (1);

You will get a below deadlock message:

1
2
3
4
5
ERROR: deadlock detected
DETAIL: Process 5944 waits for ShareLock on transaction 892; blocked by process 4884.
Process 4884 waits for ShareLock on transaction 891; blocked by process 5944.
HINT: See server log for query details.
CONTEXT: while inserting index tuple (0,2) in relation "xyz_pkey"

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

Nov 19, 2016Anvesh Patel
SQL Server:Warning in Query Execution Plan, "Operator used tempdb to spill data during execution with spill level 2"Database Theory: What is Parallel Query Processing (Parallel Database System)?
Anvesh Patel
Anvesh Patel

Database Engineer

November 19, 2016 PostgreSQLAnvesh Patel, BEGIN, block, COMMIT, database, database research and development, dbrnd, DeadLock, generate deadlock, lock, plpgsql, Postgres Query, postgresql, PostgreSQL Administrator, PostgreSQL Error, PostgreSQL Programming, PostgreSQL Tips and Tricks, transaction
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....