C language context

Started by Andrew Dunstanover 22 years ago2 messages
#1Andrew Dunstan
andrew@dunslane.net

Just how transient is the memory context created for a C language
function call?

The reason I ask is that I was getting a seg fault when I attempted to
pfree something that should have been palloced. When I commented out the
calls to pfree it worked fine. Most annoying ;-)

I'm still trying to get to the bottom of it, but I want to know if I'm
safe in relying on the context cleanup to handle things for me. It's an
immutable text function returning text, and is intended for use in a
functional index.

cheers

andrew

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Andrew Dunstan (#1)
Re: C language context

Andrew Dunstan <andrew@dunslane.net> writes:

Just how transient is the memory context created for a C language
function call?

Fairly. IIRC, you'll normally be running in a context that will be
reset at the start of the next tuple cycle for the plan node your
function is evaluated by. If you've got a heavily recursive routine,
it might need to do its own cleanup per-call, but most of the time you
need not bother with pfree.

I'm still trying to get to the bottom of it, but I want to know if I'm
safe in relying on the context cleanup to handle things for me. It's an
immutable text function returning text, and is intended for use in a
functional index.

You might wanna check the code, but I think we force a context reset
after each tuple for functional index evaluations as well.

AFAIR, the only case where we really still require user-provided
functions to avoid leaking memory is support functions and operators for
indexes (for example, the comparison functions used by btrees). The
internal memory usage in the index AMs is hairy enough to make it
hard to fix that, though I'd like to do so someday.

regards, tom lane