X-Original-To: pgsql-general-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 2C7E09DD775 for ; Fri, 2 Dec 2005 13:50:50 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 51274-02-2 for ; Fri, 2 Dec 2005 13:50:42 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from host11-117.pool80207.interbusiness.it (host11-117.pool80207.interbusiness.it [80.207.117.11]) by postgresql.org (Postfix) with ESMTP id CF2DC9DD74A for ; Fri, 2 Dec 2005 13:50:41 -0400 (AST) Received: from ([127.0.0.1]) with MailEnable ESMTP; Fri, 02 Dec 2005 18:50:40 +0100 Received: from 62.101.126.224 ([62.101.126.224] helo=[192.168.72.111]) by TVBLOB-smtp ; 2 Dec 05 17:50:40 -0000 Message-ID: <4390893D.4070109@tvblob.com> Date: Fri, 02 Dec 2005 18:49:49 +0100 From: "Francesco Formenti - TVBLOB S.r.l." Organization: TvBlob User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.8) Gecko/20050831 Debian/1.7.8-1sarge2 X-Accept-Language: ja MIME-Version: 1.0 To: pgsql-general@postgresql.org Subject: [Fwd: Re: deadlock on the same relation] Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=1.254 required=5 tests=[RCVD_NUMERIC_HELO=1.253, UNPARSEABLE_RELAY=0.001] X-Spam-Score: 1.254 X-Spam-Level: * X-Archive-Number: 200512/117 X-Sequence-Number: 87680 Tom Lane wrote: >"Francesco Formenti - TVBLOB S.r.l." 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