After going through that experience, I now agree with Kevin: an > interface where a new SPI interface lets PLs push a named tuplestore > into the SPI connection to make it available to SQL seems like the > simplest and tidiest way.
That also offers a handy step on the path toward table-valued variables and pipelined functions, both of which would be _really_ nice for PL/PgSQL users.
FWIW, I expect at some point we'd like the ability to index tuplestores as well.