Re: recursive function needs static variable

From: Joe Conway <mail(at)joeconway(dot)com>
To: Nabil Sayegh <postgresql(at)e-trolley(dot)de>
Cc: pgsql-novice <pgsql-novice(at)postgresql(dot)org>
Subject: Re: recursive function needs static variable
Date: 2003-06-25 02:34:19
Message-ID: 3EF90A2B.4070606@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

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

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Joe Conway 2003-06-25 02:37:44 Re: connectby() minor bug in errormessage
Previous Message Nabil Sayegh 2003-06-25 01:55:08 recursive function needs static variable