X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id A5FE3329D3B for ; Thu, 9 Sep 2004 02:43:55 +0100 (BST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 16808-07 for ; Thu, 9 Sep 2004 01:43:50 +0000 (GMT) Received: from mail.canberra.edu.au (mail.canberra.edu.au [137.92.97.81]) by svr1.postgresql.org (Postfix) with ESMTP id 748CA329D7A for ; Thu, 9 Sep 2004 02:43:51 +0100 (BST) Received: from [137.92.10.57] (fooey.canberra.edu.au [137.92.10.57]) by mail.canberra.edu.au (iPlanet Messaging Server 5.2 Patch 1 (built Aug 19 2002)) with ESMTP id <0I3R00KAG3H2XP@mail.canberra.edu.au> for pgsql-sql@postgresql.org; Thu, 09 Sep 2004 11:43:50 +1000 (EST) Date: Thu, 09 Sep 2004 11:43:33 +1000 From: Terence Kearns Subject: check for circular references in a 2-table heirachy To: pgsql-sql@postgresql.org Message-id: <413FB545.9090100@isd.canberra.edu.au> MIME-version: 1.0 Content-type: text/plain; charset=us-ascii; format=flowed Content-transfer-encoding: 7BIT X-Accept-Language: en-au, en-gb, en, en-us User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.2) Gecko/20040803 MultiZilla/1.6.3.2d X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200409/55 X-Sequence-Number: 18901 Does anyone know how I could check to ensure circular references are no created in my 2 table heirachy structure? Here are the tables albums album_relations +------------+ +-----------------+ | album_id | | parent_album_id | | title | | child_album_id | +------------+ +-----------------+ This structure allows an album to have multiple parents. I have written a recursive function (on a PHP class) which creates an XML tree structure based on a 2D array result set**. It has a depth checker so you can set the limit of recursion. This is all well and good, but I want to create a PLPG/SQL trigger on the album_relations table to ensure that I won't have to throw the "depth limit exceeded" exception due to circular references. Any ideas would be appreciated. ** 2D array result set is built from SELECT a.*,r.parent_album_id FROM album_relations r RIGHT JOIN albums a ON a.album_id = r.child_album_id