MemoryContextSwitchTo() confusion - Mailing list pgsql-hackers

From Dan Searle
Subject MemoryContextSwitchTo() confusion
Date
Msg-id 47E1621F.3090606@adelix.com
Whole thread Raw
Responses Re: MemoryContextSwitchTo() confusion
Re: MemoryContextSwitchTo() confusion
List pgsql-hackers
Hi,

I've written a custom C-language function that takes a SQL select 
statement as it's input parameter, runs the query using the SPI 
interface, iterates over all the results of the select using a cursor 
and collates the data using a tsearch type binary tree, then walks the 
tree to create a flat vector of ordered nodes all in the first call 
(i.e. SRF_IS_FIRSTCALL()). It then contructs tuples, turns them into 
Datums and returns all it's records with subsequent calls.

I've used the example code in the documentation, and used the doxygen 
docs for tablefunc.c as guidelines.

Everything seems to work fine, except on the final call it has no more 
data to return so cleans up all it's internal data structures and 
returns with SRF_RETURN_DONE(funcctx).

I had to fiddle about with switching memory contexts rather a lot to 
make it work this far, but I'm only guessing as to when it's appropriate 
to call MemoryContextSwitchTo(), and to which context to switch to. I 
had a few SEGV's and had to add a few MemoryContextSwitchTo() calls in 
various places, however, can someone please explain the need for 
MemoryContextSwitchTo() and when it's needed, as I'm basically guessing 
and I think it's the reason for the SEGV after the final call to 
SRF_RETURN_DONE() after it's returned all it's tuples.

I have verified that all the code seems to work, and that it is after 
the final call to SRF_RETURN_DONE() that the SEGV happens by writing 
debug strings to a file handle.

I can provide a copy of the sourece code, but I thought it best to ask 
first.

Regards, Dan...


------------------------------------------------------------------------------------
Scanned for viruses, spam and offensive content by CensorNet MailSafe

Professional Web & E-mail Filtering from www.censornet.com


pgsql-hackers by date:

Previous
From: Martin Pihlak
Date:
Subject: Re: stored procedure stats in collector
Next
From: Gregory Stark
Date:
Subject: Re: Text <-> C string