Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 7504E63347D for ; Fri, 28 Aug 2009 13:19:38 -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 99671-10 for ; Fri, 28 Aug 2009 16:19:30 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from out02.mta.xmission.com (out02.mta.xmission.com [166.70.13.232]) by mail.postgresql.org (Postfix) with ESMTP id 357DF6333E8 for ; Fri, 28 Aug 2009 13:19:30 -0300 (ADT) Received: from in02.mta.xmission.com ([166.70.13.52]) by out02.mta.xmission.com with esmtp (Exim 4.62) (envelope-from ) id 1Mh4Ah-0004bI-Uh; Fri, 28 Aug 2009 10:19:27 -0600 Received: from rsargent.dsl.xmission.com ([199.104.125.126] helo=[192.168.0.7]) by in02.mta.xmission.com with esmtps (TLSv1:AES256-SHA:256) (Exim 4.69) (envelope-from ) id 1Mh4Ah-0003qx-LX; Fri, 28 Aug 2009 10:19:27 -0600 Message-ID: <4A98038A.1080507@gmail.com> Date: Fri, 28 Aug 2009 10:19:22 -0600 From: Rob Sargent User-Agent: Thunderbird 2.0.0.23 (X11/20090817) MIME-Version: 1.0 To: rawi CC: pgsql-sql@postgresql.org Subject: Re: recursively isolate pedigrees, based only on person_id, mother_id and father_id References: <25191664.post@talk.nabble.com> In-Reply-To: <25191664.post@talk.nabble.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-XM-SPF: eid=; ; ; mid=; ; ; hst=in02.mta.xmission.com; ; ; ip=199.104.125.126; ; ; frm=robjsargent@gmail.com; ; ; spf=neutral X-XM-DomainKey: sender_domain=gmail.com; ; ; sender=robjsargent@gmail.com; ; ; status=no signature X-SA-Exim-Connect-IP: 199.104.125.126 X-SA-Exim-Rcpt-To: only4com@web.de, pgsql-sql@postgresql.org, robjsargent@gmail.com X-SA-Exim-Mail-From: robjsargent@gmail.com X-SA-Exim-Version: 4.2.1 (built Thu, 25 Oct 2007 00:26:12 +0000) X-SA-Exim-Scanned: No (on in02.mta.xmission.com); Unknown failure 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: 200908/149 X-Sequence-Number: 33058 On the assumption that you wish to generate the pedigrees for analysis or charting, why not perform the recursion in those layers (or their supporting software), This does not require a large number of sql calls since using an in-clause each call will gather one generation (ascending or descending) for all new persons retrieved in any iteration. How deep are your pedigrees and what is their typical size? rawi wrote: > Hello! > > Given a table "persons", where the individuals are bound to each other only > via the foreign keys mother_id and father_id as self joins on persons, like > that > > create table persons ( > id serial primary key, > sex character(1), > father_id integer default null references persons(id) on update cascade on > delete set null, > mother_id integer default null references persons(id) on update cascade on > delete set null > ); > > INSERT INTO persons(id, sex, father_id,mother_id) VALUES(1,'m',null,null); > INSERT INTO persons(id, sex, father_id,mother_id) VALUES(2,'f',null,null); > INSERT INTO persons(id, sex, father_id,mother_id) VALUES(8,'m',null,null); > INSERT INTO persons(id, sex, father_id,mother_id) VALUES(9,'f',null,null); > INSERT INTO persons(id, sex, father_id,mother_id) VALUES(3,'m',1,2); > INSERT INTO persons(id, sex, father_id,mother_id) VALUES(4,'f',8,9); > INSERT INTO persons(id, sex, father_id,mother_id) VALUES(5,'f',3,4); > INSERT INTO persons(id, sex, father_id,mother_id) VALUES(6,'m',3,4); > INSERT INTO persons(id, sex, father_id,mother_id) VALUES(7,'m',8,9); > INSERT INTO persons(id, sex, father_id,mother_id) VALUES(10,'m',null,null); > INSERT INTO persons(id, sex, father_id,mother_id) VALUES(11,'m',10,5); > INSERT INTO persons(id, sex, father_id,mother_id) VALUES(12,'f',7,5); -- > consanguinity > INSERT INTO persons(id, sex, father_id,mother_id) VALUES(100,'m',null,null); > INSERT INTO persons(id, sex, father_id,mother_id) VALUES(200,'f',null,null); > INSERT INTO persons(id, sex, father_id,mother_id) VALUES(300,'m',100,200); > INSERT INTO persons(id, sex, father_id,mother_id) VALUES(400,'f',null,null); > INSERT INTO persons(id, sex, father_id,mother_id) VALUES(500,'f',300,400); > > These would be the graphs of the 2 families: > http://www.nabble.com/file/p25191664/family1.jpg > http://www.nabble.com/file/p25191664/family2.jpg > > I hoped to find a recursive SQL or function, which would extract the WHOLE > family of any given person.id from the table with many families. > > After failing to accomplish this with a recursive SQL I found on the web an > advice from Celko (???) > http://www.eggheadcafe.com/conversation.aspx?messageid=29498840&threadid=29498808 > to better keep away form such things... :( > > Did someone gathered a closer expertise to that? > > Thank you very much for any hint! > Regards > Rawi >