Re: Documentation example for RECURISVE WITH isn't what I would expect - Mailing list pgsql-docs

From Tom Lane
Subject Re: Documentation example for RECURISVE WITH isn't what I would expect
Date
Msg-id 13809.1322327412@sss.pgh.pa.us
Whole thread Raw
In response to Documentation example for RECURISVE WITH isn't what I would expect  (Alex Sherwin <alex.sherwin@gmail.com>)
List pgsql-docs
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

pgsql-docs by date:

Previous
From: Alex Sherwin
Date:
Subject: Documentation example for RECURISVE WITH isn't what I would expect
Next
From: Tom Lane
Date:
Subject: Re: Wrong advisory locks docs in pg_locks