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 1hzv0e-0003CV-Cc for pgsql-general@arkaria.postgresql.org; Tue, 20 Aug 2019 03:40:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hzv0d-0007I5-82 for pgsql-general@arkaria.postgresql.org; Tue, 20 Aug 2019 03:40:35 +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 1hzv0c-0007Hx-Rs for pgsql-general@lists.postgresql.org; Tue, 20 Aug 2019 03:40:34 +0000 Received: from mail-io1-xd2a.google.com ([2607:f8b0:4864:20::d2a]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hzv0a-00075R-K7 for pgsql-general@postgresql.org; Tue, 20 Aug 2019 03:40:34 +0000 Received: by mail-io1-xd2a.google.com with SMTP id j4so9130098iop.11 for ; Mon, 19 Aug 2019 20:40:32 -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=/YBc+osgp+Jiz5VhJMO4cVpgT8Qot9VGy1vmjzRQqTU=; b=Ecca5H58plUKGlZBNT7cn7I4dM4VFq2RlR078LTqsQoyAbzrf9LrXGQg8bYDLwDgWj fwaNojmzpa8AVvM0qHFyn6vvzjNn1diOlbT6FzgYysQXQ9V2Eh8wO4ydq+sCSpwHzsm5 aSmQdV8A7TWQ1dTFVF3wNfZAev1Oq3SzZhXvj4eJwIOYvD3hQb67GFzjU5eoPY7lwLU9 IbTPturo9Na6V0Gc8QTAD7E9esnYKCPwvVoVZIuKjktskVhMR5kQcRE+GX3Q1nIv+zgE 5cJZ1Y36t9VX4l6zLjjHSeqXchWi3JwKdnTfZh85DXsScgIFYMTAONnNWn2iYlEVQN6t eeLw== 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=/YBc+osgp+Jiz5VhJMO4cVpgT8Qot9VGy1vmjzRQqTU=; b=i3JxcKCB4aZFU2eejDazcRTisH4mzf20AJOjigstBUBz4FPssDkT+mV5AekuG62+J8 ZTq8QgRn45tQ6zV5mVpRH/jHOFe7jAKBJLoxE0CkQuhEOYVimJSkMnw+JA65TixlM1W0 ChJYAZyC4x+Dl/A3qRLp3039UtmBNx0SAAfvh2RWTPmwjbJetAwr90GS/pV1EGshej8d UCfeLU5WuOHbpKhmK7H7ihvk6l8Heq9BX/Bfil1hoSw0wK5onnmQvTFHHRx0Lwi3gEps 4WBvu8nd5QGDFo/rd6p0cBaXc6xYiXKWfALwJ0CE30jA6n6gtJL9wstuaHFqjCb78O8Z L3uQ== X-Gm-Message-State: APjAAAWja9Eo0vXo3EPgZuyhma0D+FRYGDyKfpbbJg+2kliPD9BDNoRN +osUEQuWLwmZXhqZ6D9aAiM= X-Google-Smtp-Source: APXvYqwX1FPLZNa399Hlz7ca6fGgPwAYqKw39PardJaYn+aaSF4DQz1KwwUGBUYpf6chNMDJJEZ5Pw== X-Received: by 2002:a5d:8599:: with SMTP id f25mr10665238ioj.265.1566272430694; Mon, 19 Aug 2019 20:40:30 -0700 (PDT) Received: from ?IPv6:2601:681:5500:1e3c:91b8:557d:9a54:9f5f? ([2601:681:5500:1e3c:91b8:557d:9a54:9f5f]) by smtp.gmail.com with ESMTPSA id v10sm19660794iob.43.2019.08.19.20.40.29 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 19 Aug 2019 20:40:30 -0700 (PDT) Content-Type: multipart/alternative; boundary=Apple-Mail-776A7564-41FF-4BF8-83E3-EE0CC1AAFD89 Mime-Version: 1.0 (1.0) Subject: Re: SELECT all the rows where id is children of other node. From: Rob Sargent X-Mailer: iPhone Mail (16G77) In-Reply-To: Date: Mon, 19 Aug 2019 21:40:29 -0600 Cc: pgsql-general@postgresql.org Content-Transfer-Encoding: 7bit Message-Id: References: To: pabloa98 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --Apple-Mail-776A7564-41FF-4BF8-83E3-EE0CC1AAFD89 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable > 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 indi= rect) 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 indir= ect 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 r= equires several passes. Is it possible to solve it in one pass? Perhaps usin= g some low-level function or join or some index expression or auxiliary colu= mns? >=20 > It is OK to create an index or similar using recursive expressions. Howeve= r, the SELECT expressions should be solved in one pass because of speed. >=20 >=20 > Pablo Are you asking for just the function (always with a seed Id) or the complete= transformation in a single select? Would you like the descendants on one li= ne (eg n22,[n221,n222])? I wonder if you might add n3 -> null to explicitly terminate the hierarchy ?= --Apple-Mail-776A7564-41FF-4BF8-83E3-EE0CC1AAFD89 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable


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

Hello,

I have a huge table (100 m= illion rows) of relations between nodes by id in a Postgresql 11 server. Lik= e this:

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

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

<= u>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 (dir= ect or indirect) of any other node.

Example. Th= e 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 indirec= t children).

this pseudo SQL: 

<= /div>
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 creat= e an index or similar using recursive expressions. However, the SELECT expre= ssions should be solved in one pass because of speed.


Pablo

Are you asking for just the function (always wit= h a seed Id) or the complete transformation in a single select? Would you li= ke the descendants on one line (eg n22,[n221,n222])?
I wonder if y= ou might add  n3 -> null to explicitly terminate the hierarchy ?= --Apple-Mail-776A7564-41FF-4BF8-83E3-EE0CC1AAFD89--