| From: | "David O'Meara" <nilpunning(at)gmail(dot)com> |
|---|---|
| To: | pgsql-www(at)postgresql(dot)org |
| Subject: | Wiki editor request |
| Date: | 2026-04-17 20:30:23 |
| Message-ID: | CAOpL5sOuWYuEFH-N7YWrQazX1gi35cRxroydc1YvtrYFDWS-sw@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-www |
Hello,
I would like editor access to the wiki, my username is nilpunning.
I've found a mistake on
https://wiki.postgresql.org/wiki/Getting_list_of_all_children_from_adjacency_tree
and I would like to fix. Under the header PostgreSQL 8.4+ the first
example looks like this:
WITH RECURSIVE tree AS ( SELECT id, ARRAY[]::integer[] AS ancestors
FROM test WHERE parent_id IS NULL
UNION ALL
SELECT test.id, tree.ancestors || test.parent_id FROM test, tree
WHERE test.parent_id = tree.id) SELECT * FROM tree WHERE 0 =
ANY(tree.ancestors);
In particular consider this clause: WHERE 0 = ANY(tree.ancestors)
The result ancestors don't include 0:
id | ancestors
------------------------
3 | {}
5 | {3}
6 | {3}
9 | {3,5}
(4 rows)
Since this this is the first example in this section I would just remove
the where clause:
WITH RECURSIVE tree AS ( SELECT id, ARRAY[]::integer[] AS ancestors
FROM test WHERE parent_id IS NULL
UNION ALL
SELECT test.id, tree.ancestors || test.parent_id FROM test, tree
WHERE test.parent_id = tree.id) SELECT * FROM tree;
This would match the results.
Thanks,
David
root(at)9c93dad27895:/# psql -U dev -d testdb
psql (12.4 (Debian 12.4-1.pgdg100+1))
Type "help" for help.
testdb=# CREATE TABLE test (
testdb(# id SERIAL PRIMARY KEY,
testdb(# parent_id INTEGER REFERENCES test(id),
testdb(# x TEXT
testdb(# );
testdb=# insert into test(id, parent_id) VALUES (3, NULL), (5,3), (6,3), (9,5);
INSERT 0 4
testdb=# select * from test;
id | parent_id | x
----+-----------+---
3 | |
5 | 3 |
6 | 3 |
9 | 5 |
(4 rows)
testdb=# WITH RECURSIVE tree AS (
testdb(# SELECT id, ARRAY[]::integer[] AS ancestors
testdb(# FROM test WHERE parent_id IS NULL
testdb(#
testdb(# UNION ALL
testdb(#
testdb(# SELECT test.id, tree.ancestors || test.parent_id
testdb(# FROM test, tree
testdb(# WHERE test.parent_id = tree.id
testdb(# ) SELECT * FROM tree WHERE 0 = ANY(tree.ancestors);
id | ancestors
----+-----------
(0 rows)
testdb=# WITH RECURSIVE tree AS (
SELECT id, ARRAY[]::integer[] AS ancestors
FROM test WHERE parent_id IS NULL
UNION ALL
SELECT test.id, tree.ancestors || test.parent_id
FROM test, tree
WHERE test.parent_id = tree.id
) SELECT * FROM tree;
id | ancestors
----+-----------
3 | {}
5 | {3}
6 | {3}
9 | {3,5}
(4 rows)
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Joe Conway | 2026-04-18 16:45:27 | Re: Wiki editor request |
| Previous Message | Jesús Espino | 2026-04-16 13:09:57 | Re: Request to add "Deep Dive Into a SQL Query" to the PostgreSQL Books page |