Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hzvm0-0004OZ-Um for pgsql-general@arkaria.postgresql.org; Tue, 20 Aug 2019 04:29:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hzvlz-0000Mj-NX for pgsql-general@arkaria.postgresql.org; Tue, 20 Aug 2019 04:29:31 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hzvlz-0000I5-8q for pgsql-general@lists.postgresql.org; Tue, 20 Aug 2019 04:29:31 +0000 Received: from mail-io1-xd35.google.com ([2607:f8b0:4864:20::d35]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hzvls-0007RJ-Rw for pgsql-general@postgresql.org; Tue, 20 Aug 2019 04:29:30 +0000 Received: by mail-io1-xd35.google.com with SMTP id t6so9352852ios.7 for ; Mon, 19 Aug 2019 21:29:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=P9sG/GnISe5CwSINEkOulxZqzlUpPmftPkOxLLbzmzQ=; b=jMHr7Nd7DTj0ULHw8Jhj/qBuZ5Y//nT5yGJAk8zSw3Cl0iCP4xPeOq/Ah0PdFzvqAU VSVlko9O+rfxANwLrPj91jMdZz9Xjw9xoTKEzyVGVt3f7KmRCPBz0ZPVZ/dKPRx9Hp0e D7u+BVTKqMVXdtkx1bsenMriiLay3UxdjhF5qF6h8DdsV44EltEE1We+iihHb08ozxyf 8oYpKgxPFSU4J5s00hnz7vQN96gaPnun8eykRvv2MDdssQpFp4bNe+fqcbsg3cvhQpV4 Z3QFkr8xMHJfUqcKn7dd440vDJ/ty0IXIt86T04rY1tCVkicI3lPlCEo2kj3Cn3i8BxY LfIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=P9sG/GnISe5CwSINEkOulxZqzlUpPmftPkOxLLbzmzQ=; b=gb46nk3ACDjZdEs/dcE+fTrOU4AxZNBOMCq7j1oMBvUacR+EaRSLTzXAUvPe0hjNPM owLpGrZ0D57AE7rSC4qqv2rYp5CwrNHn1lUXAgIsLz5LV62iZR/Pfsgb7rMF0XeROl8n lDJ2wfhe926OI7leopPsherHV2b9/b0E8/xmTWrqpcJuVMH9jbDqi2RJpqSDpsp4Mt9q jbVTeV9V+wliE08nti3lCGqBXL6sRteS0FhM2fmbga/LdNBYFGnmrOG/eVbBaUiP+7NP JZM/NZa9+o35dZ5/pqB2eqAjvUIazxmFmV7KmaczEWLwT8NlJnH45k9uPOrPn/++HXVK FZmA== X-Gm-Message-State: APjAAAVggVltOPjDMs6nEOlkdXoqeWSZToE9q9hojac6n6SR7hH3xL1a 1p94v6J5V2XqzzixR8o7RoM= X-Google-Smtp-Source: APXvYqwMN00CPte/zjzS80ARgn+J9oHs1pSsB++yYaNOgL9cM16WiFp9a0gsHHXVJsLHo5a5tyCA1g== X-Received: by 2002:a5d:8908:: with SMTP id b8mr6741865ion.237.1566275361571; Mon, 19 Aug 2019 21:29:21 -0700 (PDT) Received: from ?IPv6:2601:681:5500:1e3c:11a0:9d93:158:f8dd? ([2601:681:5500:1e3c:11a0:9d93:158:f8dd]) by smtp.gmail.com with ESMTPSA id s4sm32090031iop.25.2019.08.19.21.29.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 19 Aug 2019 21:29:21 -0700 (PDT) From: Rob Sargent Message-Id: <786FDC9D-35D3-4552-B6EF-975F202BCC66@gmail.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_327622C2-A89F-44BF-A71B-5DB7B3396619" Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.11\)) Subject: Re: SELECT all the rows where id is children of other node. Date: Mon, 19 Aug 2019 22:29:19 -0600 In-Reply-To: Cc: "pgsql-general@postgresql.org" To: pabloa98 References: X-Mailer: Apple Mail (2.3445.104.11) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --Apple-Mail=_327622C2-A89F-44BF-A71B-5DB7B3396619 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii > On Aug 19, 2019, at 7:42 PM, pabloa98 wrote: >=20 > Hello, >=20 > I have a huge table (100 million rows) of relations between nodes by = id in a Postgresql 11 server. Like this:=20 >=20 > CREATE TABLE relations ( > pid INTEGER NOT NULL, > cid INTEGER NOT NULL, > ) >=20 > This table has parent-child relations references between nodes by id. = Like: >=20 > pid -> cid > n1 -> n2 > n1 -> n3 > n1 -> n4 > n2 -> n21 > n2 -> n22 > n2 -> n23 > n22 -> n221 > n22 -> n222 >=20 > I would like to get a list of all the nodes being children (direct or = indirect) of any other node. >=20 > Example. The children of: >=20 > 1) n3: [] (n3 has not children) > 2) n22: [n221, n222] (n22 has 2 children: n221 and n222) > 3) n1: [n2, n21, n22, n23, n221, n222] (n1 has 6 children including = indirect children). >=20 > this pseudo SQL:=20 >=20 > SELECT * > FROM relations > WHERE has_parent(myId)=20 >=20 > It can be solved with a recursive function or stored procedure. But = that requires several passes. Is it possible to solve it in one pass? = Perhaps using some low-level function or join or some index expression = or auxiliary columns? >=20 > It is OK to create an index or similar using recursive expressions. = However, the SELECT expressions should be solved in one pass because of = speed. >=20 >=20 > Pablo Back at my desk now, to show the possibilities. with recursive descendants(parent, child) as=20 (select p.p, p.c from kids p where not exists (select 1 from kids c = where c.c =3D p.p) group by p.p, p.c union all select k.* from kids k, descendants d where k.p =3D d.child) select * from descendants; parent | child=20 --------+------- 1 | 3 1 | 2 1 | 4 2 | 21 2 | 22 2 | 23 22 | 221 22 | 222 (8 rows) with recursive descendants(parent, child) as=20 (select p.p, p.c from kids p where not exists (select 1 from kids c = where c.c =3D p.p) group by p.p, p.c union all select k.* from kids k, descendants d where k.p =3D d.child) select d.parent, array_agg(d.child) from descendants d group by = d.parent; parent | array_agg =20 --------+------------ 1 | {3,2,4} 22 | {221,222} 2 | {21,22,23} (3 rows) --Apple-Mail=_327622C2-A89F-44BF-A71B-5DB7B3396619 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=us-ascii

On Aug 19, 2019, at 7:42 PM, pabloa98 <pabloa98@gmail.com> = wrote:

Hello,

I have a huge table (100 million rows) = of relations between nodes by id in a Postgresql 11 server. Like this: =

CREATE TABLE relations (
    pid INTEGER NOT NULL,
    cid INTEGER NOT NULL,
)

This table has = parent-child relations references between nodes by id. Like:

pid -> cid
n1 = -> n2
n1 -> n3
n1 -> = n4
n2 -> n21
n2 -> = n22
n2 -> n23
n22 -> = n221
n22 -> n222

I would like to get a list of all the = nodes being children (direct or indirect) of any other node.

Example. The children of:

1) n3: []  (n3 has not children)
2) = n22: [n221, n222]  (n22 has 2 children: n221 and n222)
3) n1: [n2, n21, n22, n23, n221, n222]  = (n1 has 6 children including indirect children).

this pseudo SQL: 

SELECT *
FROM relations
WHERE has_parent(myId) =

It can be solved with a recursive function or stored = procedure. But that requires several passes. Is it possible to solve it = in one pass? Perhaps using some low-level function or join or some index = expression or auxiliary columns?

It is OK to create an index or similar = using recursive expressions. However, the SELECT expressions should be = solved in one pass because of speed.


Pablo

Back at my desk now, to = show the possibilities.

with = recursive descendants(parent, child) as 
(select p.p, p.c = from kids p where not exists (select 1 from kids c where c.c =3D p.p) = group by p.p, p.c
 union all
 select k.* = from kids k, descendants d where k.p =3D d.child)
 select = * from descendants;

 parent | = child 
--------+-------
      1 = |     3
      1 |     = 2
      1 |     4
  =     2 |    21
      2 | =    22
      2 |   =  23
     22 |   221
  =    22 |   222
(8 rows)

with recursive = descendants(parent, child) as 
(select p.p, = p.c from kids p where not exists (select 1 from kids c where c.c =3D = p.p) group by p.p, p.c
 union all
 select k.* from kids k, descendants d where k.p =3D = d.child)
 select d.parent, array_agg(d.child) = from descendants d group by d.parent;

 parent | array_agg =  
--------+------------
      1 | {3,2,4}
  =    22 | {221,222}
      2 = | {21,22,23}
(3 rows)


= --Apple-Mail=_327622C2-A89F-44BF-A71B-5DB7B3396619--