I'd like to write a C-function that returns a SETOF a complex type. This
set is obtained from a query performed using an SPI cursor. I don't want
to build the complete set in memory so I tried the following.
1. During the SRF_IS_FIRSTCALL phase, I do an SPI_connect and I create
the cursor using SPI_prepare and SPI_cursor_open.
2. For each call, I obtain row(s) as needed using SPI_cursor_fetch. A
row is copied before I return the tuple in a SRF_RETURN_NEXT
3. When I have no more rows, I close the cursor and issue a SPI_finish.
Then I return SRF_RETURN DONE.
This works beautifully.
Now I'm trying to deal with scenarios where I never reach the end of the
set because the evaluator doesn't need all rows. So I use
RegisterExprContextCallback to register a callback and instead of doing
an SPI_finish when the end is reached I attempt to do this in the
callback. The callback is called OK but when it calls SPI_finish I get
an illegal memory access signal.
How am I supposed to do this? Is it at all possible to stream the
results of one query to another using a SETOF function?
Regards,
Thomas Hallgren