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 1hzxR9-0007YK-UI for pgsql-general@arkaria.postgresql.org; Tue, 20 Aug 2019 06:16:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hzxR7-0000zu-5r for pgsql-general@arkaria.postgresql.org; Tue, 20 Aug 2019 06:16:05 +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 1hzxR6-0000y7-OZ for pgsql-general@lists.postgresql.org; Tue, 20 Aug 2019 06:16:04 +0000 Received: from mail-qt1-x832.google.com ([2607:f8b0:4864:20::832]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hzxQw-0008PJ-9Y for pgsql-general@postgresql.org; Tue, 20 Aug 2019 06:16:04 +0000 Received: by mail-qt1-x832.google.com with SMTP id x4so4775405qts.5 for ; Mon, 19 Aug 2019 23:15:53 -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=W0QiRzPGFbBHcBwCqV3N1P008rtME60kP8Ip6chfN50=; b=OfVYqGLc2zvBig4l8kztAk24QXtummvkEOvG/1zVMyrZx6PBIYtn249uEA0M2is5wO hgPCQGNah/2UmE72HGtedUdYKJQEti3OFET1gG9XnRJkHFknwHQpeOaLmnjgJ39Dhm+I YgSmHORPTXi6YPGffMUvE0HSDGYBQ/BbjhL4y7nWGr2AgN/+pt5HUuMELRBjpBx/6FTN JYHtFj8PPQS7d14haG+fNCAZyEJn+V+jTFc848Pm1hM/B/FYo612L64d5y2ZCBQiPs+4 M73Db6hg8FPShveSWA9J8fdQuamqIaYijCjdVslwk7Wyzcz09SEqRsyb3xX4yh8LpwXq mBDQ== 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=W0QiRzPGFbBHcBwCqV3N1P008rtME60kP8Ip6chfN50=; b=GtegDDbhSFmIuMwIye8g0DhN6Rt+mteMsZA2AqQYCbs5KvjjDlXP7GQd8nf88biCpD uwO60SedFZKYlKY9K/h3/rw8WR9ORJ3FIMKnKDFc9QwXF0XPlGDBmqUVzKB37bD8Sh3E zVO1ksmqgFoT48ldI4Vf3+G/oYqYLDuOxCTgMxTfono3KZYWLunrRblHaSqybuidenuG TAT8xjCmBS3dxs13C/Thn3uBimv6C6jKOlJaqlugHjkTty7bC0jSIl2CZv1nI4RFH5ow s6qZvQIvDDjsumnEc60AyAmfjQwKUtJhZ+W1euJTa3DwozKjE3A8PmNSKRDBHnF+1rrt 0dBw== X-Gm-Message-State: APjAAAXDFq3PSlhW5o86gy3COSWco5TJnX5M20uIevVTK3bZpOiE/sVJ 3dILrhmCaGGWuhdK77V5BxBZwf6R X-Google-Smtp-Source: APXvYqx4K1WE8kdvkOV0Zge3AjuV0RViRasUaSEbYLdg2oZjTBYgIN50jRmOHMfed8G6S2vYi9YSFg== X-Received: by 2002:a05:6638:105:: with SMTP id x5mr1496970jao.43.1566275456597; Mon, 19 Aug 2019 21:30:56 -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 e22sm12671656iog.2.2019.08.19.21.30.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 19 Aug 2019 21:30:56 -0700 (PDT) From: Rob Sargent Message-Id: Content-Type: multipart/alternative; boundary="Apple-Mail=_28717C4B-B23B-4489-9614-63A62DD3609B" 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:30:55 -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=_28717C4B-B23B-4489-9614-63A62DD3609B 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 ooops. didn=E2=80=99t get all generations. sorry= --Apple-Mail=_28717C4B-B23B-4489-9614-63A62DD3609B 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

ooops. didn=E2=80=99= t get all generations. sorry
= --Apple-Mail=_28717C4B-B23B-4489-9614-63A62DD3609B--