pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
[Fwd: Re: deadlock on the same relation]
2+ messages / 2 participants
[nested] [flat]

* [Fwd: Re: deadlock on the same relation]
@ 2005-12-02 17:49  Francesco Formenti - TVBLOB S.r.l. <francesco.formenti@tvblob.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Francesco Formenti - TVBLOB S.r.l. @ 2005-12-02 17:49 UTC (permalink / raw)
  To: pgsql-general

Tom Lane wrote:

>"Francesco Formenti - TVBLOB S.r.l." <francesco.formenti@tvblob.com> writes:
>  
>
>>I have a problem about deadlock. I have several stored procedures; only 
>>one of them uses ACCESS EXCLUSIVE LOCK on a table; however, all the 
>>stored procedures can access to that table, using SELECT, INSERT or UPDATE.
>>The stored procedures are called by different processes of an external 
>>application.
>>    
>>
>
>  
>
>>In a non-predictable way, I obtain error messages like this one:
>>    
>>
>
>  
>
>>2005-11-29 18:23:06 [12771] ERROR:  deadlock detected
>>DETAIL:  Process 12771 waits for AccessExclusiveLock on relation 26052 
>>of database 17142; blocked by process 12773.
>>        Process 12773 waits for AccessExclusiveLock on relation 26052 of 
>>database 17142; blocked by process 12771.
>>CONTEXT:  PL/pgSQL function "set_session_box_status" line 7 at SQL statement
>>    
>>
>
>Probably you have been careless about avoiding "lock upgrade"
>situations.  If you are going to take an exclusive lock on a relation,
>it is dangerous to already hold a non-exclusive lock on the same
>relation, because that prevents anyone else from getting an exclusive
>lock; thus if another process is doing the exact same thing you are in
>a deadlock situation.
>
>Since SELECT/INSERT/UPDATE take non-exclusive locks, you can't do one of
>those and later ask for exclusive lock within the same transaction.
>The general rule is "get the strongest lock you will need first".
>
>			regards, tom lane
>
>
>  
>


Unfortunately, the first operation I do after the "BEGIN" declaration is 
the LOCK TABLE in access exclusive mode, and is the only explicit lock I 
perform in all the stored procedures. I'm wondering: if other functions 
access to the same table, via SELECT or UPDATE, not specifying an 
explicit lock, could this generate a deadlock? The fact that I don't 
understand is the common resource on which the two processes are locked 
into. 
I can imagine a flow like this:

Transaction 1: ---lock table A (for an UPDATE, for instance)
Transaction 2: ---lock access exclusive on table B (at the beginning of 
the stored procedure)
Transaction 1: ---try to lock table B (for an UPDATE, for instance)
Transaction 2: ---try to lock table A (for an UPDATE, for instance)

But I think it doesn't generate a deadlock error message on the same 
resource (in this case, table B), like the one I've got.

Thanks
Regards,
Francesco





-- 

Francesco Formenti - TVBLOB S.r.l.
Software Engineer

 Via G. Paisiello, 9 20131 Milano, Italia
 -----------------------------------------
 Phone +39 02 36562440
 Fax +39 02 20408347
 Web Site http://www.tvblob.com
 E-mail francesco.formenti@tvblob.com





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

* Re: [Fwd: Re: deadlock on the same relation]
@ 2005-12-02 20:46  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Francesco Formenti - TVBLOB S.r.l. <francesco.formenti@tvblob.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Tom Lane @ 2005-12-02 20:46 UTC (permalink / raw)
  To: Francesco Formenti - TVBLOB S.r.l. <francesco.formenti@tvblob.com>; +Cc: pgsql-general

"Francesco Formenti - TVBLOB S.r.l." <francesco.formenti@tvblob.com> writes:
> Tom Lane wrote:
>> Probably you have been careless about avoiding "lock upgrade"
>> situations.

> Unfortunately, the first operation I do after the "BEGIN" declaration is 
> the LOCK TABLE in access exclusive mode, and is the only explicit lock I 
> perform in all the stored procedures.

If you mean that you placed a LOCK TABLE inside the stored procedure,
that's far from being the same thing as the start of the transaction.
For example, if your application does

	BEGIN;
	SELECT * FROM mytab;
	SELECT myprocedure();
	COMMIT;

then by the time control arrives inside myprocedure your transaction
already holds a nonexclusive lock on "mytab".  If you do LOCK TABLE mytab
inside the function then you're risking deadlock.

			regards, tom lane



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


end of thread, other threads:[~2005-12-02 20:46 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-12-02 17:49 [Fwd: Re: deadlock on the same relation] Francesco Formenti - TVBLOB S.r.l. <francesco.formenti@tvblob.com>
2005-12-02 20:46 ` Tom Lane <tgl@sss.pgh.pa.us>

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