Thread: recursive function needs static variable

recursive function needs static variable

From
Nabil Sayegh
Date:
Hi all,

I just changed connectby() to order siblings and return an additional
serial column.

Everything is working fine so far, but I think there could be a problem
due to the static definition of the serial variable.

I need the variable to be static, because the function itself is
recursive.

What will happen if 2 processes concurrently use the function ? will
they conflict ?

I guess, that if I set the variable to 1 at each beginning of the
function, it will conflict.

If I do _not_ reset the serial at each function call, that would lead to
an overflow sooner or later.

Any idea ?

--
 e-Trolley Sayegh & John, Nabil Sayegh
 Tel.: 0700 etrolley /// 0700 38765539
 Fax.: +49 69 8299381-8
 PGP : http://www.e-trolley.de


Re: recursive function needs static variable

From
Joe Conway
Date:
Nabil Sayegh wrote:
> I just changed connectby() to order siblings and return an additional
> serial column.
>
> Everything is working fine so far, but I think there could be a problem
> due to the static definition of the serial variable.
>
> I need the variable to be static, because the function itself is
> recursive.
>
> What will happen if 2 processes concurrently use the function ? will
> they conflict ?

Each backend connection is a single threaded process, so there should be
no problem. But I don't think it needs to be static anyway (see below).

> I guess, that if I set the variable to 1 at each beginning of the
> function, it will conflict.
>
> If I do _not_ reset the serial at each function call, that would lead to
> an overflow sooner or later.

Add the counter variable to build_tuplestore_recursively()'s arguments.
Initialize it in connectby(), and increment it every time
BuildTupleFromCStrings() is called.

Joe


Re: recursive function needs static variable

From
Nabil Sayegh
Date:
Am Mit, 2003-06-25 um 04.34 schrieb Joe Conway:
> Nabil Sayegh wrote:

> Each backend connection is a single threaded process, so there should be
> no problem. But I don't think it needs to be static anyway (see below).

Is this also true for several function calls in 1 query ?

> > I guess, that if I set the variable to 1 at each beginning of the
> > function, it will conflict.

So, this is okay ?

> Add the counter variable to build_tuplestore_recursively()'s arguments.
> Initialize it in connectby(), and increment it every time
> BuildTupleFromCStrings() is called.

Already tried that, but due to the recursion the incrementation steps
get undone each time the build_tuplestore_recursively returns :).
=> The serial is nomore UNIQUE

tfyh
--
 e-Trolley Sayegh & John, Nabil Sayegh
 Tel.: 0700 etrolley /// 0700 38765539
 Fax.: +49 69 8299381-8
 PGP : http://www.e-trolley.de


Re: recursive function needs static variable

From
Joe Conway
Date:
Nabil Sayegh wrote:
> Am Mit, 2003-06-25 um 04.34 schrieb Joe Conway:
>
>>Nabil Sayegh wrote:
>
>>Each backend connection is a single threaded process, so there should be
>>no problem. But I don't think it needs to be static anyway (see below).
>
> Is this also true for several function calls in 1 query ?

Yes. Each call to connectby_text() will run to completion and return the
results as a tuplestore.

>>>I guess, that if I set the variable to 1 at each beginning of the
>>>function, it will conflict.
>
> So, this is okay ?

Should be fine.

>
>>Add the counter variable to build_tuplestore_recursively()'s arguments.
>>Initialize it in connectby(), and increment it every time
>>BuildTupleFromCStrings() is called.
>
> Already tried that, but due to the recursion the incrementation steps
> get undone each time the build_tuplestore_recursively returns :).
> => The serial is nomore UNIQUE

Not if you pass it by reference.

Joe


Re: recursive function needs static variable

From
Nabil Sayegh
Date:
Am Mit, 2003-06-25 um 06.35 schrieb Joe Conway:
> >>Add the counter variable to build_tuplestore_recursively()'s arguments.
> >>Initialize it in connectby(), and increment it every time
> >>BuildTupleFromCStrings() is called.
> >
> > Already tried that, but due to the recursion the incrementation steps
> > get undone each time the build_tuplestore_recursively returns :).
> > => The serial is nomore UNIQUE
>
> Not if you pass it by reference.

Oh, my stupid :) Of course.

thx again
--
 e-Trolley Sayegh & John, Nabil Sayegh
 Tel.: 0700 etrolley /// 0700 38765539
 Fax.: +49 69 8299381-8
 PGP : http://www.e-trolley.de