1 /*-------------------------------------------------------------------------
4 * Utility and convenience functions for fmgr functions that return
5 * sets and/or composite types.
7 * Copyright (c) 2002-2007, PostgreSQL Global Development Group
10 * $PostgreSQL: pgsql/src/backend/utils/fmgr/funcapi.c,v 1.32 2007/01/05 22:19:43 momjian Exp $
12 *-------------------------------------------------------------------------
16 #include "access/heapam.h"
17 #include "catalog/namespace.h"
18 #include "catalog/pg_proc.h"
19 #include "catalog/pg_type.h"
21 #include "parser/parse_coerce.h"
22 #include "parser/parse_expr.h"
23 #include "utils/array.h"
24 #include "utils/builtins.h"
25 #include "utils/lsyscache.h"
26 #include "utils/syscache.h"
27 #include "utils/typcache.h"
30 static void shutdown_MultiFuncCall(Datum arg);
31 static TypeFuncClass internal_get_result_type(Oid funcid,
33 ReturnSetInfo *rsinfo,
35 TupleDesc *resultTupleDesc);
36 static bool resolve_polymorphic_tupdesc(TupleDesc tupdesc,
37 oidvector *declared_args,
39 static TypeFuncClass get_type_func_class(Oid typid);
44 * Create an empty FuncCallContext data structure
45 * and do some other basic Multi-function call setup
49 init_MultiFuncCall(PG_FUNCTION_ARGS)
51 FuncCallContext *retval;
54 * Bail if we're called in the wrong context
56 if (fcinfo->resultinfo == NULL || !IsA(fcinfo->resultinfo, ReturnSetInfo))
58 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
59 errmsg("set-valued function called in context that cannot accept a set")));
61 if (fcinfo->flinfo->fn_extra == NULL)
66 ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
69 * Allocate suitably long-lived space and zero it
71 retval = (FuncCallContext *)
72 MemoryContextAllocZero(fcinfo->flinfo->fn_mcxt,
73 sizeof(FuncCallContext));
76 * initialize the elements
78 retval->call_cntr = 0;
79 retval->max_calls = 0;
81 retval->user_fctx = NULL;
82 retval->attinmeta = NULL;
83 retval->tuple_desc = NULL;
84 retval->multi_call_memory_ctx = fcinfo->flinfo->fn_mcxt;
87 * save the pointer for cross-call use
89 fcinfo->flinfo->fn_extra = retval;
92 * Ensure we will get shut down cleanly if the exprcontext is not run
95 RegisterExprContextCallback(rsi->econtext,
96 shutdown_MultiFuncCall,
97 PointerGetDatum(fcinfo->flinfo));
101 /* second and subsequent calls */
102 elog(ERROR, "init_MultiFuncCall may not be called more than once");
104 /* never reached, but keep compiler happy */
114 * Do Multi-function per-call setup
117 per_MultiFuncCall(PG_FUNCTION_ARGS)
119 FuncCallContext *retval = (FuncCallContext *) fcinfo->flinfo->fn_extra;
122 * Clear the TupleTableSlot, if present. This is for safety's sake: the
123 * Slot will be in a long-lived context (it better be, if the
124 * FuncCallContext is pointing to it), but in most usage patterns the
125 * tuples stored in it will be in the function's per-tuple context. So at
126 * the beginning of each call, the Slot will hold a dangling pointer to an
127 * already-recycled tuple. We clear it out here.
129 * Note: use of retval->slot is obsolete as of 8.0, and we expect that it
130 * will always be NULL. This is just here for backwards compatibility in
131 * case someone creates a slot anyway.
133 if (retval->slot != NULL)
134 ExecClearTuple(retval->slot);
141 * Clean up after init_MultiFuncCall
144 end_MultiFuncCall(PG_FUNCTION_ARGS, FuncCallContext *funcctx)
146 ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
148 /* Deregister the shutdown callback */
149 UnregisterExprContextCallback(rsi->econtext,
150 shutdown_MultiFuncCall,
151 PointerGetDatum(fcinfo->flinfo));
153 /* But use it to do the real work */
154 shutdown_MultiFuncCall(PointerGetDatum(fcinfo->flinfo));
158 * shutdown_MultiFuncCall
159 * Shutdown function to clean up after init_MultiFuncCall
162 shutdown_MultiFuncCall(Datum arg)
164 FmgrInfo *flinfo = (FmgrInfo *) DatumGetPointer(arg);
165 FuncCallContext *funcctx = (FuncCallContext *) flinfo->fn_extra;
167 /* unbind from flinfo */
168 flinfo->fn_extra = NULL;
171 * Caller is responsible to free up memory for individual struct elements
172 * other than att_in_funcinfo and elements.
174 if (funcctx->attinmeta != NULL)
175 pfree(funcctx->attinmeta);
182 * get_call_result_type
183 * Given a function's call info record, determine the kind of datatype
184 * it is supposed to return. If resultTypeId isn't NULL, *resultTypeId
185 * receives the actual datatype OID (this is mainly useful for scalar
186 * result types). If resultTupleDesc isn't NULL, *resultTupleDesc
187 * receives a pointer to a TupleDesc when the result is of a composite
188 * type, or NULL when it's a scalar result.
190 * One hard case that this handles is resolution of actual rowtypes for
191 * functions returning RECORD (from either the function's OUT parameter
192 * list, or a ReturnSetInfo context node). TYPEFUNC_RECORD is returned
193 * only when we couldn't resolve the actual rowtype for lack of information.
195 * The other hard case that this handles is resolution of polymorphism.
196 * We will never return ANYELEMENT or ANYARRAY, either as a scalar result
197 * type or as a component of a rowtype.
199 * This function is relatively expensive --- in a function returning set,
200 * try to call it only the first time through.
203 get_call_result_type(FunctionCallInfo fcinfo,
205 TupleDesc *resultTupleDesc)
207 return internal_get_result_type(fcinfo->flinfo->fn_oid,
208 fcinfo->flinfo->fn_expr,
209 (ReturnSetInfo *) fcinfo->resultinfo,
215 * get_expr_result_type
216 * As above, but work from a calling expression node tree
219 get_expr_result_type(Node *expr,
221 TupleDesc *resultTupleDesc)
223 TypeFuncClass result;
225 if (expr && IsA(expr, FuncExpr))
226 result = internal_get_result_type(((FuncExpr *) expr)->funcid,
231 else if (expr && IsA(expr, OpExpr))
232 result = internal_get_result_type(get_opcode(((OpExpr *) expr)->opno),
239 /* handle as a generic expression; no chance to resolve RECORD */
240 Oid typid = exprType(expr);
243 *resultTypeId = typid;
245 *resultTupleDesc = NULL;
246 result = get_type_func_class(typid);
247 if (result == TYPEFUNC_COMPOSITE && resultTupleDesc)
248 *resultTupleDesc = lookup_rowtype_tupdesc_copy(typid, -1);
255 * get_func_result_type
256 * As above, but work from a function's OID only
258 * This will not be able to resolve pure-RECORD results nor polymorphism.
261 get_func_result_type(Oid functionId,
263 TupleDesc *resultTupleDesc)
265 return internal_get_result_type(functionId,
273 * internal_get_result_type -- workhorse code implementing all the above
275 * funcid must always be supplied. call_expr and rsinfo can be NULL if not
276 * available. We will return TYPEFUNC_RECORD, and store NULL into
277 * *resultTupleDesc, if we cannot deduce the complete result rowtype from
278 * the available information.
281 internal_get_result_type(Oid funcid,
283 ReturnSetInfo *rsinfo,
285 TupleDesc *resultTupleDesc)
287 TypeFuncClass result;
289 Form_pg_proc procform;
293 /* First fetch the function's pg_proc row to inspect its rettype */
294 tp = SearchSysCache(PROCOID,
295 ObjectIdGetDatum(funcid),
297 if (!HeapTupleIsValid(tp))
298 elog(ERROR, "cache lookup failed for function %u", funcid);
299 procform = (Form_pg_proc) GETSTRUCT(tp);
301 rettype = procform->prorettype;
303 /* Check for OUT parameters defining a RECORD result */
304 tupdesc = build_function_result_tupdesc_t(tp);
308 * It has OUT parameters, so it's basically like a regular composite
309 * type, except we have to be able to resolve any polymorphic OUT
313 *resultTypeId = rettype;
315 if (resolve_polymorphic_tupdesc(tupdesc,
316 &procform->proargtypes,
319 if (tupdesc->tdtypeid == RECORDOID &&
320 tupdesc->tdtypmod < 0)
321 assign_record_type_typmod(tupdesc);
323 *resultTupleDesc = tupdesc;
324 result = TYPEFUNC_COMPOSITE;
329 *resultTupleDesc = NULL;
330 result = TYPEFUNC_RECORD;
339 * If scalar polymorphic result, try to resolve it.
341 if (rettype == ANYARRAYOID || rettype == ANYELEMENTOID)
343 Oid newrettype = exprType(call_expr);
345 if (newrettype == InvalidOid) /* this probably should not happen */
347 (errcode(ERRCODE_DATATYPE_MISMATCH),
348 errmsg("could not determine actual result type for function \"%s\" declared to return type %s",
349 NameStr(procform->proname),
350 format_type_be(rettype))));
351 rettype = newrettype;
355 *resultTypeId = rettype;
357 *resultTupleDesc = NULL; /* default result */
359 /* Classify the result type */
360 result = get_type_func_class(rettype);
363 case TYPEFUNC_COMPOSITE:
365 *resultTupleDesc = lookup_rowtype_tupdesc_copy(rettype, -1);
366 /* Named composite types can't have any polymorphic columns */
368 case TYPEFUNC_SCALAR:
370 case TYPEFUNC_RECORD:
371 /* We must get the tupledesc from call context */
372 if (rsinfo && IsA(rsinfo, ReturnSetInfo) &&
373 rsinfo->expectedDesc != NULL)
375 result = TYPEFUNC_COMPOSITE;
377 *resultTupleDesc = rsinfo->expectedDesc;
378 /* Assume no polymorphic columns here, either */
391 * Given the result tuple descriptor for a function with OUT parameters,
392 * replace any polymorphic columns (ANYELEMENT/ANYARRAY) with correct data
393 * types deduced from the input arguments. Returns TRUE if able to deduce
394 * all types, FALSE if not.
397 resolve_polymorphic_tupdesc(TupleDesc tupdesc, oidvector *declared_args,
400 int natts = tupdesc->natts;
401 int nargs = declared_args->dim1;
402 bool have_anyelement_result = false;
403 bool have_anyarray_result = false;
404 Oid anyelement_type = InvalidOid;
405 Oid anyarray_type = InvalidOid;
408 /* See if there are any polymorphic outputs; quick out if not */
409 for (i = 0; i < natts; i++)
411 switch (tupdesc->attrs[i]->atttypid)
414 have_anyelement_result = true;
417 have_anyarray_result = true;
423 if (!have_anyelement_result && !have_anyarray_result)
427 * Otherwise, extract actual datatype(s) from input arguments. (We assume
428 * the parser already validated consistency of the arguments.)
431 return false; /* no hope */
433 for (i = 0; i < nargs; i++)
435 switch (declared_args->values[i])
438 if (!OidIsValid(anyelement_type))
439 anyelement_type = get_call_expr_argtype(call_expr, i);
442 if (!OidIsValid(anyarray_type))
443 anyarray_type = get_call_expr_argtype(call_expr, i);
450 /* If nothing found, parser messed up */
451 if (!OidIsValid(anyelement_type) && !OidIsValid(anyarray_type))
454 /* If needed, deduce one polymorphic type from the other */
455 if (have_anyelement_result && !OidIsValid(anyelement_type))
456 anyelement_type = resolve_generic_type(ANYELEMENTOID,
459 if (have_anyarray_result && !OidIsValid(anyarray_type))
460 anyarray_type = resolve_generic_type(ANYARRAYOID,
464 /* And finally replace the tuple column types as needed */
465 for (i = 0; i < natts; i++)
467 switch (tupdesc->attrs[i]->atttypid)
470 TupleDescInitEntry(tupdesc, i + 1,
471 NameStr(tupdesc->attrs[i]->attname),
477 TupleDescInitEntry(tupdesc, i + 1,
478 NameStr(tupdesc->attrs[i]->attname),
492 * Given the declared argument types and modes for a function,
493 * replace any polymorphic types (ANYELEMENT/ANYARRAY) with correct data
494 * types deduced from the input arguments. Returns TRUE if able to deduce
495 * all types, FALSE if not. This is the same logic as
496 * resolve_polymorphic_tupdesc, but with a different argument representation.
498 * argmodes may be NULL, in which case all arguments are assumed to be IN mode.
501 resolve_polymorphic_argtypes(int numargs, Oid *argtypes, char *argmodes,
504 bool have_anyelement_result = false;
505 bool have_anyarray_result = false;
506 Oid anyelement_type = InvalidOid;
507 Oid anyarray_type = InvalidOid;
511 /* First pass: resolve polymorphic inputs, check for outputs */
513 for (i = 0; i < numargs; i++)
515 char argmode = argmodes ? argmodes[i] : PROARGMODE_IN;
520 if (argmode == PROARGMODE_OUT)
521 have_anyelement_result = true;
524 if (!OidIsValid(anyelement_type))
526 anyelement_type = get_call_expr_argtype(call_expr,
528 if (!OidIsValid(anyelement_type))
531 argtypes[i] = anyelement_type;
535 if (argmode == PROARGMODE_OUT)
536 have_anyarray_result = true;
539 if (!OidIsValid(anyarray_type))
541 anyarray_type = get_call_expr_argtype(call_expr,
543 if (!OidIsValid(anyarray_type))
546 argtypes[i] = anyarray_type;
552 if (argmode != PROARGMODE_OUT)
557 if (!have_anyelement_result && !have_anyarray_result)
560 /* If no input polymorphics, parser messed up */
561 if (!OidIsValid(anyelement_type) && !OidIsValid(anyarray_type))
564 /* If needed, deduce one polymorphic type from the other */
565 if (have_anyelement_result && !OidIsValid(anyelement_type))
566 anyelement_type = resolve_generic_type(ANYELEMENTOID,
569 if (have_anyarray_result && !OidIsValid(anyarray_type))
570 anyarray_type = resolve_generic_type(ANYARRAYOID,
574 /* And finally replace the output column types as needed */
575 for (i = 0; i < numargs; i++)
580 argtypes[i] = anyelement_type;
583 argtypes[i] = anyarray_type;
594 * get_type_func_class
595 * Given the type OID, obtain its TYPEFUNC classification.
597 * This is intended to centralize a bunch of formerly ad-hoc code for
598 * classifying types. The categories used here are useful for deciding
599 * how to handle functions returning the datatype.
602 get_type_func_class(Oid typid)
604 switch (get_typtype(typid))
607 return TYPEFUNC_COMPOSITE;
610 return TYPEFUNC_SCALAR;
612 if (typid == RECORDOID)
613 return TYPEFUNC_RECORD;
616 * We treat VOID and CSTRING as legitimate scalar datatypes,
617 * mostly for the convenience of the JDBC driver (which wants to
618 * be able to do "SELECT * FROM foo()" for all legitimately
619 * user-callable functions).
621 if (typid == VOIDOID || typid == CSTRINGOID)
622 return TYPEFUNC_SCALAR;
623 return TYPEFUNC_OTHER;
625 /* shouldn't get here, probably */
626 return TYPEFUNC_OTHER;
633 * Fetch info about the argument types, names, and IN/OUT modes from the
634 * pg_proc tuple. Return value is the total number of arguments.
635 * Other results are palloc'd. *p_argtypes is always filled in, but
636 * *p_argnames and *p_argmodes will be set NULL in the default cases
637 * (no names, and all IN arguments, respectively).
639 * Note that this function simply fetches what is in the pg_proc tuple;
640 * it doesn't do any interpretation of polymorphic types.
643 get_func_arg_info(HeapTuple procTup,
644 Oid **p_argtypes, char ***p_argnames, char **p_argmodes)
646 Form_pg_proc procStruct = (Form_pg_proc) GETSTRUCT(procTup);
647 Datum proallargtypes;
657 /* First discover the total number of parameters and get their types */
658 proallargtypes = SysCacheGetAttr(PROCOID, procTup,
659 Anum_pg_proc_proallargtypes,
664 * We expect the arrays to be 1-D arrays of the right types; verify
665 * that. For the OID and char arrays, we don't need to use
666 * deconstruct_array() since the array data is just going to look like
667 * a C array of values.
669 arr = DatumGetArrayTypeP(proallargtypes); /* ensure not toasted */
670 numargs = ARR_DIMS(arr)[0];
671 if (ARR_NDIM(arr) != 1 ||
674 ARR_ELEMTYPE(arr) != OIDOID)
675 elog(ERROR, "proallargtypes is not a 1-D Oid array");
676 Assert(numargs >= procStruct->pronargs);
677 *p_argtypes = (Oid *) palloc(numargs * sizeof(Oid));
678 memcpy(*p_argtypes, ARR_DATA_PTR(arr),
679 numargs * sizeof(Oid));
683 /* If no proallargtypes, use proargtypes */
684 numargs = procStruct->proargtypes.dim1;
685 Assert(numargs == procStruct->pronargs);
686 *p_argtypes = (Oid *) palloc(numargs * sizeof(Oid));
687 memcpy(*p_argtypes, procStruct->proargtypes.values,
688 numargs * sizeof(Oid));
691 /* Get argument names, if available */
692 proargnames = SysCacheGetAttr(PROCOID, procTup,
693 Anum_pg_proc_proargnames,
699 deconstruct_array(DatumGetArrayTypeP(proargnames),
700 TEXTOID, -1, false, 'i',
701 &elems, NULL, &nelems);
702 if (nelems != numargs) /* should not happen */
703 elog(ERROR, "proargnames must have the same number of elements as the function has arguments");
704 *p_argnames = (char **) palloc(sizeof(char *) * numargs);
705 for (i = 0; i < numargs; i++)
706 (*p_argnames)[i] = DatumGetCString(DirectFunctionCall1(textout,
710 /* Get argument modes, if available */
711 proargmodes = SysCacheGetAttr(PROCOID, procTup,
712 Anum_pg_proc_proargmodes,
718 arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
719 if (ARR_NDIM(arr) != 1 ||
720 ARR_DIMS(arr)[0] != numargs ||
722 ARR_ELEMTYPE(arr) != CHAROID)
723 elog(ERROR, "proargmodes is not a 1-D char array");
724 *p_argmodes = (char *) palloc(numargs * sizeof(char));
725 memcpy(*p_argmodes, ARR_DATA_PTR(arr),
726 numargs * sizeof(char));
734 * get_func_result_name
736 * If the function has exactly one output parameter, and that parameter
737 * is named, return the name (as a palloc'd string). Else return NULL.
739 * This is used to determine the default output column name for functions
740 * returning scalar types.
743 get_func_result_name(Oid functionId)
758 /* First fetch the function's pg_proc row */
759 procTuple = SearchSysCache(PROCOID,
760 ObjectIdGetDatum(functionId),
762 if (!HeapTupleIsValid(procTuple))
763 elog(ERROR, "cache lookup failed for function %u", functionId);
765 /* If there are no named OUT parameters, return NULL */
766 if (heap_attisnull(procTuple, Anum_pg_proc_proargmodes) ||
767 heap_attisnull(procTuple, Anum_pg_proc_proargnames))
771 /* Get the data out of the tuple */
772 proargmodes = SysCacheGetAttr(PROCOID, procTuple,
773 Anum_pg_proc_proargmodes,
776 proargnames = SysCacheGetAttr(PROCOID, procTuple,
777 Anum_pg_proc_proargnames,
782 * We expect the arrays to be 1-D arrays of the right types; verify
783 * that. For the char array, we don't need to use deconstruct_array()
784 * since the array data is just going to look like a C array of
787 arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
788 numargs = ARR_DIMS(arr)[0];
789 if (ARR_NDIM(arr) != 1 ||
792 ARR_ELEMTYPE(arr) != CHAROID)
793 elog(ERROR, "proargmodes is not a 1-D char array");
794 argmodes = (char *) ARR_DATA_PTR(arr);
795 arr = DatumGetArrayTypeP(proargnames); /* ensure not toasted */
796 if (ARR_NDIM(arr) != 1 ||
797 ARR_DIMS(arr)[0] != numargs ||
799 ARR_ELEMTYPE(arr) != TEXTOID)
800 elog(ERROR, "proargnames is not a 1-D text array");
801 deconstruct_array(arr, TEXTOID, -1, false, 'i',
802 &argnames, NULL, &nargnames);
803 Assert(nargnames == numargs);
805 /* scan for output argument(s) */
808 for (i = 0; i < numargs; i++)
810 if (argmodes[i] == PROARGMODE_IN)
812 Assert(argmodes[i] == PROARGMODE_OUT ||
813 argmodes[i] == PROARGMODE_INOUT);
814 if (++numoutargs > 1)
816 /* multiple out args, so forget it */
820 result = DatumGetCString(DirectFunctionCall1(textout,
822 if (result == NULL || result[0] == '\0')
824 /* Parameter is not named, so forget it */
831 ReleaseSysCache(procTuple);
838 * build_function_result_tupdesc_t
840 * Given a pg_proc row for a function, return a tuple descriptor for the
841 * result rowtype, or NULL if the function does not have OUT parameters.
843 * Note that this does not handle resolution of ANYELEMENT/ANYARRAY types;
844 * that is deliberate.
847 build_function_result_tupdesc_t(HeapTuple procTuple)
849 Form_pg_proc procform = (Form_pg_proc) GETSTRUCT(procTuple);
850 Datum proallargtypes;
855 /* Return NULL if the function isn't declared to return RECORD */
856 if (procform->prorettype != RECORDOID)
859 /* If there are no OUT parameters, return NULL */
860 if (heap_attisnull(procTuple, Anum_pg_proc_proallargtypes) ||
861 heap_attisnull(procTuple, Anum_pg_proc_proargmodes))
864 /* Get the data out of the tuple */
865 proallargtypes = SysCacheGetAttr(PROCOID, procTuple,
866 Anum_pg_proc_proallargtypes,
869 proargmodes = SysCacheGetAttr(PROCOID, procTuple,
870 Anum_pg_proc_proargmodes,
873 proargnames = SysCacheGetAttr(PROCOID, procTuple,
874 Anum_pg_proc_proargnames,
877 proargnames = PointerGetDatum(NULL); /* just to be sure */
879 return build_function_result_tupdesc_d(proallargtypes,
885 * build_function_result_tupdesc_d
887 * Build a RECORD function's tupledesc from the pg_proc proallargtypes,
888 * proargmodes, and proargnames arrays. This is split out for the
889 * convenience of ProcedureCreate, which needs to be able to compute the
890 * tupledesc before actually creating the function.
892 * Returns NULL if there are not at least two OUT or INOUT arguments.
895 build_function_result_tupdesc_d(Datum proallargtypes,
904 Datum *argnames = NULL;
911 /* Can't have output args if columns are null */
912 if (proallargtypes == PointerGetDatum(NULL) ||
913 proargmodes == PointerGetDatum(NULL))
917 * We expect the arrays to be 1-D arrays of the right types; verify that.
918 * For the OID and char arrays, we don't need to use deconstruct_array()
919 * since the array data is just going to look like a C array of values.
921 arr = DatumGetArrayTypeP(proallargtypes); /* ensure not toasted */
922 numargs = ARR_DIMS(arr)[0];
923 if (ARR_NDIM(arr) != 1 ||
926 ARR_ELEMTYPE(arr) != OIDOID)
927 elog(ERROR, "proallargtypes is not a 1-D Oid array");
928 argtypes = (Oid *) ARR_DATA_PTR(arr);
929 arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
930 if (ARR_NDIM(arr) != 1 ||
931 ARR_DIMS(arr)[0] != numargs ||
933 ARR_ELEMTYPE(arr) != CHAROID)
934 elog(ERROR, "proargmodes is not a 1-D char array");
935 argmodes = (char *) ARR_DATA_PTR(arr);
936 if (proargnames != PointerGetDatum(NULL))
938 arr = DatumGetArrayTypeP(proargnames); /* ensure not toasted */
939 if (ARR_NDIM(arr) != 1 ||
940 ARR_DIMS(arr)[0] != numargs ||
942 ARR_ELEMTYPE(arr) != TEXTOID)
943 elog(ERROR, "proargnames is not a 1-D text array");
944 deconstruct_array(arr, TEXTOID, -1, false, 'i',
945 &argnames, NULL, &nargnames);
946 Assert(nargnames == numargs);
949 /* zero elements probably shouldn't happen, but handle it gracefully */
953 /* extract output-argument types and names */
954 outargtypes = (Oid *) palloc(numargs * sizeof(Oid));
955 outargnames = (char **) palloc(numargs * sizeof(char *));
957 for (i = 0; i < numargs; i++)
961 if (argmodes[i] == PROARGMODE_IN)
963 Assert(argmodes[i] == PROARGMODE_OUT ||
964 argmodes[i] == PROARGMODE_INOUT);
965 outargtypes[numoutargs] = argtypes[i];
967 pname = DatumGetCString(DirectFunctionCall1(textout, argnames[i]));
970 if (pname == NULL || pname[0] == '\0')
972 /* Parameter is not named, so gin up a column name */
973 pname = (char *) palloc(32);
974 snprintf(pname, 32, "column%d", numoutargs + 1);
976 outargnames[numoutargs] = pname;
981 * If there is no output argument, or only one, the function does not
987 desc = CreateTemplateTupleDesc(numoutargs, false);
988 for (i = 0; i < numoutargs; i++)
990 TupleDescInitEntry(desc, i + 1,
1002 * RelationNameGetTupleDesc
1004 * Given a (possibly qualified) relation name, build a TupleDesc.
1006 * Note: while this works as advertised, it's seldom the best way to
1007 * build a tupdesc for a function's result type. It's kept around
1008 * only for backwards compatibility with existing user-written code.
1011 RelationNameGetTupleDesc(const char *relname)
1018 /* Open relation and copy the tuple description */
1019 relname_list = stringToQualifiedNameList(relname, "RelationNameGetTupleDesc");
1020 relvar = makeRangeVarFromNameList(relname_list);
1021 rel = relation_openrv(relvar, AccessShareLock);
1022 tupdesc = CreateTupleDescCopy(RelationGetDescr(rel));
1023 relation_close(rel, AccessShareLock);
1031 * Given a type Oid, build a TupleDesc. (In most cases you should be
1032 * using get_call_result_type or one of its siblings instead of this
1033 * routine, so that you can handle OUT parameters, RECORD result type,
1034 * and polymorphic results.)
1036 * If the type is composite, *and* a colaliases List is provided, *and*
1037 * the List is of natts length, use the aliases instead of the relation
1038 * attnames. (NB: this usage is deprecated since it may result in
1039 * creation of unnecessary transient record types.)
1041 * If the type is a base type, a single item alias List is required.
1044 TypeGetTupleDesc(Oid typeoid, List *colaliases)
1046 TypeFuncClass functypclass = get_type_func_class(typeoid);
1047 TupleDesc tupdesc = NULL;
1050 * Build a suitable tupledesc representing the output rows
1052 if (functypclass == TYPEFUNC_COMPOSITE)
1054 /* Composite data type, e.g. a table's row type */
1055 tupdesc = lookup_rowtype_tupdesc_copy(typeoid, -1);
1057 if (colaliases != NIL)
1059 int natts = tupdesc->natts;
1062 /* does the list length match the number of attributes? */
1063 if (list_length(colaliases) != natts)
1065 (errcode(ERRCODE_DATATYPE_MISMATCH),
1066 errmsg("number of aliases does not match number of columns")));
1068 /* OK, use the aliases instead */
1069 for (varattno = 0; varattno < natts; varattno++)
1071 char *label = strVal(list_nth(colaliases, varattno));
1074 namestrcpy(&(tupdesc->attrs[varattno]->attname), label);
1077 /* The tuple type is now an anonymous record type */
1078 tupdesc->tdtypeid = RECORDOID;
1079 tupdesc->tdtypmod = -1;
1082 else if (functypclass == TYPEFUNC_SCALAR)
1084 /* Base data type, i.e. scalar */
1087 /* the alias list is required for base types */
1088 if (colaliases == NIL)
1090 (errcode(ERRCODE_DATATYPE_MISMATCH),
1091 errmsg("no column alias was provided")));
1093 /* the alias list length must be 1 */
1094 if (list_length(colaliases) != 1)
1096 (errcode(ERRCODE_DATATYPE_MISMATCH),
1097 errmsg("number of aliases does not match number of columns")));
1099 /* OK, get the column alias */
1100 attname = strVal(linitial(colaliases));
1102 tupdesc = CreateTemplateTupleDesc(1, false);
1103 TupleDescInitEntry(tupdesc,
1110 else if (functypclass == TYPEFUNC_RECORD)
1112 /* XXX can't support this because typmod wasn't passed in ... */
1114 (errcode(ERRCODE_DATATYPE_MISMATCH),
1115 errmsg("could not determine row description for function returning record")));
1119 /* crummy error message, but parser should have caught this */
1120 elog(ERROR, "function in FROM has unsupported return type");