"s anwar" <sanwar(at)gmail(dot)com> writes:
> I have written a PostgreSQL 8.3beta2 server side function named
> array_times_scalar (source included below). It works, but I haven't found
> sufficient examples to be certain that I am not leaking memory. I was
> wondering if someone can either point me to examples or take a look at the
> code below.
As a rule of thumb, SQL-callable C functions don't need to be
particularly concerned about "leaking" memory allocations made with
palloc(). Such functions are normally called with CurrentMemoryContext
set to a short-lived context that will be reset soon (usually at the
end of the processing cycle for the current tuple). So anything you
palloc will go away pretty soon anyway. There's even an argument to be
made that explicit pfree's are a bad idea, because they eat more cycles
than would be needed to let the memory be reclaimed by the next context
There are some exceptions, in particular functions that are used as
b-tree index support functions need to be more careful. But for the
most part I'd say don't worry unless you have direct evidence that you
have a problem.
> I am, however, getting incorrect values if I select a particular array index
> out of the result of this function.
You declared the function as "returns real" but the function code
seems to think that it can return some randomly chosen type different
from array of FLOAT4OID. I think this disconnect is the problem...
regards, tom lane
In response to
pgsql-novice by date
|Next:||From: messias||Date: 2008-02-29 07:06:41|
|Subject: Re: Re: Monitoring new records|
|Previous:||From: Tom Lane||Date: 2008-02-29 04:24:28|
|Subject: Re: pidof not working for postgresql-8.3 ? |