Skip site navigation (1) Skip section navigation (2)

Re: Postgres 8.3 - C function taking and returning arrays

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "s anwar" <sanwar(at)gmail(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Postgres 8.3 - C function taking and returning arrays
Date: 2008-02-29 04:55:42
Message-ID: 22138.1204260942@sss.pgh.pa.us (view raw or flat)
Thread:
Lists: pgsql-novice
"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
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

In response to

pgsql-novice by date

Next:From: messiasDate: 2008-02-29 07:06:41
Subject: Re: Re: Monitoring new records
Previous:From: Tom LaneDate: 2008-02-29 04:24:28
Subject: Re: pidof not working for postgresql-8.3 ?

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group