Re: WITH RECURSIVE doesn't work properly for me

From: Jing Fan <fanjing09(at)gmail(dot)com>
To: Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: WITH RECURSIVE doesn't work properly for me
Date: 2013-11-06 14:23:05
Message-ID: CA+BectmttF-GB99RR_96OYOCyhTanB9VmEMuk1aAKnw2vfbALw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

But after this iteration, the paths will be:
A B 1
B C 1
C B 1
A C 2
A B 3

in next iteration, the recursive statement will generate (A,C,2), (A,B,3),
and (A,C,4), after the group by, it will still be (A,C,2) and (A,B,3)
so I think it should stop after this iteration.

On Wed, Nov 6, 2013 at 8:10 AM, Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at>wrote:

> Jing Fan wrote:
> > I am sorry but I still don't understand why it doesn't work. Possibly I
> misunderstand how with
> > recursive works?
> > In my opinion,
> > with recursive table as{
> > seed statement
> > union
> > recursive statement
> > }
> > In every iteration, It will just generate results from seed statement
> union recursive statement and
> > put them into a new temporary table, and then compare the results with
> the former temporary table and
> > check if there are any new tuples. If no new tuples, just stop
> iteration. Is there any tricky things
> > about recursive statement?
>
> That is correct.
>
> Let's assume that we have three nodes A, B and C.
> Also, A points to B, B points to C and C points to B.
>
> Let's assume that we already generated (A, B, 1) and (A, C, 2)
> in previous iterations.
>
> Then the "recursive statement" will generate the new
> rows (A, C, 2) and (A, B, 3).
> The SELECT ... GROUP BY only surrounds the recursive statement,
> So the result will still be (A, C, 2) and (A, B, 3).
>
> Then the UNION will take care of the first triple, but the second
> one will be added in this iteration.
>
> And so on ad infinitum.
>
> Yours,
> Laurenz Albe
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Albe Laurenz 2013-11-06 14:40:23 Re: WITH RECURSIVE doesn't work properly for me
Previous Message Albe Laurenz 2013-11-06 14:10:28 Re: WITH RECURSIVE doesn't work properly for me