Re: Strange deadlock with object/target of lock : transaction

From: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
To: Achilleas Mantzios <a(dot)mantzios(at)cloud(dot)gatewaynet(dot)com>, pgsql-general(at)lists(dot)postgresql(dot)org
Cc: Achilleas Mantzios <itdev(at)gatewaynet(dot)com>
Subject: Re: Strange deadlock with object/target of lock : transaction
Date: 2026-10-21 22:23:14
Message-ID: cf0634aa759c32b3e74fc31920a13d8161482ae3.camel@cybertec.at
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, 2026-10-21 at 17:36 +0200, Achilleas Mantzios wrote:
> So, what happened, the INSERT by it self acquires a ROW EXCLUSIVE (RowExclusiveLock)
> on the table even before the BEFORE trigger is called.  A ROW EXCLUSIVE lock does not
> block another session acquiring also a  LOW EXCLUSIVE lock on same table.
>
> But what the BEFORE trigger did was 
>
> LOCK TABLE payments_finalization IN SHARE UPDATE EXCLUSIVE MODE;
>
> which is stronger than ROW EXCLUSIVE and conflicts with itself on the same table.
>
> So what was happening is that :
>
> [...]
> >
> pgsql acquires a ROW EXCLUSIVE on the table for both sessions, ROW EXCLUSIVE is not self-conflicting.
>
> Then both backends calle the BEFORE trigger.
> >
> Both get to execute the explicit  LOCK TABLE payments_finalization IN SHARE UPDATE EXCLUSIVE MODE ;
> the first session waits on the second to release its ROW EXCLUSIVE lock.
> the second session waits on the first to release its ROW EXCLUSIVE lock.
>
> A clear dead-lock situation.

Thanks!

I cannot be certain about your special case, but in my opinion explicitly
locking a table is almost always too strong a measure, and there are better
ways of solving the problem.

Yours,
Laurenz Albe

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Pierre Forstmann 2026-10-22 12:11:02 cannot install PG on Oracle Linux 9
Previous Message Achilleas Mantzios 2026-10-21 15:36:03 Re: Strange deadlock with object/target of lock : transaction