Re: Postgres 8.3 - C function taking and returning arrays - Mailing list pgsql-novice

From Tom Lane
Subject Re: Postgres 8.3 - C function taking and returning arrays
Date
Msg-id 22138.1204260942@sss.pgh.pa.us
Whole thread Raw
In response to Postgres 8.3 - C function taking and returning arrays  ("s anwar" <sanwar@gmail.com>)
List pgsql-novice
"s anwar" <sanwar@gmail.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
reset.

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

pgsql-novice by date:

Previous
From: Tom Lane
Date:
Subject: Re: pidof not working for postgresql-8.3 ?
Next
From: messias
Date:
Subject: Re: Re: Monitoring new records