pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alban Hertroys <haramrae@gmail.com>
To: Avi Weinberg <AviW@gilat.com>
Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: Recursive Parent-Child Function Bottom Up
Date: Mon, 26 Jul 2021 17:52:06 +0200
Message-ID: <903EB164-89AA-4CB4-A031-A707954E6B23@gmail.com> (raw)
In-Reply-To: <DB9PR07MB718032F033C6D95B92E5BAAECBE89@DB9PR07MB7180.eurprd07.prod.outlook.com>
References: <DB9PR07MB718032F033C6D95B92E5BAAECBE89@DB9PR07MB7180.eurprd07.prod.outlook.com>


> On 26 Jul 2021, at 17:19, Avi Weinberg <AviW@gilat.com> wrote:
> 
> Hi,
>  
> I would like to populate the children_ids column with all the ids of the children recursively (+ grandchildren etc.)
> If I do it top-bottom I will end up doing extra work since there is no need to go all levels down if I can just compute my IMMEDIATE children "children_ids" and just concatenate all their lists. 

(…)

> create table tree(id int primary key, parent int, children_ids text);
> insert into tree (id, parent) values
> (273,   0),
> (274,      273),
> (275,      273),
> (277,      273),
> (278,      277),
> (280,      275),
> (281,      280),
> (282,      281),
> (283,      282),
> (284,      282),
> (285,      282),
> (286,      282),
> (287,      282),
> (288,      282),
> (289,      282),
> (290,      281),
> (291,      290),
> (292,      290),
> (293,      290),
> (294,      290),
> (295,      290);

First you need to figure out what your starting set of nodes is, and since you’re going to go bottom-up, those are your leaf nodes. Without any indicators for that though, you’ll have to determine that from a sub-query.

Something like this:

with recursive foo (id, parent, children_ids) as (
	select id, parent, null::text
	  from tree t
	 where not exists (
		select 1 from tree c where c.parent = t.id
	 )
	union all
	select t.id, t.parent
	,	f.id || case f.children_ids when '' then '' else ',’ end || f.children_ids
	  from foo f
	  join tree t on f.parent = t.id
	 where f.parent <> 0
;

Regards,

Alban Hertroys
--
If you can't see the forest for the trees,
cut the trees and you'll find there is no forest.






view thread (7+ messages)  latest in thread

Message-ID: <903EB164-89AA-4CB4-A031-A707954E6B23@gmail.com>
Permalink:  ../903EB164-89AA-4CB4-A031-A707954E6B23@gmail.com/
Also on:    postgresql.org/message-id/903EB164-89AA-4CB4-A031-A707954E6B23@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: haramrae@gmail.com, AviW@gilat.com
  Subject: Re: Recursive Parent-Child Function Bottom Up
  In-Reply-To: <903EB164-89AA-4CB4-A031-A707954E6B23@gmail.com>
  References: <DB9PR07MB718032F033C6D95B92E5BAAECBE89@DB9PR07MB7180.eurprd07.prod.outlook.com> <903EB164-89AA-4CB4-A031-A707954E6B23@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox