It seems worthwhile to investigate how we might go about improving this so that the evaluation of the target list happens after LIMIT, at least for the columns which are not required before LIMIT.
Currently, the FunctionScan executes until it decides to stop. Then later the LIMIT is applied.
To change this, you'd need to pass down the LIMIT value into the FunctionScan node, as is already done between Limit/Sort nodes.
On the FunctionScan, you can set the max_calls value in the FuncCallContext, but currently that isn't enforced.
--
Simon Riggs http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services