SPI_finish and RegisterExprContextCallback

From: Thomas Hallgren <thhal(at)mailblocks(dot)com>
To: "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: SPI_finish and RegisterExprContextCallback
Date: 2005-02-18 14:05:48
Message-ID: thhal-0vo7xAueixicgfalih4JG7E89BwyDfT@mailblocks.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2005-02-18 14:37:10 Re: SPI_finish and RegisterExprContextCallback
Previous Message John DeSoi 2005-02-18 13:16:28 sigint psql