Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m82ts-00076w-5O for pgsql-general@arkaria.postgresql.org; Mon, 26 Jul 2021 15:52:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m82tq-0002tp-SS for pgsql-general@arkaria.postgresql.org; Mon, 26 Jul 2021 15:52:14 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m82tq-0002ss-HM for pgsql-general@lists.postgresql.org; Mon, 26 Jul 2021 15:52:14 +0000 Received: from mail-ed1-x52d.google.com ([2a00:1450:4864:20::52d]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1m82tk-0003M3-B6 for pgsql-general@lists.postgresql.org; Mon, 26 Jul 2021 15:52:14 +0000 Received: by mail-ed1-x52d.google.com with SMTP id r16so11187300edt.7 for ; Mon, 26 Jul 2021 08:52:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=R1Z1rYtf/cLaXZdIlGYNt/qw1lhvmxPQrOmGoTcRedI=; b=kBdhowRvpp3oMFNKKU73ltPQCwGamwdnei+kRoVov9PHnQQ9+9zX4vOC6DAmcXr50m f1DYX+Z0TM8bqe/vtrBMojha613FIclq1ZTP6t+uLSOdQ3GY/Yvn717tztwJmO1PyCUU GMxjUqoUtI81ru5UE4JwI90WKGe1mUB2tPet9FJMnDX+mWt+ujT0Z9Jh+OJxUAGMNjHL wrxUWNS10SMYqCGYc9ADt4Zk7ggeAi6/YgS8Jl5bFrzb07JhFXDpx+Xzm0sPuHXzMEAC 345K8tahMA4W1Pakn1wokpnDU9v8WXeCINSFbYAIPgD7PNzVGK09f3eEv1fQxL1NOUou totw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=R1Z1rYtf/cLaXZdIlGYNt/qw1lhvmxPQrOmGoTcRedI=; b=Zh0DpfOCoyGdSk45hMsim9m6S2HTt9SuL76K8mpM7erUjzu8lWdFOcm4n4WO5Wtag4 nXRYWFvdz9ues7Qb/VkS+vNlW0nn+g878ZL3n4CgFcAHTcicsjkN58RXcOj/f4XVDSEm u/3msaQrW3G0MRUAWYn5My18bI6gKtAz740/QJH4gqND6+tpCuTPupNohIZjtCGGhFZ/ WRfPs1IwoLuVSK3YQC0mL9hIey//yI4bPWwYwJ8V1RLjmIPB+/n+dW5otlABAhBgPWyJ OxomBuOjQ7hP4tyyeLSCGKrIBbL+OFNZlL8iDcw2BhgW9KzjSBceVyGnDcD/riHyj49M 7vEg== X-Gm-Message-State: AOAM533irfEPl7GtwpGP9VrGCS3UhOR9du0PGi7SL8tHU2X5UaMDMC7C V6eATJ22OVxX0HHh3/ukKHM= X-Google-Smtp-Source: ABdhPJxZWQFgg9U81SL14ib4nlS95pkkEoNLL1bX+SBzOPA55mKAWdFW2LKJnEAhvFwZT8h4vxZkDw== X-Received: by 2002:a05:6402:35c7:: with SMTP id z7mr22424866edc.36.1627314727471; Mon, 26 Jul 2021 08:52:07 -0700 (PDT) Received: from hollewijn.internal (217-19-30-105.dsl.cambrium.nl. [217.19.30.105]) by smtp.gmail.com with ESMTPSA id y6sm96225edv.88.2021.07.26.08.52.06 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 26 Jul 2021 08:52:07 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.17\)) Subject: Re: Recursive Parent-Child Function Bottom Up From: Alban Hertroys In-Reply-To: Date: Mon, 26 Jul 2021 17:52:06 +0200 Cc: "pgsql-general@lists.postgresql.org" Content-Transfer-Encoding: quoted-printable Message-Id: <903EB164-89AA-4CB4-A031-A707954E6B23@gmail.com> References: To: Avi Weinberg X-Mailer: Apple Mail (2.3445.104.17) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On 26 Jul 2021, at 17:19, Avi Weinberg wrote: >=20 > Hi, > =20 > 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.=20 (=E2=80=A6) > 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=E2=80=99re going to go bottom-up, those are your leaf nodes. = Without any indicators for that though, you=E2=80=99ll 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 =3D t.id ) union all select t.id, t.parent , f.id || case f.children_ids when '' then '' else ',=E2=80=99= end || f.children_ids from foo f join tree t on f.parent =3D 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.