Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eiaMN-0001Bu-7M for pgsql-general@arkaria.postgresql.org; Mon, 05 Feb 2018 06:34:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eiaML-0003UZ-Km for pgsql-general@arkaria.postgresql.org; Mon, 05 Feb 2018 06:34:33 +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_2) (envelope-from ) id 1eiIvm-0000x0-3U for pgsql-general@lists.postgresql.org; Sun, 04 Feb 2018 11:57:58 +0000 Received: from lb2-smtp-cloud7.xs4all.net ([194.109.24.28]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eiIvj-0002AM-8n for pgsql-general@lists.postgresql.org; Sun, 04 Feb 2018 11:57:57 +0000 Received: from mail1.pinkwin.com ([83.162.151.158]) by smtp-cloud7.xs4all.net with ESMTP id iIvfekdjR3A62iIvgeXoRl; Sun, 04 Feb 2018 12:57:53 +0100 Received: from localhost (localhost [127.0.0.1]) by mail1.pinkwin.com (Postfix) with ESMTP id 35BA261E0F for ; Sun, 4 Feb 2018 12:57:51 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at pinkwin.com Received: from mail1.pinkwin.com ([127.0.0.1]) by localhost (mail1.pinkwin.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 8yD_kCdKEDbC for ; Sun, 4 Feb 2018 12:57:39 +0100 (CET) Received: from mail.pinkwin.com (localhost [IPv6:::1]) by mail1.pinkwin.com (Postfix) with ESMTP id 04B6661DD2 for ; Sun, 4 Feb 2018 12:57:39 +0100 (CET) Received: from 192.168.178.70 (SquirrelMail authenticated user mark) by mail.pinkwin.com with HTTP; Sun, 4 Feb 2018 12:57:39 +0100 Message-ID: <69d128a8524692d0610ae3118580c859.squirrel@mail.pinkwin.com> Date: Sun, 4 Feb 2018 12:57:39 +0100 Subject: Looking for workaround to avoid deadlock when using exclusion constraint From: "Mark Scheffer" To: pgsql-general@lists.postgresql.org User-Agent: SquirrelMail/1.4.23 [SVN] MIME-Version: 1.0 Content-Type: text/plain;charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Priority: 3 (Normal) Importance: Normal X-CMAE-Envelope: MS4wfOos0ZM+1ErU4Exe3MHWl4w8GCMtuxXQ8zrkQz3F9W9qFA214GKDnUycB29EtJLbRlvRTwU1jB1A7X2+DXpiGcsOqLmLDEqqu4EOXQNq+nbmVDF/eGt2 XH7XJFt372CA1fuMMq3JxBzZsRSfmFx+zyoe9ibLu8uB5dKpNcXk/T3XhHlGuBqj7P+R2DTFctnucJ7OBitKjOsOkt+xNwrk6/8= List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi, Following code demonstrates an attempt to circumvent an unexpected deadlock (cf. BUG #15026) when using exclusion constraints. CREATE TABLE locked ( key text NOT NULL, EXCLUDE USING gist (key WITH =) ); DO $$ DECLARE id locked.key%TYPE; BEGIN WHILE id IS NULL LOOP BEGIN SELECT key FROM locked WHERE key = 'a' INTO STRICT id; EXCEPTION WHEN no_data_found THEN BEGIN INSERT INTO locked(key) values('a') RETURNING key INTO id; EXCEPTION WHEN deadlock_detected THEN RAISE WARNING 'Deadlock!! (t=%)', clock_timestamp(); END; END; END LOOP; END; $$; This (imperfect) code works fine when two sesions compete for the record, but will NEVER finish if three sessions compete! To demonstrate this, first run following code from a 4th psql session: DROP TABLE IF EXISTS locked; CREATE TABLE locked ( key text NOT NULL, EXCLUDE USING gist (key WITH =) ); BEGIN; INSERT INTO locked(key) values('a'); Then run the DO block above from three sessions. They will wait on a (share) lock as expected. Now performing a ROLLBACK in the 4th session, results in a never ending sequence of dead-lock exceptions plus retries in the other sessions. Only "solution" I can think of is to introduce an exclusive lock on some other entity just before doing the INSERT. Questions: - Anyone better ideas? - What happens within Postgresql (special to Exclusion constraints) that causes this deadlock and why is it (too) hard to fix it within the backend? Please check my two posts in pgsql-bugs on "BUG #15026: Deadlock using GIST index" for more info including a comment from backend source code where the dead-lock is predicted, but i.m.o underestimated. Regards, Mark