Re: Why MemoryContextSwitch in ExecRelCheck ? - Mailing list pgsql-hackers

From Tom Lane
Subject Re: Why MemoryContextSwitch in ExecRelCheck ?
Date
Msg-id 9605.1010424492@sss.pgh.pa.us
Whole thread Raw
In response to Why MemoryContextSwitch in ExecRelCheck ?  (Holger Krug <hkrug@rationalizer.com>)
Responses Re: Why MemoryContextSwitch in ExecRelCheck ?  (Holger Krug <hkrug@rationalizer.com>)
List pgsql-hackers
Holger Krug <hkrug@rationalizer.com> writes:
> Nevertheless in ExecRelCheck a context switch to per-query memory
> context is made:
> Is this a switch from per-query memory context to per-query memory
> context, hence not necessary, or do I miss something ?

[ thinks ... ]  It might be unnecessary.  I'm not convinced that the
per-query context would always be the active one when ExecRelCheck is
called, however.  There are various per-tuple contexts that might be
used as well.

MemoryContextSwitchTo() is cheap enough that I prefer to call it when
there's any doubt, rather than build a routine that will fail silently
if it's called in the wrong context.  There are two typical scenarios
for routines that are building data structures that will outlive the
routine's execution:

1. Data structure is to be returned to the caller.  In this case the
caller is responsible for identifying the context to allocate the data
structure in, either explicitly or by passing it as the current context.

2. Data structure is owned and managed by the routine, which must know
which context it's supposed to live in.  In these cases I think the
routine ought always to explicitly switch to that context, not assume
that it's being called in that context.

I've been trying to migrate away from running with CurrentMemoryContext
set to anything longer-lived than a per-tuple context, though the
project is by no means complete.
        regards, tom lane


pgsql-hackers by date:

Previous
From: Holger Krug
Date:
Subject: Why MemoryContextSwitch in ExecRelCheck ?
Next
From: Tom Lane
Date:
Subject: Re: ecpg compile error on AIX