Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 62645637466 for ; Tue, 16 Mar 2010 19:22:27 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 20823-04 for ; Tue, 16 Mar 2010 22:22:16 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id CF5B265B079 for ; Tue, 16 Mar 2010 19:22:16 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id o2GMMCpN015828; Tue, 16 Mar 2010 18:22:12 -0400 (EDT) To: Richard Huxton cc: Tony Cebzanov , pgsql-sql@postgresql.org Subject: Re: Avoiding cycles in a directed graph In-reply-to: <4BA002DC.1050700@archonet.com> References: <4B9FDFDE.5030005@andrew.cmu.edu> <4B9FE752.4080709@archonet.com> <14113.1268771643@sss.pgh.pa.us> <4B9FEEE8.1060404@andrew.cmu.edu> <14550.1268773752@sss.pgh.pa.us> <4BA002DC.1050700@archonet.com> Comments: In-reply-to Richard Huxton message dated "Tue, 16 Mar 2010 22:14:52 -0000" Date: Tue, 16 Mar 2010 18:22:12 -0400 Message-ID: <15827.1268778132@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 201003/117 X-Sequence-Number: 33824 Richard Huxton writes: > On 16/03/10 21:09, Tom Lane wrote: >> If you don't expect this to be common, maybe you could fix the >> concurrency issue by taking a table-wide lock that locks out >> other writers. > Surely SELECT FOR UPDATE on the parents would be sufficient? If there's > no overlap between (currently non-cyclic) graphs being altered then > there can't be any conflict. Um, what if the cycle is being formed from whole cloth? For instance T1 inserts an edge A->B while T2 is inserting B->A. There are no pre-existing rows to lock, but there will still be a cycle after they both commit. Also it seems pretty deadlock-prone if there are multiple existing rows to try to lock. Perhaps you could work around the risk by locking those rows one at a time in an application-defined ordering ... but I'm afraid the performance would be poor, unless the connected graphs are always very small. On the whole I think Tony's better off with a KISS approach, ie just lock the whole table against other writers. regards, tom lane