From: Tom Lane <tgl@sss.pgh.pa.us>
To: Alex Sherwin <alex.sherwin@gmail.com>
Cc: pgsql-docs@postgresql.org
Subject: Re: Documentation example for RECURISVE WITH isn't what I would expect
Date: Sat, 26 Nov 2011 12:10:12 -0500
Message-ID: <13809.1322327412@sss.pgh.pa.us> (raw)
In-Reply-To: <CAMqGX520K=3onSR45KQb=7iwk9V2Xt43V267K+Lx+XZcA=K0aw@mail.gmail.com>
References: <CAMqGX520K=3onSR45KQb=7iwk9V2Xt43V267K+Lx+XZcA=K0aw@mail.gmail.com>
Alex Sherwin <alex.sherwin@gmail.com> writes:
> The 9.1 docs for RECURSIVE WITH:
> http://www.postgresql.org/docs/9.1/static/queries-with.html eventually
> builds up to this example query:
> [ which is claimed not to work ]
No, the example works as intended for me. The reason "cycle" never gets
set to true in your test case is that your test data contains no loops.
Try something like this instead:
create table graph (id int, link int, data text);
insert into graph values (1, null, 'root');
insert into graph values (2, 1, 'top 1');
insert into graph values (3, 1, 'top 2');
insert into graph values (4, 2, 'child 1');
insert into graph values (5, 3, 'child 2');
insert into graph values (10, 11, 'loop 1');
insert into graph values (11, 10, 'loop 2');
The query as shown in the docs produces:
id | link | data | depth | path | cycle
----+------+---------+-------+------------+-------
1 | | root | 1 | {1} | f
2 | 1 | top 1 | 1 | {2} | f
3 | 1 | top 2 | 1 | {3} | f
4 | 2 | child 1 | 1 | {4} | f
5 | 3 | child 2 | 1 | {5} | f
10 | 11 | loop 1 | 1 | {10} | f
11 | 10 | loop 2 | 1 | {11} | f
1 | | root | 2 | {3,1} | f
1 | | root | 2 | {2,1} | f
2 | 1 | top 1 | 2 | {4,2} | f
3 | 1 | top 2 | 2 | {5,3} | f
10 | 11 | loop 1 | 2 | {11,10} | f
11 | 10 | loop 2 | 2 | {10,11} | f
1 | | root | 3 | {4,2,1} | f
1 | | root | 3 | {5,3,1} | f
10 | 11 | loop 1 | 3 | {10,11,10} | t
11 | 10 | loop 2 | 3 | {11,10,11} | t
(17 rows)
Your proposed query produces:
id | link | data | depth | path | cycle
----+------+---------+-------+------+-------
1 | | root | 1 | {1} | f
2 | 1 | top 1 | 1 | {2} | f
3 | 1 | top 2 | 1 | {3} | f
4 | 2 | child 1 | 1 | {4} | f
5 | 3 | child 2 | 1 | {5} | f
10 | 11 | loop 1 | 1 | {10} | f
11 | 10 | loop 2 | 1 | {11} | f
(7 rows)
which seems pretty broken to me, since it's not showing the paths at
all, much less providing any hint of where the loops are.
You could argue about the usefulness of the specific output from the
sample query --- in particular, it might make more sense to treat the
links as going in the other direction --- but the point here is to
discover all the paths that are traceable through the given set of
links.
regards, tom lane
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-docs@postgresql.org
Cc: tgl@sss.pgh.pa.us, alex.sherwin@gmail.com
Subject: Re: Documentation example for RECURISVE WITH isn't what I would expect
In-Reply-To: <13809.1322327412@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox