Re: Deadlocks and transactions - Mailing list pgsql-novice

From Lætitia Avrot
Subject Re: Deadlocks and transactions
Date
Msg-id CAB_COdjG9P+BR9U6Ekrwe+t_AqkgDLXY8Mz3bzyaQpvB3EgWgw@mail.gmail.com
Whole thread Raw
In response to Re: Deadlocks and transactions  ("David G. Johnston" <david.g.johnston@gmail.com>)
Responses Re: Deadlocks and transactions  (JORGE MALDONADO <jorgemal1960@gmail.com>)
List pgsql-novice
Hi Jorge,

Here are the two advices I give developpers to reduce the risk a deadlock occures :
- Always change(update, delete...) data in the same order
- Keep your transactions short

Cheers,

Lætitia

2018-03-19 23:18 GMT+01:00 David G. Johnston <david.g.johnston@gmail.com>:
On Mon, Mar 19, 2018 at 2:46 PM, JORGE MALDONADO <jorgemal1960@gmail.com> wrote:
I have a process that inserts a record in one table and, after that, a record in another table is updated. Because there are 2 DB operations, I decided to perform both of them in a transaction. 

Can a deadlock take place even if transactions are used?

Its impossible to deadlock without transactions​.

Simplistically, a deadlock happens when there are two processes - one holds lock A and wants lock B while the other wants lock A while holding lock B.

Your choice to use a transaction here is good but you will have at least some risk of deadlock with others parts of the system.  Other processes running this same exact code, however, should not pose a risk since the locking order would be consistent.

David J.


pgsql-novice by date:

Previous
From: "David G. Johnston"
Date:
Subject: Re: Deadlocks and transactions
Next
From: Stephen Froehlich
Date:
Subject: Replicate a table through a client?