Re: Foreign memory context read

From: Heikki Linnakangas <heikki(dot)linnakangas(at)enterprisedb(dot)com>
To: Vaibhav Kaushal <vaibhavkaushal123(at)gmail(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Foreign memory context read
Date: 2011-05-23 11:23:33
Message-ID: 4DDA43B5.9000403@enterprisedb.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 23.05.2011 13:44, Vaibhav Kaushal wrote:
> Hello,
>
> I made some code changes, compilation went fine but the database could not
> start with the message:
>
> LOG: server process (PID 17684) was terminated by signal 11: Segmentation
> fault
>
> I think this is because of memory allocation outside of any memory context.

There's always a memory context active, it just might not be the correct
one.

> Is it possible to create some variable in a memory context (say
> "cut_context") and then access the variable in that context from a piece of
> code which is working with variables in a different context (say the
> "per_query" context)?
>
> If yes, then how?

Sure, for accessing a variable, it doesn't matter which memory context
it was allocated in. As long as you make sure you allocate things in
sufficiently long-lived memory contexts, so that your allocations are
not free'd too early, while they're still needed by some code.

> My first guess is a Memory Context switch. But then, I need to bring in the
> value of the variable from the cut_context (which was formed earlier) to the
> per_query context which was created later on.
>
> Precisely I am trying to create a small array of Datums (before the ExecQual
> is called inside ExecScan) and then use the array inside the ExecEvalVar
> (which obviously is inside the executer).

Switching to the right memory context before the palloc() call is the
key. Sounds like you want to allocate your array in the per-query memory
context. If you need to move a value from one memory context to another,
like if you need to take a Datum that's already been allocated in some
other memory context, and store it in that array, you need to copy the
Datum to the right memory context.

--
Heikki Linnakangas
EnterpriseDB http://www.enterprisedb.com

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Vaibhav Kaushal 2011-05-23 11:33:52 Re: Foreign memory context read
Previous Message Vaibhav Kaushal 2011-05-23 10:44:27 Foreign memory context read