I believe the bug to be here:
/* Normal case for scalar results */
estate->retval = exec_eval_expr(estate, stmt->expr,
&(estate->retisnull),
&(estate->rettype));
in exec_stmt_return() in pl_exec.c.
As I understand it, exec_eval_expr overwrites(!) estate->rettype instead
of casting the result to it.
What should be done is something like:
Datum value;
Oid valtype;
value = exec_eval_expr(estate, stmt->expr, &(estate->retisnull),
&valtype);
estate->retval = exec_cast_value(value, valtype, estate->rettype,
reqinput,
reqioparam,
reqtypmod,
estate->retisnull);
but where to get reqinput, reqioparam and reqtypmod? They should be
accessed through estate pointer, I think.
---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?
http://archives.postgresql.org