I have tried to solve this issue. Please see the attached patch.
With this patch, any expression is allowed in the return statement. For any invalid expression an error is generated without doing any special handling.
When a row expression is used in the return statement then the resultant tuple will have rowtype in a single column that needed to be extracted. Hence I have handled that case in exec_stmt_return().
> I wrote patch some years ago. It was rejected from performance reasons > - because every row had to be casted to resulted type.
I don't recall that patch in any detail, but it's not apparent to me that an extra cast step *must* be required to implement this. In the cases that are supported now, surely we could notice that no additional work is required.
It's also worth commenting that if we were to switch the storage of composite-type plpgsql variables to HeapTuple, as has been suggested here for other reasons, the performance tradeoffs in this area would likely change completely anyway.