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 1i03ty-0006IE-5S for pgsql-general@arkaria.postgresql.org; Tue, 20 Aug 2019 13:10:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1i03tx-0007Id-0D for pgsql-general@arkaria.postgresql.org; Tue, 20 Aug 2019 13:10:17 +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 1i03tw-0007IW-Mk for pgsql-general@lists.postgresql.org; Tue, 20 Aug 2019 13:10:16 +0000 Received: from mail-io1-xd32.google.com ([2607:f8b0:4864:20::d32]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i03tu-0004GY-BU for pgsql-general@postgresql.org; Tue, 20 Aug 2019 13:10:16 +0000 Received: by mail-io1-xd32.google.com with SMTP id s21so12024726ioa.1 for ; Tue, 20 Aug 2019 06:10:14 -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=7NzY8qytbGnox2pN55AVpeYE5/1/lYtTbuJgsYPWXYA=; b=L1xe8t/pLcjFR+VAgSn9M1vc86auk2ZXuYoNEpo3fBtI5NMvDDgDqAMdYdTHTxbfTt KqLESP3RGFbznarI0q5OAtuhfE1wIWbfcXQrrn11qB+BXu9g/+FY4aO/spUu8V8N9y5n fGOdta64DIc1fp7tpRZqOk/kv5Be53X1uAoQBmIdOtya2oQ+2YiBbw8jDTQnRmarxpwq 2mnyGNNSeze6oFt6DMLt22P2UpxuPAbDf8NHtx7a9Lv0nWyRZ4t1gidNSOHAoIBu9JDc upZ9Lbi2NGTy6d1y4LpgJVHN3MF7wErsanuAn7IL+NzNL4BqqFuh9RNkh9cV80Y7M7rx DVXw== 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=7NzY8qytbGnox2pN55AVpeYE5/1/lYtTbuJgsYPWXYA=; b=gggclB5j9U+30kRPYksrNRdxgDdU3JSaSA13hKrJxGL7U4/A7ELB/G/e4RWhW3aiDQ QL5zfNnb8z9LNHnurIKmEZhE7KXEmPNEh1ffnH2oyCNTbLU8CE3xh9GBZJ8Ju8cac++Z 8p4M01zcOJOTUhbz7IJCxafOc13B3csIG6AJsXgOuCwF2VRyFbFdWYijQgstaL6A++lw aKv4qA8TfUqQ+6jhRYarMAJMCJ0j0ppfM6NH1G+nI9bksE0JotiFDhq17+VaM3Ac3skP +32XDALLtKp/g3FNPyiLkLwCFiqIx5WVIwXPdL2wbqYVIeGWfgpqJLlgX7zIywnAqDG6 PSKQ== X-Gm-Message-State: APjAAAU4+88hAzcw7OZ6CjXedtRvvyKHOgMoipnNHoKR1ioWtrpLiwmP i3ugXISNwSMKvYFR7YFk5C8= X-Google-Smtp-Source: APXvYqwP8Ot0jM7L4gDiyxvyIPqYWfAmvIjsNHd2wbPcxkxeVGoU5QHvZOVTOC3eGGATWnCgVj2aOg== X-Received: by 2002:a02:810:: with SMTP id 16mr3698710jac.121.1566306612395; Tue, 20 Aug 2019 06:10:12 -0700 (PDT) Received: from [192.168.1.108] (c-73-63-59-118.hsd1.ut.comcast.net. [73.63.59.118]) by smtp.gmail.com with ESMTPSA id i9sm18900433ioe.35.2019.08.20.06.10.11 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 20 Aug 2019 06:10:11 -0700 (PDT) From: Rob Sargent Message-Id: <60F3F05A-84AF-4944-8EA2-5484A5D5B0C3@gmail.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_C7AB3E27-086F-4D1E-92F2-74B3BB02DA78" 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: Tue, 20 Aug 2019 07:10:08 -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=_C7AB3E27-086F-4D1E-92F2-74B3BB02DA78 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > 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 I could not find away to handle the branches but this is more complete. with recursive descendants (last, children) as=20 (select c.c, array[null::int] from kids c where not exists (select 1 = from kids p where c.c =3D p.p) union all select k.p, array[k.c] || l.children from kids k, descendants l where = k.c =3D l.last) select last, children from descendants where children[1] is not null = order by last last | children =20 ------+----------------- 1 | {2,22,222,NULL} 1 | {4,NULL} 1 | {2,21,NULL} 1 | {2,23,NULL} 1 | {2,22,221,NULL} 1 | {3,NULL} 2 | {22,221,NULL} 2 | {22,222,NULL} 2 | {21,NULL} 2 | {23,NULL} 22 | {221,NULL} 22 | {222,NULL} (12 rows) I=E2=80=99ll throw in the towel now= --Apple-Mail=_C7AB3E27-086F-4D1E-92F2-74B3BB02DA78 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8

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
I could not find away to handle the branches = but this is more complete.
with recursive = descendants (last, children) as 
(select c.c, = array[null::int] from kids c where not exists (select 1 from kids p = where c.c =3D p.p)
 union all
 select k.p, array[k.c] || l.children from kids k, = descendants l where k.c =3D l.last)
 select = last, children from descendants where children[1] is not null order by = last
 last |    children   =   
------+-----------------
    1 | {2,22,222,NULL}
  =   1 | {4,NULL}
    1 | = {2,21,NULL}
    1 | {2,23,NULL}
    1 | {2,22,221,NULL}
  =   1 | {3,NULL}
    2 | = {22,221,NULL}
    2 | = {22,222,NULL}
    2 | {21,NULL}
    2 | {23,NULL}
  =  22 | {221,NULL}
   22 | = {222,NULL}
(12 rows)

I=E2=80=99ll throw in the towel = now
= --Apple-Mail=_C7AB3E27-086F-4D1E-92F2-74B3BB02DA78--