pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
algo for canceling a deadlocked transaction
6+ messages / 4 participants
[nested] [flat]

* algo for canceling a deadlocked transaction
@ 2018-04-09 13:46 Thomas Poty <thomas.poty@gmail.com>
  2018-04-09 13:51 ` Re: algo for canceling a deadlocked transaction Stephen Frost <sfrost@snowman.net>
  0 siblings, 1 reply; 6+ messages in thread

From: Thomas Poty @ 2018-04-09 13:46 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Good afternoon,

My question is : In case of a deadlock between 2 transaction,  how to know
which transaction will be canceled? Is it predictable?

I have tried to look into sources but i have found nothing. ( probably, i
am the problem)

Regards,


Thomas

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: algo for canceling a deadlocked transaction
  2018-04-09 13:46 algo for canceling a deadlocked transaction Thomas Poty <thomas.poty@gmail.com>
@ 2018-04-09 13:51 ` Stephen Frost <sfrost@snowman.net>
  2018-04-09 14:33   ` Re: algo for canceling a deadlocked transaction Thomas Poty <thomas.poty@gmail.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Stephen Frost @ 2018-04-09 13:51 UTC (permalink / raw)
  To: Thomas Poty <thomas.poty@gmail.com>; +Cc: pgsql-general@lists.postgresql.org

Greetings,

* Thomas Poty (thomas.poty@gmail.com) wrote:
> My question is : In case of a deadlock between 2 transaction,  how to know
> which transaction will be canceled? Is it predictable?

The short answer is "it's whichever one detected the deadlock."  The
deadlock timeout fires after a lock has been held that long and if a
deadlock is detected then the process detecting it will be canceled.

I'd strongly recommend reviewing your application and addressing
deadlocks by changing how the application acquires locks to be
consistent and to avoid lock escalation instead of worrying about how to
predict a deadlock- a properly designed and written application
shouldn't be causing deadlocks to happen in the first place.

Thanks!

Stephen

Attachments:

  signature.asc (application/pgp-signature, 818B) download

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: algo for canceling a deadlocked transaction
  2018-04-09 13:46 algo for canceling a deadlocked transaction Thomas Poty <thomas.poty@gmail.com>
  2018-04-09 13:51 ` Re: algo for canceling a deadlocked transaction Stephen Frost <sfrost@snowman.net>
@ 2018-04-09 14:33   ` Thomas Poty <thomas.poty@gmail.com>
  2018-04-09 14:42     ` Re: algo for canceling a deadlocked transaction Christophe Pettus <xof@thebuild.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Thomas Poty @ 2018-04-09 14:33 UTC (permalink / raw)
  To: Stephen Frost <sfrost@snowman.net>; +Cc: pgsql-general@lists.postgresql.org

Hello Stephen,

> The short answer is "it's whichever one detected the deadlock."  The
> deadlock timeout fires after a lock has been held that long and if a
> deadlock is detected then the process detecting it will be canceled.

ok, and long  answer ? is it random?


> I'd strongly recommend reviewing your application and addressing
> deadlocks by changing how the application acquires locks to be
> consistent and to avoid lock escalation instead of worrying about how to
> predict a deadlock- a properly designed and written application
> shouldn't be causing deadlocks to happen in the first place.

I didn't want to predict the deadlock happening. I only want to know if it
is predictable to know which transaction will be canceled.

Thank you

2018-04-09 15:51 GMT+02:00 Stephen Frost <sfrost@snowman.net>:

> Greetings,
>
> * Thomas Poty (thomas.poty@gmail.com) wrote:
> > My question is : In case of a deadlock between 2 transaction,  how to
> know
> > which transaction will be canceled? Is it predictable?
>
> The short answer is "it's whichever one detected the deadlock."  The
> deadlock timeout fires after a lock has been held that long and if a
> deadlock is detected then the process detecting it will be canceled.
>
> I'd strongly recommend reviewing your application and addressing
> deadlocks by changing how the application acquires locks to be
> consistent and to avoid lock escalation instead of worrying about how to
> predict a deadlock- a properly designed and written application
> shouldn't be causing deadlocks to happen in the first place.
>
> Thanks!
>
> Stephen
>

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: algo for canceling a deadlocked transaction
  2018-04-09 13:46 algo for canceling a deadlocked transaction Thomas Poty <thomas.poty@gmail.com>
  2018-04-09 13:51 ` Re: algo for canceling a deadlocked transaction Stephen Frost <sfrost@snowman.net>
  2018-04-09 14:33   ` Re: algo for canceling a deadlocked transaction Thomas Poty <thomas.poty@gmail.com>
@ 2018-04-09 14:42     ` Christophe Pettus <xof@thebuild.com>
  2018-04-09 15:04       ` Re: algo for canceling a deadlocked transaction Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 6+ messages in thread

From: Christophe Pettus @ 2018-04-09 14:42 UTC (permalink / raw)
  To: Thomas Poty <thomas.poty@gmail.com>; +Cc: Stephen Frost <sfrost@snowman.net>; pgsql-general@lists.postgresql.org


> On Apr 9, 2018, at 07:33, Thomas Poty <thomas.poty@gmail.com> wrote:
> 
> ok, and long  answer ? is it random?

It's not literally random, but from the application point of view, it's not predictable.  For example, it's not always the one that opened first, or any other consistent measure.
--
-- Christophe Pettus
   xof@thebuild.com





^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: algo for canceling a deadlocked transaction
  2018-04-09 13:46 algo for canceling a deadlocked transaction Thomas Poty <thomas.poty@gmail.com>
  2018-04-09 13:51 ` Re: algo for canceling a deadlocked transaction Stephen Frost <sfrost@snowman.net>
  2018-04-09 14:33   ` Re: algo for canceling a deadlocked transaction Thomas Poty <thomas.poty@gmail.com>
  2018-04-09 14:42     ` Re: algo for canceling a deadlocked transaction Christophe Pettus <xof@thebuild.com>
@ 2018-04-09 15:04       ` Tom Lane <tgl@sss.pgh.pa.us>
  2018-04-09 18:47         ` Re: algo for canceling a deadlocked transaction Thomas Poty <thomas.poty@gmail.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Tom Lane @ 2018-04-09 15:04 UTC (permalink / raw)
  To: Christophe Pettus <xof@thebuild.com>; +Cc: Thomas Poty <thomas.poty@gmail.com>; Stephen Frost <sfrost@snowman.net>; pgsql-general@lists.postgresql.org

Christophe Pettus <xof@thebuild.com> writes:
>> On Apr 9, 2018, at 07:33, Thomas Poty <thomas.poty@gmail.com> wrote:
>> ok, and long  answer ? is it random?

> It's not literally random, but from the application point of view, it's not predictable.  For example, it's not always the one that opened first, or any other consistent measure.

It's whichever one runs the deadlock detector first after the circular
wait becomes established.  For instance:

* Process A takes lock L1

* Process B takes lock L2

* Process A tries to take lock L2, blocks

* Process B tries to take lock L1, blocks (now a deadlock exists)

Process A will run the deadlock detector one deadlock_timeout after
blocking.  If that happens before B has blocked, then A will see
no deadlock and will go back to waiting.  In that case, when B's
own deadlock_timeout expires and it runs the deadlock detector,
it will see the deadlock and fix it by canceling its own wait.
On the other hand, if B started to wait less than one deadlock_timeout
after A did, then A will be first to observe the deadlock and it will
cancel itself, not B.

So you can't predict it unless you have a lot of knowledge about
the timing of events.  You could probably make it more predictable
by making deadlock_timeout either very short or very long, but
neither of those are desirable things to do.

			regards, tom lane




^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: algo for canceling a deadlocked transaction
  2018-04-09 13:46 algo for canceling a deadlocked transaction Thomas Poty <thomas.poty@gmail.com>
  2018-04-09 13:51 ` Re: algo for canceling a deadlocked transaction Stephen Frost <sfrost@snowman.net>
  2018-04-09 14:33   ` Re: algo for canceling a deadlocked transaction Thomas Poty <thomas.poty@gmail.com>
  2018-04-09 14:42     ` Re: algo for canceling a deadlocked transaction Christophe Pettus <xof@thebuild.com>
  2018-04-09 15:04       ` Re: algo for canceling a deadlocked transaction Tom Lane <tgl@sss.pgh.pa.us>
@ 2018-04-09 18:47         ` Thomas Poty <thomas.poty@gmail.com>
  0 siblings, 0 replies; 6+ messages in thread

From: Thomas Poty @ 2018-04-09 18:47 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Christophe Pettus <xof@thebuild.com>; Stephen Frost <sfrost@snowman.net>; pgsql-general@lists.postgresql.org

Hello Tom,

Thank you for the clarification!

Regards,
 Thomas

Le lun. 9 avr. 2018 à 17:04, Tom Lane <tgl@sss.pgh.pa.us> a écrit :

> Christophe Pettus <xof@thebuild.com> writes:
> >> On Apr 9, 2018, at 07:33, Thomas Poty <thomas.poty@gmail.com> wrote:
> >> ok, and long  answer ? is it random?
>
> > It's not literally random, but from the application point of view, it's
> not predictable.  For example, it's not always the one that opened first,
> or any other consistent measure.
>
> It's whichever one runs the deadlock detector first after the circular
> wait becomes established.  For instance:
>
> * Process A takes lock L1
>
> * Process B takes lock L2
>
> * Process A tries to take lock L2, blocks
>
> * Process B tries to take lock L1, blocks (now a deadlock exists)
>
> Process A will run the deadlock detector one deadlock_timeout after
> blocking.  If that happens before B has blocked, then A will see
> no deadlock and will go back to waiting.  In that case, when B's
> own deadlock_timeout expires and it runs the deadlock detector,
> it will see the deadlock and fix it by canceling its own wait.
> On the other hand, if B started to wait less than one deadlock_timeout
> after A did, then A will be first to observe the deadlock and it will
> cancel itself, not B.
>
> So you can't predict it unless you have a lot of knowledge about
> the timing of events.  You could probably make it more predictable
> by making deadlock_timeout either very short or very long, but
> neither of those are desirable things to do.
>
>                         regards, tom lane
>

^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2018-04-09 18:47 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-04-09 13:46 algo for canceling a deadlocked transaction Thomas Poty <thomas.poty@gmail.com>
2018-04-09 13:51 ` Stephen Frost <sfrost@snowman.net>
2018-04-09 14:33   ` Thomas Poty <thomas.poty@gmail.com>
2018-04-09 14:42     ` Christophe Pettus <xof@thebuild.com>
2018-04-09 15:04       ` Tom Lane <tgl@sss.pgh.pa.us>
2018-04-09 18:47         ` Thomas Poty <thomas.poty@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox