Re: [SQL] Avoiding cycles in a directed graph

2010-03-17 Thread Tony Cebzanov
On 3/16/10 6:22 PM, Tom Lane wrote: > Richard Huxton writes: > > > 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. For

Re: [SQL] Avoiding cycles in a directed graph

2010-03-16 Thread Tom Lane
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 b

Re: [SQL] Avoiding cycles in a directed graph

2010-03-16 Thread Richard Huxton
On 16/03/10 21:09, Tom Lane wrote: Tony Cebzanov writes: I'm okay with running the big, fat WITH RECURSIVE query in my insert trigger if I have to -- it won't be great for performance, but I don't expect this to be a frequent operation, so I'll accept the performance hit if it works. Unfortu

Re: [SQL] Avoiding cycles in a directed graph

2010-03-16 Thread Tom Lane
Tony Cebzanov writes: > I'm okay with running the big, fat WITH RECURSIVE query in my insert > trigger if I have to -- it won't be great for performance, but I don't > expect this to be a frequent operation, so I'll accept the performance > hit if it works. > Unfortunately I can't even get that w

Re: [SQL] Avoiding cycles in a directed graph

2010-03-16 Thread Tony Cebzanov
On 3/16/10 4:34 PM, Tom Lane wrote: > The same kind of problem exists for unique and foreign key constraints, > both of which use low-level locking mechanisms to catch such cases. > There's no way that I can see to express the "no cycle" constraint as a > uniqueness constraint unfortunately. You c

Re: [SQL] Avoiding cycles in a directed graph

2010-03-16 Thread Tom Lane
Richard Huxton writes: > On 16/03/10 19:45, Tony Cebzanov wrote: >> That's great to avoid looping forever on queries, but what about >> preventing anyone from inserting edges that would create cycles in the >> first place? I reckon I'll need a trigger of some sort, but nothing >> I've tried has e

Re: [SQL] Avoiding cycles in a directed graph

2010-03-16 Thread Richard Huxton
On 16/03/10 19:45, Tony Cebzanov wrote: I'm using the following table to represent an acyclic directed graph: [snip] I see there is an example in the online docs for detecting cycles in recursive queries, and I've adapted the example query to the table above: [snip] That's great to avoid loop