Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZK9KU-0004bL-9m for pgsql-bugs@arkaria.postgresql.org; Tue, 28 Jul 2015 18:10:18 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZK9KT-0007xS-P5 for pgsql-bugs@arkaria.postgresql.org; Tue, 28 Jul 2015 18:10:17 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZK9Jr-0007Bj-RT for pgsql-bugs@postgresql.org; Tue, 28 Jul 2015 18:09:39 +0000 Received: from douglastechnology.co.uk ([109.169.21.47] helo=mail.douglastechnology.co.uk) by magus.postgresql.org with esmtp (Exim 4.84) (envelope-from ) id 1ZK9Ji-0001C1-H1 for pgsql-bugs@postgresql.org; Tue, 28 Jul 2015 18:09:36 +0000 Received: from redditch (unknown [95.154.250.181]) by mail.douglastechnology.co.uk (Postfix) with ESMTPSA id A06014002AB61; Tue, 28 Jul 2015 19:09:26 +0100 (BST) From: "Jack Douglas" To: "'Tom Lane'" Cc: References: <20150728162823.25043.27625@wrigleys.postgresql.org> <17665.1438105627@sss.pgh.pa.us> In-Reply-To: <17665.1438105627@sss.pgh.pa.us> Subject: Re: BUG #13523: Unexplained deadlocks (possible race condition) Date: Tue, 28 Jul 2015 19:09:25 +0100 Message-ID: <01b001d0c960$894af9e0$9be0eda0$@douglastechnology.co.uk> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Outlook 15.0 Thread-Index: AQGcf0xE18AKTxoNJZtQsHxZmHenRQIzy08Onkghl8A= Content-Language: en-gb X-Pg-Spam-Score: -3.3 (---) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org > I believe the issue with this is that a SQL function will do parsing (and maybe planning too; don't feel like checking the code right now) for the entire function body at once. This means that due to the INSERT command you acquire RowExclusiveLock on the "test" table during function body parsing, before the LOCK command actually executes. So the LOCK represents a lock escalation attempt, and deadlocks are to be expected. That makes perfect sense, many thanks for the explanation. > This coding technique would be safe in plpgsql, but not in a SQL-language function. That's useful to know - I already worked around the issue with a retry-loop (as I'm basically doing an upsert the 'bad' way with `lock table` - roll on 9.5!) Kind regards Jack -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs