Re: Recursive Parent-Child Function Bottom Up - Mailing list pgsql-general

From Rob Sargent
Subject Re: Recursive Parent-Child Function Bottom Up
Date
Msg-id 5fd63f2e-4fc0-fff6-fbd3-ed340894e33c@gmail.com
Whole thread Raw
In response to Recursive Parent-Child Function Bottom Up  (Avi Weinberg <AviW@gilat.com>)
List pgsql-general
On 7/26/21 9:19 AM, Avi Weinberg wrote:
@font-face {font-family:"Cambria Math"; panose-1:2 4 5 3 5 4 6 3 2 4;}@font-face {font-family:Calibri; panose-1:2 15 5 2 2 2 4 3 2 4;}p.MsoNormal, li.MsoNormal, div.MsoNormal {margin:0cm; margin-bottom:.0001pt; text-align:right; direction:rtl; unicode-bidi:embed; font-size:11.0pt; font-family:"Calibri",sans-serif;}a:link, span.MsoHyperlink {mso-style-priority:99; color:#0563C1; text-decoration:underline;}a:visited, span.MsoHyperlinkFollowed {mso-style-priority:99; color:#954F72; text-decoration:underline;}span.EmailStyle17 {mso-style-type:personal-compose; font-family:"Calibri",sans-serif; color:windowtext;}.MsoChpDefault {mso-style-type:export-only; font-family:"Calibri",sans-serif;}div.WordSection1 {page:WordSection1;}

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.

 

I looked at https://stackoverflow.com/questions/41376655/how-can-i-traverse-a-tree-bottom-up-to-calculate-a-weighted-average-of-node-va

But was not able to get it to work on my case.  

 

Your assistance is most welcome!

 

 

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);

IMPORTANT - This email and any attachments is intended for the above named addressee(s), and may contain information which is confidential or privileged. If you are not the intended recipient, please inform the sender immediately and delete this email: you should not copy or use this e-mail for any purpose nor disclose its contents to any person.
I'm sorry, what output format are you looking for?  Perhaps an array of all descendants for each founder? (founder int, descendants int[]).   I recommend a null for no-known-parent rather than zero.  Or just founder int, descendant int? With or without sub-trees?

pgsql-general by date:

Previous
From: Marcin Barczynski
Date:
Subject: Re: Why does VACUUM FULL pg_class sometimes wait for ShareLock on another transaction after getting AccessExclusiveLock on pg_class?
Next
From: Rob Sargent
Date:
Subject: Re: Recursive Parent-Child Function Bottom Up