SPI_connect() changes the memory context to a newly-created one, and then SPI_finish() restores it. That seems a bit dangerous because the caller might not be expecting it. Is there a reason it doesn't just change to the new memory context as-needed?
spi.c:161: /* ... and switch to procedure's context */ _SPI_current->savedcxt = MemoryContextSwitchTo(_SPI_current- >procCxt); Regards, Jeff Davis