PostgreSQL - retrieve all IDs in a tree for a given subnode
I have a non-binary tree of customer, and I need to obtain all the IDs in a tree for the given node.
The table is very simple, just an join table with a parent id and child id.
This is a representation of the tree I stored in my db.
In this example if I search for node 17 I need in return 14-17. If I search for 11 I need in return 1-6-5-4-8-11-12-7-2-10-3.
The order is not important. I only need the ID to avoid circularity when adding children to a node.
I created this query.
The ancestor part works fine, I retrieve all parent nodes, but for the descendants I have some trouble. I'm only able to retrieve some part of the tree.
For example, with node 11 I retrieve 4-10-6-11-7-8, so all right part of the tree is missing.
WITH RECURSIVE
-- starting node(s)
starting (parent, child) AS
(
SELECT t.parent, t.child
FROM public.customerincustomer AS t
WHERE t.child = :node or t.parent = :node
)
,
ancestors (parent, child) AS
(
SELECT t.parent, t.child
FROM public.customerincustomer AS t
WHERE t.parent IN (SELECT parent FROM starting)
UNION ALL
SELECT t.parent, t.child
FROM public.customerincustomer AS t JOIN ancestors AS a ON t.child = a.parent
),
descendants (parent, child) AS
(
SELECT t.parent, t.child
FROM public.customerincustomer AS t
WHERE t.parent IN (SELECT parent FROM starting) or t.child in (select child from starting)
UNION ALL
SELECT t.parent, t.child
FROM public.customerincustomer AS t JOIN ancestors AS a ON t.parent = a.child
)
table ancestors
union all
table descendants
UPDATE
I see that many examples included in the tree table also the root in form (root_id, null).
In my case I don't have this record.
For example, taking the smallest tree 14->17, in my table I have only one record
parent, child
14 17
postgresql recursive tree
add a comment |
I have a non-binary tree of customer, and I need to obtain all the IDs in a tree for the given node.
The table is very simple, just an join table with a parent id and child id.
This is a representation of the tree I stored in my db.
In this example if I search for node 17 I need in return 14-17. If I search for 11 I need in return 1-6-5-4-8-11-12-7-2-10-3.
The order is not important. I only need the ID to avoid circularity when adding children to a node.
I created this query.
The ancestor part works fine, I retrieve all parent nodes, but for the descendants I have some trouble. I'm only able to retrieve some part of the tree.
For example, with node 11 I retrieve 4-10-6-11-7-8, so all right part of the tree is missing.
WITH RECURSIVE
-- starting node(s)
starting (parent, child) AS
(
SELECT t.parent, t.child
FROM public.customerincustomer AS t
WHERE t.child = :node or t.parent = :node
)
,
ancestors (parent, child) AS
(
SELECT t.parent, t.child
FROM public.customerincustomer AS t
WHERE t.parent IN (SELECT parent FROM starting)
UNION ALL
SELECT t.parent, t.child
FROM public.customerincustomer AS t JOIN ancestors AS a ON t.child = a.parent
),
descendants (parent, child) AS
(
SELECT t.parent, t.child
FROM public.customerincustomer AS t
WHERE t.parent IN (SELECT parent FROM starting) or t.child in (select child from starting)
UNION ALL
SELECT t.parent, t.child
FROM public.customerincustomer AS t JOIN ancestors AS a ON t.parent = a.child
)
table ancestors
union all
table descendants
UPDATE
I see that many examples included in the tree table also the root in form (root_id, null).
In my case I don't have this record.
For example, taking the smallest tree 14->17, in my table I have only one record
parent, child
14 17
postgresql recursive tree
Depending on your workload and use case you may also want to considerltree
– Evan Carroll
Dec 13 '18 at 15:05
add a comment |
I have a non-binary tree of customer, and I need to obtain all the IDs in a tree for the given node.
The table is very simple, just an join table with a parent id and child id.
This is a representation of the tree I stored in my db.
In this example if I search for node 17 I need in return 14-17. If I search for 11 I need in return 1-6-5-4-8-11-12-7-2-10-3.
The order is not important. I only need the ID to avoid circularity when adding children to a node.
I created this query.
The ancestor part works fine, I retrieve all parent nodes, but for the descendants I have some trouble. I'm only able to retrieve some part of the tree.
For example, with node 11 I retrieve 4-10-6-11-7-8, so all right part of the tree is missing.
WITH RECURSIVE
-- starting node(s)
starting (parent, child) AS
(
SELECT t.parent, t.child
FROM public.customerincustomer AS t
WHERE t.child = :node or t.parent = :node
)
,
ancestors (parent, child) AS
(
SELECT t.parent, t.child
FROM public.customerincustomer AS t
WHERE t.parent IN (SELECT parent FROM starting)
UNION ALL
SELECT t.parent, t.child
FROM public.customerincustomer AS t JOIN ancestors AS a ON t.child = a.parent
),
descendants (parent, child) AS
(
SELECT t.parent, t.child
FROM public.customerincustomer AS t
WHERE t.parent IN (SELECT parent FROM starting) or t.child in (select child from starting)
UNION ALL
SELECT t.parent, t.child
FROM public.customerincustomer AS t JOIN ancestors AS a ON t.parent = a.child
)
table ancestors
union all
table descendants
UPDATE
I see that many examples included in the tree table also the root in form (root_id, null).
In my case I don't have this record.
For example, taking the smallest tree 14->17, in my table I have only one record
parent, child
14 17
postgresql recursive tree
I have a non-binary tree of customer, and I need to obtain all the IDs in a tree for the given node.
The table is very simple, just an join table with a parent id and child id.
This is a representation of the tree I stored in my db.
In this example if I search for node 17 I need in return 14-17. If I search for 11 I need in return 1-6-5-4-8-11-12-7-2-10-3.
The order is not important. I only need the ID to avoid circularity when adding children to a node.
I created this query.
The ancestor part works fine, I retrieve all parent nodes, but for the descendants I have some trouble. I'm only able to retrieve some part of the tree.
For example, with node 11 I retrieve 4-10-6-11-7-8, so all right part of the tree is missing.
WITH RECURSIVE
-- starting node(s)
starting (parent, child) AS
(
SELECT t.parent, t.child
FROM public.customerincustomer AS t
WHERE t.child = :node or t.parent = :node
)
,
ancestors (parent, child) AS
(
SELECT t.parent, t.child
FROM public.customerincustomer AS t
WHERE t.parent IN (SELECT parent FROM starting)
UNION ALL
SELECT t.parent, t.child
FROM public.customerincustomer AS t JOIN ancestors AS a ON t.child = a.parent
),
descendants (parent, child) AS
(
SELECT t.parent, t.child
FROM public.customerincustomer AS t
WHERE t.parent IN (SELECT parent FROM starting) or t.child in (select child from starting)
UNION ALL
SELECT t.parent, t.child
FROM public.customerincustomer AS t JOIN ancestors AS a ON t.parent = a.child
)
table ancestors
union all
table descendants
UPDATE
I see that many examples included in the tree table also the root in form (root_id, null).
In my case I don't have this record.
For example, taking the smallest tree 14->17, in my table I have only one record
parent, child
14 17
postgresql recursive tree
postgresql recursive tree
edited Dec 30 '18 at 1:53
Peter Mortensen
23119
23119
asked Dec 13 '18 at 11:12
Luca Nitti
283
283
Depending on your workload and use case you may also want to considerltree
– Evan Carroll
Dec 13 '18 at 15:05
add a comment |
Depending on your workload and use case you may also want to considerltree
– Evan Carroll
Dec 13 '18 at 15:05
Depending on your workload and use case you may also want to consider
ltree
– Evan Carroll
Dec 13 '18 at 15:05
Depending on your workload and use case you may also want to consider
ltree
– Evan Carroll
Dec 13 '18 at 15:05
add a comment |
2 Answers
2
active
oldest
votes
A very primitive implementation:
It basically divides the problem into two subproblems:
- First find all the ancestors of the node in question (including the node itself). If the node has no parents, then this would be just itself.
- Then find the descendants of all those ancestors (including themselves). We may have several nodes in the
ancestors
result set, we may get duplicates here, so we useUNION
(and notUNION ALL
) to remove them. - Note that the query will work even if the input node is a root with has no children.
- It will also work if the data set is not a forest of trees but an arbitrary directed graph (where nodes can have more than one parent).
The query:
WITH RECURSIVE
ancestors (parent) AS
(
SELECT :node -- start with the given node
UNION ALL
SELECT t.parent -- and find all its ancestors
FROM public.customerincustomer AS t JOIN ancestors AS a ON t.child = a.parent
),
descendants (customer) AS
(
SELECT parent AS customer -- now start with all the ancestors
FROM ancestors
UNION
SELECT t.child -- and find all their descendants
FROM public.customerincustomer AS t JOIN descendants AS d ON t.parent = d.customer
)
SELECT customer
FROM descendants ;
Thanks! this is exactly what i need
– Luca Nitti
Dec 13 '18 at 14:37
add a comment |
This function returns the parent level of node_id
:
There is a 'level' row due there isn't a row (id, null) for parent row.
CREATE FUNCTION get_parent(node_id int)
RETURNS integer AS
$$
WITH RECURSIVE get_parent AS
(
SELECT
t1.id,
t1.parent_id,
t1.name,
0 AS level
FROM
tree t1
WHERE
t1.id = node_id
UNION ALL
SELECT
t2.id,
t2.parent_id,
t2.name,
level+1
FROM
tree t2
INNER JOIN
get_parent ON get_parent.parent_id = t2.id
)
SELECT
id
FROM
get_parent
ORDER BY
level DESC
LIMIT 1 ;
$$
LANGUAGE SQL;
select get_parent(7);
| get_parent |
| ---------: |
| 6 |
Now, next query returns the whole tree structure based on a parent node.
WITH RECURSIVE childs AS
(
SELECT
t1.id,
t1.parent_id,
t1.name
FROM
tree t1
WHERE
t1.id = get_parent(7)
UNION ALL
SELECT
t2.id,
t2.parent_id,
t2.name
FROM
tree t2
INNER JOIN
childs ON childs.id = t2.parent_id
)
SELECT
id,
parent_id,
name
FROM
childs;
id | parent_id | name
-: | --------: | :------
6 | 1 | Node 6
4 | 6 | Node 4
8 | 6 | Node 8
11 | 6 | Node 11
7 | 11 | Node 7
10 | 7 | Node 10
db<>fiddle here
as i said, i don't have problem with ancestors. I need the whole tree.
– Luca Nitti
Dec 13 '18 at 12:04
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f224871%2fpostgresql-retrieve-all-ids-in-a-tree-for-a-given-subnode%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
A very primitive implementation:
It basically divides the problem into two subproblems:
- First find all the ancestors of the node in question (including the node itself). If the node has no parents, then this would be just itself.
- Then find the descendants of all those ancestors (including themselves). We may have several nodes in the
ancestors
result set, we may get duplicates here, so we useUNION
(and notUNION ALL
) to remove them. - Note that the query will work even if the input node is a root with has no children.
- It will also work if the data set is not a forest of trees but an arbitrary directed graph (where nodes can have more than one parent).
The query:
WITH RECURSIVE
ancestors (parent) AS
(
SELECT :node -- start with the given node
UNION ALL
SELECT t.parent -- and find all its ancestors
FROM public.customerincustomer AS t JOIN ancestors AS a ON t.child = a.parent
),
descendants (customer) AS
(
SELECT parent AS customer -- now start with all the ancestors
FROM ancestors
UNION
SELECT t.child -- and find all their descendants
FROM public.customerincustomer AS t JOIN descendants AS d ON t.parent = d.customer
)
SELECT customer
FROM descendants ;
Thanks! this is exactly what i need
– Luca Nitti
Dec 13 '18 at 14:37
add a comment |
A very primitive implementation:
It basically divides the problem into two subproblems:
- First find all the ancestors of the node in question (including the node itself). If the node has no parents, then this would be just itself.
- Then find the descendants of all those ancestors (including themselves). We may have several nodes in the
ancestors
result set, we may get duplicates here, so we useUNION
(and notUNION ALL
) to remove them. - Note that the query will work even if the input node is a root with has no children.
- It will also work if the data set is not a forest of trees but an arbitrary directed graph (where nodes can have more than one parent).
The query:
WITH RECURSIVE
ancestors (parent) AS
(
SELECT :node -- start with the given node
UNION ALL
SELECT t.parent -- and find all its ancestors
FROM public.customerincustomer AS t JOIN ancestors AS a ON t.child = a.parent
),
descendants (customer) AS
(
SELECT parent AS customer -- now start with all the ancestors
FROM ancestors
UNION
SELECT t.child -- and find all their descendants
FROM public.customerincustomer AS t JOIN descendants AS d ON t.parent = d.customer
)
SELECT customer
FROM descendants ;
Thanks! this is exactly what i need
– Luca Nitti
Dec 13 '18 at 14:37
add a comment |
A very primitive implementation:
It basically divides the problem into two subproblems:
- First find all the ancestors of the node in question (including the node itself). If the node has no parents, then this would be just itself.
- Then find the descendants of all those ancestors (including themselves). We may have several nodes in the
ancestors
result set, we may get duplicates here, so we useUNION
(and notUNION ALL
) to remove them. - Note that the query will work even if the input node is a root with has no children.
- It will also work if the data set is not a forest of trees but an arbitrary directed graph (where nodes can have more than one parent).
The query:
WITH RECURSIVE
ancestors (parent) AS
(
SELECT :node -- start with the given node
UNION ALL
SELECT t.parent -- and find all its ancestors
FROM public.customerincustomer AS t JOIN ancestors AS a ON t.child = a.parent
),
descendants (customer) AS
(
SELECT parent AS customer -- now start with all the ancestors
FROM ancestors
UNION
SELECT t.child -- and find all their descendants
FROM public.customerincustomer AS t JOIN descendants AS d ON t.parent = d.customer
)
SELECT customer
FROM descendants ;
A very primitive implementation:
It basically divides the problem into two subproblems:
- First find all the ancestors of the node in question (including the node itself). If the node has no parents, then this would be just itself.
- Then find the descendants of all those ancestors (including themselves). We may have several nodes in the
ancestors
result set, we may get duplicates here, so we useUNION
(and notUNION ALL
) to remove them. - Note that the query will work even if the input node is a root with has no children.
- It will also work if the data set is not a forest of trees but an arbitrary directed graph (where nodes can have more than one parent).
The query:
WITH RECURSIVE
ancestors (parent) AS
(
SELECT :node -- start with the given node
UNION ALL
SELECT t.parent -- and find all its ancestors
FROM public.customerincustomer AS t JOIN ancestors AS a ON t.child = a.parent
),
descendants (customer) AS
(
SELECT parent AS customer -- now start with all the ancestors
FROM ancestors
UNION
SELECT t.child -- and find all their descendants
FROM public.customerincustomer AS t JOIN descendants AS d ON t.parent = d.customer
)
SELECT customer
FROM descendants ;
edited Dec 13 '18 at 15:00
answered Dec 13 '18 at 14:11
yper-crazyhat-cubeᵀᴹ
74.5k11126206
74.5k11126206
Thanks! this is exactly what i need
– Luca Nitti
Dec 13 '18 at 14:37
add a comment |
Thanks! this is exactly what i need
– Luca Nitti
Dec 13 '18 at 14:37
Thanks! this is exactly what i need
– Luca Nitti
Dec 13 '18 at 14:37
Thanks! this is exactly what i need
– Luca Nitti
Dec 13 '18 at 14:37
add a comment |
This function returns the parent level of node_id
:
There is a 'level' row due there isn't a row (id, null) for parent row.
CREATE FUNCTION get_parent(node_id int)
RETURNS integer AS
$$
WITH RECURSIVE get_parent AS
(
SELECT
t1.id,
t1.parent_id,
t1.name,
0 AS level
FROM
tree t1
WHERE
t1.id = node_id
UNION ALL
SELECT
t2.id,
t2.parent_id,
t2.name,
level+1
FROM
tree t2
INNER JOIN
get_parent ON get_parent.parent_id = t2.id
)
SELECT
id
FROM
get_parent
ORDER BY
level DESC
LIMIT 1 ;
$$
LANGUAGE SQL;
select get_parent(7);
| get_parent |
| ---------: |
| 6 |
Now, next query returns the whole tree structure based on a parent node.
WITH RECURSIVE childs AS
(
SELECT
t1.id,
t1.parent_id,
t1.name
FROM
tree t1
WHERE
t1.id = get_parent(7)
UNION ALL
SELECT
t2.id,
t2.parent_id,
t2.name
FROM
tree t2
INNER JOIN
childs ON childs.id = t2.parent_id
)
SELECT
id,
parent_id,
name
FROM
childs;
id | parent_id | name
-: | --------: | :------
6 | 1 | Node 6
4 | 6 | Node 4
8 | 6 | Node 8
11 | 6 | Node 11
7 | 11 | Node 7
10 | 7 | Node 10
db<>fiddle here
as i said, i don't have problem with ancestors. I need the whole tree.
– Luca Nitti
Dec 13 '18 at 12:04
add a comment |
This function returns the parent level of node_id
:
There is a 'level' row due there isn't a row (id, null) for parent row.
CREATE FUNCTION get_parent(node_id int)
RETURNS integer AS
$$
WITH RECURSIVE get_parent AS
(
SELECT
t1.id,
t1.parent_id,
t1.name,
0 AS level
FROM
tree t1
WHERE
t1.id = node_id
UNION ALL
SELECT
t2.id,
t2.parent_id,
t2.name,
level+1
FROM
tree t2
INNER JOIN
get_parent ON get_parent.parent_id = t2.id
)
SELECT
id
FROM
get_parent
ORDER BY
level DESC
LIMIT 1 ;
$$
LANGUAGE SQL;
select get_parent(7);
| get_parent |
| ---------: |
| 6 |
Now, next query returns the whole tree structure based on a parent node.
WITH RECURSIVE childs AS
(
SELECT
t1.id,
t1.parent_id,
t1.name
FROM
tree t1
WHERE
t1.id = get_parent(7)
UNION ALL
SELECT
t2.id,
t2.parent_id,
t2.name
FROM
tree t2
INNER JOIN
childs ON childs.id = t2.parent_id
)
SELECT
id,
parent_id,
name
FROM
childs;
id | parent_id | name
-: | --------: | :------
6 | 1 | Node 6
4 | 6 | Node 4
8 | 6 | Node 8
11 | 6 | Node 11
7 | 11 | Node 7
10 | 7 | Node 10
db<>fiddle here
as i said, i don't have problem with ancestors. I need the whole tree.
– Luca Nitti
Dec 13 '18 at 12:04
add a comment |
This function returns the parent level of node_id
:
There is a 'level' row due there isn't a row (id, null) for parent row.
CREATE FUNCTION get_parent(node_id int)
RETURNS integer AS
$$
WITH RECURSIVE get_parent AS
(
SELECT
t1.id,
t1.parent_id,
t1.name,
0 AS level
FROM
tree t1
WHERE
t1.id = node_id
UNION ALL
SELECT
t2.id,
t2.parent_id,
t2.name,
level+1
FROM
tree t2
INNER JOIN
get_parent ON get_parent.parent_id = t2.id
)
SELECT
id
FROM
get_parent
ORDER BY
level DESC
LIMIT 1 ;
$$
LANGUAGE SQL;
select get_parent(7);
| get_parent |
| ---------: |
| 6 |
Now, next query returns the whole tree structure based on a parent node.
WITH RECURSIVE childs AS
(
SELECT
t1.id,
t1.parent_id,
t1.name
FROM
tree t1
WHERE
t1.id = get_parent(7)
UNION ALL
SELECT
t2.id,
t2.parent_id,
t2.name
FROM
tree t2
INNER JOIN
childs ON childs.id = t2.parent_id
)
SELECT
id,
parent_id,
name
FROM
childs;
id | parent_id | name
-: | --------: | :------
6 | 1 | Node 6
4 | 6 | Node 4
8 | 6 | Node 8
11 | 6 | Node 11
7 | 11 | Node 7
10 | 7 | Node 10
db<>fiddle here
This function returns the parent level of node_id
:
There is a 'level' row due there isn't a row (id, null) for parent row.
CREATE FUNCTION get_parent(node_id int)
RETURNS integer AS
$$
WITH RECURSIVE get_parent AS
(
SELECT
t1.id,
t1.parent_id,
t1.name,
0 AS level
FROM
tree t1
WHERE
t1.id = node_id
UNION ALL
SELECT
t2.id,
t2.parent_id,
t2.name,
level+1
FROM
tree t2
INNER JOIN
get_parent ON get_parent.parent_id = t2.id
)
SELECT
id
FROM
get_parent
ORDER BY
level DESC
LIMIT 1 ;
$$
LANGUAGE SQL;
select get_parent(7);
| get_parent |
| ---------: |
| 6 |
Now, next query returns the whole tree structure based on a parent node.
WITH RECURSIVE childs AS
(
SELECT
t1.id,
t1.parent_id,
t1.name
FROM
tree t1
WHERE
t1.id = get_parent(7)
UNION ALL
SELECT
t2.id,
t2.parent_id,
t2.name
FROM
tree t2
INNER JOIN
childs ON childs.id = t2.parent_id
)
SELECT
id,
parent_id,
name
FROM
childs;
id | parent_id | name
-: | --------: | :------
6 | 1 | Node 6
4 | 6 | Node 4
8 | 6 | Node 8
11 | 6 | Node 11
7 | 11 | Node 7
10 | 7 | Node 10
db<>fiddle here
edited Dec 13 '18 at 16:14
answered Dec 13 '18 at 11:39
McNets
14.8k41857
14.8k41857
as i said, i don't have problem with ancestors. I need the whole tree.
– Luca Nitti
Dec 13 '18 at 12:04
add a comment |
as i said, i don't have problem with ancestors. I need the whole tree.
– Luca Nitti
Dec 13 '18 at 12:04
as i said, i don't have problem with ancestors. I need the whole tree.
– Luca Nitti
Dec 13 '18 at 12:04
as i said, i don't have problem with ancestors. I need the whole tree.
– Luca Nitti
Dec 13 '18 at 12:04
add a comment |
Thanks for contributing an answer to Database Administrators Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f224871%2fpostgresql-retrieve-all-ids-in-a-tree-for-a-given-subnode%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Depending on your workload and use case you may also want to consider
ltree
– Evan Carroll
Dec 13 '18 at 15:05