Re: Problem with Numerics multiplication in C-function - Mailing list pgsql-general

From Martijn van Oosterhout
Subject Re: Problem with Numerics multiplication in C-function
Date
Msg-id 20090803064238.GB1487@svana.org
Whole thread Raw
In response to Problem with Numerics multiplication in C-function  (Ilya Urikh <ilya.urikh@gmail.com>)
Responses Re: Problem with Numerics multiplication in C-function  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
On Mon, Aug 03, 2009 at 01:45:53PM +1100, Ilya Urikh wrote:
> Hi, I have a strange problem with Numeric multiplication in C-function.
>
> There are 2 functions getRate and getVAT which return Numeric. In 3rd
> function calculateService I try to multiply the results of getRate and
> getVAT. After execution I have two types of error, some time without
> messages and with message "Invalid memory alloc ... ".
> If I initialize the Numeric variables inside calculateService and multiply,
> function numeric_mul works fine.

Not entirly sure, but I think it's because SPI_finish destroys the SPI
memory context, including the data in it, which is the data you are
trying to return. See this page in the documentation:

http://www.postgresql.org/docs/8.3/interactive/spi-memory.html

What you need to do is copy the Datum you wish to return into the upper
memory context. I'm not seeing a helpful SPI utility function or
obvious example here you help you, but I'm sure it's something with
MemoryContextSwitchTo() and datumCopy().

Hope this helps,
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> Please line up in a tree and maintain the heap invariant while
> boarding. Thank you for flying nlogn airlines.

Attachment

pgsql-general by date:

Previous
From: sweta@opspl.com
Date:
Subject: Re: Drop Cluster
Next
From: "Chris Black"
Date:
Subject: Out of Office AutoReply: Never thought he would be able