*************** *** 367,447 **** */ tlistlen = ExecCleanTargetListLength(tlist); - /* - * For base-type returns, the target list should have exactly one - * entry, and its type should agree with what the user declared. (As - * of Postgres 7.2, we accept binary-compatible types too.) - */ typerelid = typeidTypeRelid(rettype); - if (typerelid == InvalidOid) - { - if (tlistlen != 1) - elog(ERROR, "function declared to return %s returns multiple columns in final SELECT", - format_type_be(rettype)); ! restype = ((TargetEntry *) lfirst(tlist))->resdom->restype; ! if (!IsBinaryCompatible(restype, rettype)) ! elog(ERROR, "return type mismatch in function: declared to return %s, returns %s", ! format_type_be(rettype), format_type_be(restype)); ! return; ! } - /* - * If the target list is of length 1, and the type of the varnode in - * the target list matches the declared return type, this is okay. - * This can happen, for example, where the body of the function is - * 'SELECT func2()', where func2 has the same return type as the - * function that's calling it. - */ - if (tlistlen == 1) - { - restype = ((TargetEntry *) lfirst(tlist))->resdom->restype; - if (IsBinaryCompatible(restype, rettype)) return; } ! /* ! * By here, the procedure returns a tuple or set of tuples. This part ! * of the typechecking is a hack. We look up the relation that is the ! * declared return type, and be sure that attributes 1 .. n in the ! * target list match the declared types. ! */ ! reln = heap_open(typerelid, AccessShareLock); ! relid = reln->rd_id; ! relnatts = reln->rd_rel->relnatts; ! ! if (tlistlen != relnatts) ! elog(ERROR, "function declared to return %s does not SELECT the right number of columns (%d)", ! format_type_be(rettype), relnatts); ! /* expect attributes 1 .. n in order */ ! i = 0; ! foreach(tlistitem, tlist) ! { ! TargetEntry *tle = (TargetEntry *) lfirst(tlistitem); ! Oid tletype; ! Oid atttype; ! ! if (tle->resdom->resjunk) ! continue; ! tletype = exprType(tle->expr); ! atttype = reln->rd_att->attrs[i]->atttypid; ! if (!IsBinaryCompatible(tletype, atttype)) ! elog(ERROR, "function declared to return %s returns %s instead of %s at column %d", ! format_type_be(rettype), ! format_type_be(tletype), ! format_type_be(atttype), ! i + 1); ! i++; ! } ! ! /* this shouldn't happen, but let's just check... */ ! if (i != relnatts) ! elog(ERROR, "function declared to return %s does not SELECT the right number of columns (%d)", ! format_type_be(rettype), relnatts); ! heap_close(reln, AccessShareLock); } --- 368,467 ---- */ tlistlen = ExecCleanTargetListLength(tlist); typerelid = typeidTypeRelid(rettype); ! if (fn_typtype == 'b') ! { ! /* ! * For base-type returns, the target list should have exactly one ! * entry, and its type should agree with what the user declared. (As ! * of Postgres 7.2, we accept binary-compatible types too.) ! */ ! if (typerelid == InvalidOid) ! { ! if (tlistlen != 1) ! elog(ERROR, "function declared to return %s returns multiple columns in final SELECT", ! format_type_be(rettype)); ! ! restype = ((TargetEntry *) lfirst(tlist))->resdom->restype; ! if (!IsBinaryCompatible(restype, rettype)) ! elog(ERROR, "return type mismatch in function: declared to return %s, returns %s", ! format_type_be(rettype), format_type_be(restype)); return; + } + + /* + * If the target list is of length 1, and the type of the varnode in + * the target list matches the declared return type, this is okay. + * This can happen, for example, where the body of the function is + * 'SELECT func2()', where func2 has the same return type as the + * function that's calling it. + */ + if (tlistlen == 1) + { + restype = ((TargetEntry *) lfirst(tlist))->resdom->restype; + if (IsBinaryCompatible(restype, rettype)) + return; + } } + else if (fn_typtype == 'c') + { + /* + * By here, the procedure returns a tuple or set of tuples. This part + * of the typechecking is a hack. We look up the relation that is the + * declared return type, and be sure that attributes 1 .. n in the + * target list match the declared types. + */ + reln = heap_open(typerelid, AccessShareLock); + relid = reln->rd_id; + relnatts = reln->rd_rel->relnatts; + + if (tlistlen != relnatts) + elog(ERROR, "function declared to return %s does not SELECT the right number of columns (%d)", + format_type_be(rettype), relnatts); + + /* expect attributes 1 .. n in order */ + i = 0; + foreach(tlistitem, tlist) + { + TargetEntry *tle = (TargetEntry *) lfirst(tlistitem); + Oid tletype; + Oid atttype; + + if (tle->resdom->resjunk) + continue; + tletype = exprType(tle->expr); + atttype = reln->rd_att->attrs[i]->atttypid; + if (!IsBinaryCompatible(tletype, atttype)) + elog(ERROR, "function declared to return %s returns %s instead of %s at column %d", + format_type_be(rettype), + format_type_be(tletype), + format_type_be(atttype), + i + 1); + i++; + } ! /* this shouldn't happen, but let's just check... */ ! if (i != relnatts) ! elog(ERROR, "function declared to return %s does not SELECT the right number of columns (%d)", ! format_type_be(rettype), relnatts); ! heap_close(reln, AccessShareLock); ! return; ! } ! else if (fn_typtype == 'p' && rettype == RECORDOID) ! { ! /* ! * For RECORD return type, defer this check until we get the ! * first tuple. ! */ ! return; ! } ! else ! elog(ERROR, "Unknown kind of return type specified for function"); }