]> granicus.if.org Git - postgresql/blob - src/backend/utils/fmgr/funcapi.c
Update copyright for 2014
[postgresql] / src / backend / utils / fmgr / funcapi.c
1 /*-------------------------------------------------------------------------
2  *
3  * funcapi.c
4  *        Utility and convenience functions for fmgr functions that return
5  *        sets and/or composite types.
6  *
7  * Copyright (c) 2002-2014, PostgreSQL Global Development Group
8  *
9  * IDENTIFICATION
10  *        src/backend/utils/fmgr/funcapi.c
11  *
12  *-------------------------------------------------------------------------
13  */
14 #include "postgres.h"
15
16 #include "access/htup_details.h"
17 #include "catalog/namespace.h"
18 #include "catalog/pg_proc.h"
19 #include "catalog/pg_type.h"
20 #include "funcapi.h"
21 #include "nodes/nodeFuncs.h"
22 #include "parser/parse_coerce.h"
23 #include "utils/array.h"
24 #include "utils/builtins.h"
25 #include "utils/lsyscache.h"
26 #include "utils/memutils.h"
27 #include "utils/rel.h"
28 #include "utils/syscache.h"
29 #include "utils/typcache.h"
30
31
32 static void shutdown_MultiFuncCall(Datum arg);
33 static TypeFuncClass internal_get_result_type(Oid funcid,
34                                                  Node *call_expr,
35                                                  ReturnSetInfo *rsinfo,
36                                                  Oid *resultTypeId,
37                                                  TupleDesc *resultTupleDesc);
38 static bool resolve_polymorphic_tupdesc(TupleDesc tupdesc,
39                                                         oidvector *declared_args,
40                                                         Node *call_expr);
41 static TypeFuncClass get_type_func_class(Oid typid);
42
43
44 /*
45  * init_MultiFuncCall
46  * Create an empty FuncCallContext data structure
47  * and do some other basic Multi-function call setup
48  * and error checking
49  */
50 FuncCallContext *
51 init_MultiFuncCall(PG_FUNCTION_ARGS)
52 {
53         FuncCallContext *retval;
54
55         /*
56          * Bail if we're called in the wrong context
57          */
58         if (fcinfo->resultinfo == NULL || !IsA(fcinfo->resultinfo, ReturnSetInfo))
59                 ereport(ERROR,
60                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
61                                  errmsg("set-valued function called in context that cannot accept a set")));
62
63         if (fcinfo->flinfo->fn_extra == NULL)
64         {
65                 /*
66                  * First call
67                  */
68                 ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
69                 MemoryContext multi_call_ctx;
70
71                 /*
72                  * Create a suitably long-lived context to hold cross-call data
73                  */
74                 multi_call_ctx = AllocSetContextCreate(fcinfo->flinfo->fn_mcxt,
75                                                                                            "SRF multi-call context",
76                                                                                            ALLOCSET_SMALL_MINSIZE,
77                                                                                            ALLOCSET_SMALL_INITSIZE,
78                                                                                            ALLOCSET_SMALL_MAXSIZE);
79
80                 /*
81                  * Allocate suitably long-lived space and zero it
82                  */
83                 retval = (FuncCallContext *)
84                         MemoryContextAllocZero(multi_call_ctx,
85                                                                    sizeof(FuncCallContext));
86
87                 /*
88                  * initialize the elements
89                  */
90                 retval->call_cntr = 0;
91                 retval->max_calls = 0;
92                 retval->slot = NULL;
93                 retval->user_fctx = NULL;
94                 retval->attinmeta = NULL;
95                 retval->tuple_desc = NULL;
96                 retval->multi_call_memory_ctx = multi_call_ctx;
97
98                 /*
99                  * save the pointer for cross-call use
100                  */
101                 fcinfo->flinfo->fn_extra = retval;
102
103                 /*
104                  * Ensure we will get shut down cleanly if the exprcontext is not run
105                  * to completion.
106                  */
107                 RegisterExprContextCallback(rsi->econtext,
108                                                                         shutdown_MultiFuncCall,
109                                                                         PointerGetDatum(fcinfo->flinfo));
110         }
111         else
112         {
113                 /* second and subsequent calls */
114                 elog(ERROR, "init_MultiFuncCall cannot be called more than once");
115
116                 /* never reached, but keep compiler happy */
117                 retval = NULL;
118         }
119
120         return retval;
121 }
122
123 /*
124  * per_MultiFuncCall
125  *
126  * Do Multi-function per-call setup
127  */
128 FuncCallContext *
129 per_MultiFuncCall(PG_FUNCTION_ARGS)
130 {
131         FuncCallContext *retval = (FuncCallContext *) fcinfo->flinfo->fn_extra;
132
133         /*
134          * Clear the TupleTableSlot, if present.  This is for safety's sake: the
135          * Slot will be in a long-lived context (it better be, if the
136          * FuncCallContext is pointing to it), but in most usage patterns the
137          * tuples stored in it will be in the function's per-tuple context. So at
138          * the beginning of each call, the Slot will hold a dangling pointer to an
139          * already-recycled tuple.      We clear it out here.
140          *
141          * Note: use of retval->slot is obsolete as of 8.0, and we expect that it
142          * will always be NULL.  This is just here for backwards compatibility in
143          * case someone creates a slot anyway.
144          */
145         if (retval->slot != NULL)
146                 ExecClearTuple(retval->slot);
147
148         return retval;
149 }
150
151 /*
152  * end_MultiFuncCall
153  * Clean up after init_MultiFuncCall
154  */
155 void
156 end_MultiFuncCall(PG_FUNCTION_ARGS, FuncCallContext *funcctx)
157 {
158         ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
159
160         /* Deregister the shutdown callback */
161         UnregisterExprContextCallback(rsi->econtext,
162                                                                   shutdown_MultiFuncCall,
163                                                                   PointerGetDatum(fcinfo->flinfo));
164
165         /* But use it to do the real work */
166         shutdown_MultiFuncCall(PointerGetDatum(fcinfo->flinfo));
167 }
168
169 /*
170  * shutdown_MultiFuncCall
171  * Shutdown function to clean up after init_MultiFuncCall
172  */
173 static void
174 shutdown_MultiFuncCall(Datum arg)
175 {
176         FmgrInfo   *flinfo = (FmgrInfo *) DatumGetPointer(arg);
177         FuncCallContext *funcctx = (FuncCallContext *) flinfo->fn_extra;
178
179         /* unbind from flinfo */
180         flinfo->fn_extra = NULL;
181
182         /*
183          * Delete context that holds all multi-call data, including the
184          * FuncCallContext itself
185          */
186         MemoryContextDelete(funcctx->multi_call_memory_ctx);
187 }
188
189
190 /*
191  * get_call_result_type
192  *              Given a function's call info record, determine the kind of datatype
193  *              it is supposed to return.  If resultTypeId isn't NULL, *resultTypeId
194  *              receives the actual datatype OID (this is mainly useful for scalar
195  *              result types).  If resultTupleDesc isn't NULL, *resultTupleDesc
196  *              receives a pointer to a TupleDesc when the result is of a composite
197  *              type, or NULL when it's a scalar result.
198  *
199  * One hard case that this handles is resolution of actual rowtypes for
200  * functions returning RECORD (from either the function's OUT parameter
201  * list, or a ReturnSetInfo context node).      TYPEFUNC_RECORD is returned
202  * only when we couldn't resolve the actual rowtype for lack of information.
203  *
204  * The other hard case that this handles is resolution of polymorphism.
205  * We will never return polymorphic pseudotypes (ANYELEMENT etc), either
206  * as a scalar result type or as a component of a rowtype.
207  *
208  * This function is relatively expensive --- in a function returning set,
209  * try to call it only the first time through.
210  */
211 TypeFuncClass
212 get_call_result_type(FunctionCallInfo fcinfo,
213                                          Oid *resultTypeId,
214                                          TupleDesc *resultTupleDesc)
215 {
216         return internal_get_result_type(fcinfo->flinfo->fn_oid,
217                                                                         fcinfo->flinfo->fn_expr,
218                                                                         (ReturnSetInfo *) fcinfo->resultinfo,
219                                                                         resultTypeId,
220                                                                         resultTupleDesc);
221 }
222
223 /*
224  * get_expr_result_type
225  *              As above, but work from a calling expression node tree
226  */
227 TypeFuncClass
228 get_expr_result_type(Node *expr,
229                                          Oid *resultTypeId,
230                                          TupleDesc *resultTupleDesc)
231 {
232         TypeFuncClass result;
233
234         if (expr && IsA(expr, FuncExpr))
235                 result = internal_get_result_type(((FuncExpr *) expr)->funcid,
236                                                                                   expr,
237                                                                                   NULL,
238                                                                                   resultTypeId,
239                                                                                   resultTupleDesc);
240         else if (expr && IsA(expr, OpExpr))
241                 result = internal_get_result_type(get_opcode(((OpExpr *) expr)->opno),
242                                                                                   expr,
243                                                                                   NULL,
244                                                                                   resultTypeId,
245                                                                                   resultTupleDesc);
246         else
247         {
248                 /* handle as a generic expression; no chance to resolve RECORD */
249                 Oid                     typid = exprType(expr);
250
251                 if (resultTypeId)
252                         *resultTypeId = typid;
253                 if (resultTupleDesc)
254                         *resultTupleDesc = NULL;
255                 result = get_type_func_class(typid);
256                 if (result == TYPEFUNC_COMPOSITE && resultTupleDesc)
257                         *resultTupleDesc = lookup_rowtype_tupdesc_copy(typid, -1);
258         }
259
260         return result;
261 }
262
263 /*
264  * get_func_result_type
265  *              As above, but work from a function's OID only
266  *
267  * This will not be able to resolve pure-RECORD results nor polymorphism.
268  */
269 TypeFuncClass
270 get_func_result_type(Oid functionId,
271                                          Oid *resultTypeId,
272                                          TupleDesc *resultTupleDesc)
273 {
274         return internal_get_result_type(functionId,
275                                                                         NULL,
276                                                                         NULL,
277                                                                         resultTypeId,
278                                                                         resultTupleDesc);
279 }
280
281 /*
282  * internal_get_result_type -- workhorse code implementing all the above
283  *
284  * funcid must always be supplied.      call_expr and rsinfo can be NULL if not
285  * available.  We will return TYPEFUNC_RECORD, and store NULL into
286  * *resultTupleDesc, if we cannot deduce the complete result rowtype from
287  * the available information.
288  */
289 static TypeFuncClass
290 internal_get_result_type(Oid funcid,
291                                                  Node *call_expr,
292                                                  ReturnSetInfo *rsinfo,
293                                                  Oid *resultTypeId,
294                                                  TupleDesc *resultTupleDesc)
295 {
296         TypeFuncClass result;
297         HeapTuple       tp;
298         Form_pg_proc procform;
299         Oid                     rettype;
300         TupleDesc       tupdesc;
301
302         /* First fetch the function's pg_proc row to inspect its rettype */
303         tp = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
304         if (!HeapTupleIsValid(tp))
305                 elog(ERROR, "cache lookup failed for function %u", funcid);
306         procform = (Form_pg_proc) GETSTRUCT(tp);
307
308         rettype = procform->prorettype;
309
310         /* Check for OUT parameters defining a RECORD result */
311         tupdesc = build_function_result_tupdesc_t(tp);
312         if (tupdesc)
313         {
314                 /*
315                  * It has OUT parameters, so it's basically like a regular composite
316                  * type, except we have to be able to resolve any polymorphic OUT
317                  * parameters.
318                  */
319                 if (resultTypeId)
320                         *resultTypeId = rettype;
321
322                 if (resolve_polymorphic_tupdesc(tupdesc,
323                                                                                 &procform->proargtypes,
324                                                                                 call_expr))
325                 {
326                         if (tupdesc->tdtypeid == RECORDOID &&
327                                 tupdesc->tdtypmod < 0)
328                                 assign_record_type_typmod(tupdesc);
329                         if (resultTupleDesc)
330                                 *resultTupleDesc = tupdesc;
331                         result = TYPEFUNC_COMPOSITE;
332                 }
333                 else
334                 {
335                         if (resultTupleDesc)
336                                 *resultTupleDesc = NULL;
337                         result = TYPEFUNC_RECORD;
338                 }
339
340                 ReleaseSysCache(tp);
341
342                 return result;
343         }
344
345         /*
346          * If scalar polymorphic result, try to resolve it.
347          */
348         if (IsPolymorphicType(rettype))
349         {
350                 Oid                     newrettype = exprType(call_expr);
351
352                 if (newrettype == InvalidOid)   /* this probably should not happen */
353                         ereport(ERROR,
354                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
355                                          errmsg("could not determine actual result type for function \"%s\" declared to return type %s",
356                                                         NameStr(procform->proname),
357                                                         format_type_be(rettype))));
358                 rettype = newrettype;
359         }
360
361         if (resultTypeId)
362                 *resultTypeId = rettype;
363         if (resultTupleDesc)
364                 *resultTupleDesc = NULL;        /* default result */
365
366         /* Classify the result type */
367         result = get_type_func_class(rettype);
368         switch (result)
369         {
370                 case TYPEFUNC_COMPOSITE:
371                         if (resultTupleDesc)
372                                 *resultTupleDesc = lookup_rowtype_tupdesc_copy(rettype, -1);
373                         /* Named composite types can't have any polymorphic columns */
374                         break;
375                 case TYPEFUNC_SCALAR:
376                         break;
377                 case TYPEFUNC_RECORD:
378                         /* We must get the tupledesc from call context */
379                         if (rsinfo && IsA(rsinfo, ReturnSetInfo) &&
380                                 rsinfo->expectedDesc != NULL)
381                         {
382                                 result = TYPEFUNC_COMPOSITE;
383                                 if (resultTupleDesc)
384                                         *resultTupleDesc = rsinfo->expectedDesc;
385                                 /* Assume no polymorphic columns here, either */
386                         }
387                         break;
388                 default:
389                         break;
390         }
391
392         ReleaseSysCache(tp);
393
394         return result;
395 }
396
397 /*
398  * Given the result tuple descriptor for a function with OUT parameters,
399  * replace any polymorphic columns (ANYELEMENT etc) with correct data types
400  * deduced from the input arguments. Returns TRUE if able to deduce all types,
401  * FALSE if not.
402  */
403 static bool
404 resolve_polymorphic_tupdesc(TupleDesc tupdesc, oidvector *declared_args,
405                                                         Node *call_expr)
406 {
407         int                     natts = tupdesc->natts;
408         int                     nargs = declared_args->dim1;
409         bool            have_anyelement_result = false;
410         bool            have_anyarray_result = false;
411         bool            have_anyrange_result = false;
412         bool            have_anynonarray = false;
413         bool            have_anyenum = false;
414         Oid                     anyelement_type = InvalidOid;
415         Oid                     anyarray_type = InvalidOid;
416         Oid                     anyrange_type = InvalidOid;
417         Oid                     anycollation = InvalidOid;
418         int                     i;
419
420         /* See if there are any polymorphic outputs; quick out if not */
421         for (i = 0; i < natts; i++)
422         {
423                 switch (tupdesc->attrs[i]->atttypid)
424                 {
425                         case ANYELEMENTOID:
426                                 have_anyelement_result = true;
427                                 break;
428                         case ANYARRAYOID:
429                                 have_anyarray_result = true;
430                                 break;
431                         case ANYNONARRAYOID:
432                                 have_anyelement_result = true;
433                                 have_anynonarray = true;
434                                 break;
435                         case ANYENUMOID:
436                                 have_anyelement_result = true;
437                                 have_anyenum = true;
438                                 break;
439                         case ANYRANGEOID:
440                                 have_anyrange_result = true;
441                                 break;
442                         default:
443                                 break;
444                 }
445         }
446         if (!have_anyelement_result && !have_anyarray_result &&
447                 !have_anyrange_result)
448                 return true;
449
450         /*
451          * Otherwise, extract actual datatype(s) from input arguments.  (We assume
452          * the parser already validated consistency of the arguments.)
453          */
454         if (!call_expr)
455                 return false;                   /* no hope */
456
457         for (i = 0; i < nargs; i++)
458         {
459                 switch (declared_args->values[i])
460                 {
461                         case ANYELEMENTOID:
462                         case ANYNONARRAYOID:
463                         case ANYENUMOID:
464                                 if (!OidIsValid(anyelement_type))
465                                         anyelement_type = get_call_expr_argtype(call_expr, i);
466                                 break;
467                         case ANYARRAYOID:
468                                 if (!OidIsValid(anyarray_type))
469                                         anyarray_type = get_call_expr_argtype(call_expr, i);
470                                 break;
471                         case ANYRANGEOID:
472                                 if (!OidIsValid(anyrange_type))
473                                         anyrange_type = get_call_expr_argtype(call_expr, i);
474                                 break;
475                         default:
476                                 break;
477                 }
478         }
479
480         /* If nothing found, parser messed up */
481         if (!OidIsValid(anyelement_type) && !OidIsValid(anyarray_type) &&
482                 !OidIsValid(anyrange_type))
483                 return false;
484
485         /* If needed, deduce one polymorphic type from others */
486         if (have_anyelement_result && !OidIsValid(anyelement_type))
487         {
488                 if (OidIsValid(anyarray_type))
489                         anyelement_type = resolve_generic_type(ANYELEMENTOID,
490                                                                                                    anyarray_type,
491                                                                                                    ANYARRAYOID);
492                 if (OidIsValid(anyrange_type))
493                 {
494                         Oid                     subtype = resolve_generic_type(ANYELEMENTOID,
495                                                                                                            anyrange_type,
496                                                                                                            ANYRANGEOID);
497
498                         /* check for inconsistent array and range results */
499                         if (OidIsValid(anyelement_type) && anyelement_type != subtype)
500                                 return false;
501                         anyelement_type = subtype;
502                 }
503         }
504
505         if (have_anyarray_result && !OidIsValid(anyarray_type))
506                 anyarray_type = resolve_generic_type(ANYARRAYOID,
507                                                                                          anyelement_type,
508                                                                                          ANYELEMENTOID);
509
510         /*
511          * We can't deduce a range type from other polymorphic inputs, because
512          * there may be multiple range types for the same subtype.
513          */
514         if (have_anyrange_result && !OidIsValid(anyrange_type))
515                 return false;
516
517         /* Enforce ANYNONARRAY if needed */
518         if (have_anynonarray && type_is_array(anyelement_type))
519                 return false;
520
521         /* Enforce ANYENUM if needed */
522         if (have_anyenum && !type_is_enum(anyelement_type))
523                 return false;
524
525         /*
526          * Identify the collation to use for polymorphic OUT parameters. (It'll
527          * necessarily be the same for both anyelement and anyarray.)  Note that
528          * range types are not collatable, so any possible internal collation of a
529          * range type is not considered here.
530          */
531         if (OidIsValid(anyelement_type))
532                 anycollation = get_typcollation(anyelement_type);
533         else if (OidIsValid(anyarray_type))
534                 anycollation = get_typcollation(anyarray_type);
535
536         if (OidIsValid(anycollation))
537         {
538                 /*
539                  * The types are collatable, so consider whether to use a nondefault
540                  * collation.  We do so if we can identify the input collation used
541                  * for the function.
542                  */
543                 Oid                     inputcollation = exprInputCollation(call_expr);
544
545                 if (OidIsValid(inputcollation))
546                         anycollation = inputcollation;
547         }
548
549         /* And finally replace the tuple column types as needed */
550         for (i = 0; i < natts; i++)
551         {
552                 switch (tupdesc->attrs[i]->atttypid)
553                 {
554                         case ANYELEMENTOID:
555                         case ANYNONARRAYOID:
556                         case ANYENUMOID:
557                                 TupleDescInitEntry(tupdesc, i + 1,
558                                                                    NameStr(tupdesc->attrs[i]->attname),
559                                                                    anyelement_type,
560                                                                    -1,
561                                                                    0);
562                                 TupleDescInitEntryCollation(tupdesc, i + 1, anycollation);
563                                 break;
564                         case ANYARRAYOID:
565                                 TupleDescInitEntry(tupdesc, i + 1,
566                                                                    NameStr(tupdesc->attrs[i]->attname),
567                                                                    anyarray_type,
568                                                                    -1,
569                                                                    0);
570                                 TupleDescInitEntryCollation(tupdesc, i + 1, anycollation);
571                                 break;
572                         case ANYRANGEOID:
573                                 TupleDescInitEntry(tupdesc, i + 1,
574                                                                    NameStr(tupdesc->attrs[i]->attname),
575                                                                    anyrange_type,
576                                                                    -1,
577                                                                    0);
578                                 /* no collation should be attached to a range type */
579                                 break;
580                         default:
581                                 break;
582                 }
583         }
584
585         return true;
586 }
587
588 /*
589  * Given the declared argument types and modes for a function, replace any
590  * polymorphic types (ANYELEMENT etc) with correct data types deduced from the
591  * input arguments.  Returns TRUE if able to deduce all types, FALSE if not.
592  * This is the same logic as resolve_polymorphic_tupdesc, but with a different
593  * argument representation.
594  *
595  * argmodes may be NULL, in which case all arguments are assumed to be IN mode.
596  */
597 bool
598 resolve_polymorphic_argtypes(int numargs, Oid *argtypes, char *argmodes,
599                                                          Node *call_expr)
600 {
601         bool            have_anyelement_result = false;
602         bool            have_anyarray_result = false;
603         bool            have_anyrange_result = false;
604         Oid                     anyelement_type = InvalidOid;
605         Oid                     anyarray_type = InvalidOid;
606         Oid                     anyrange_type = InvalidOid;
607         int                     inargno;
608         int                     i;
609
610         /* First pass: resolve polymorphic inputs, check for outputs */
611         inargno = 0;
612         for (i = 0; i < numargs; i++)
613         {
614                 char            argmode = argmodes ? argmodes[i] : PROARGMODE_IN;
615
616                 switch (argtypes[i])
617                 {
618                         case ANYELEMENTOID:
619                         case ANYNONARRAYOID:
620                         case ANYENUMOID:
621                                 if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
622                                         have_anyelement_result = true;
623                                 else
624                                 {
625                                         if (!OidIsValid(anyelement_type))
626                                         {
627                                                 anyelement_type = get_call_expr_argtype(call_expr,
628                                                                                                                                 inargno);
629                                                 if (!OidIsValid(anyelement_type))
630                                                         return false;
631                                         }
632                                         argtypes[i] = anyelement_type;
633                                 }
634                                 break;
635                         case ANYARRAYOID:
636                                 if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
637                                         have_anyarray_result = true;
638                                 else
639                                 {
640                                         if (!OidIsValid(anyarray_type))
641                                         {
642                                                 anyarray_type = get_call_expr_argtype(call_expr,
643                                                                                                                           inargno);
644                                                 if (!OidIsValid(anyarray_type))
645                                                         return false;
646                                         }
647                                         argtypes[i] = anyarray_type;
648                                 }
649                                 break;
650                         case ANYRANGEOID:
651                                 if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
652                                         have_anyrange_result = true;
653                                 else
654                                 {
655                                         if (!OidIsValid(anyrange_type))
656                                         {
657                                                 anyrange_type = get_call_expr_argtype(call_expr,
658                                                                                                                           inargno);
659                                                 if (!OidIsValid(anyrange_type))
660                                                         return false;
661                                         }
662                                         argtypes[i] = anyrange_type;
663                                 }
664                                 break;
665                         default:
666                                 break;
667                 }
668                 if (argmode != PROARGMODE_OUT && argmode != PROARGMODE_TABLE)
669                         inargno++;
670         }
671
672         /* Done? */
673         if (!have_anyelement_result && !have_anyarray_result &&
674                 !have_anyrange_result)
675                 return true;
676
677         /* If no input polymorphics, parser messed up */
678         if (!OidIsValid(anyelement_type) && !OidIsValid(anyarray_type) &&
679                 !OidIsValid(anyrange_type))
680                 return false;
681
682         /* If needed, deduce one polymorphic type from others */
683         if (have_anyelement_result && !OidIsValid(anyelement_type))
684         {
685                 if (OidIsValid(anyarray_type))
686                         anyelement_type = resolve_generic_type(ANYELEMENTOID,
687                                                                                                    anyarray_type,
688                                                                                                    ANYARRAYOID);
689                 if (OidIsValid(anyrange_type))
690                 {
691                         Oid                     subtype = resolve_generic_type(ANYELEMENTOID,
692                                                                                                            anyrange_type,
693                                                                                                            ANYRANGEOID);
694
695                         /* check for inconsistent array and range results */
696                         if (OidIsValid(anyelement_type) && anyelement_type != subtype)
697                                 return false;
698                         anyelement_type = subtype;
699                 }
700         }
701
702         if (have_anyarray_result && !OidIsValid(anyarray_type))
703                 anyarray_type = resolve_generic_type(ANYARRAYOID,
704                                                                                          anyelement_type,
705                                                                                          ANYELEMENTOID);
706
707         /*
708          * We can't deduce a range type from other polymorphic inputs, because
709          * there may be multiple range types for the same subtype.
710          */
711         if (have_anyrange_result && !OidIsValid(anyrange_type))
712                 return false;
713
714         /* XXX do we need to enforce ANYNONARRAY or ANYENUM here?  I think not */
715
716         /* And finally replace the output column types as needed */
717         for (i = 0; i < numargs; i++)
718         {
719                 switch (argtypes[i])
720                 {
721                         case ANYELEMENTOID:
722                         case ANYNONARRAYOID:
723                         case ANYENUMOID:
724                                 argtypes[i] = anyelement_type;
725                                 break;
726                         case ANYARRAYOID:
727                                 argtypes[i] = anyarray_type;
728                                 break;
729                         case ANYRANGEOID:
730                                 argtypes[i] = anyrange_type;
731                                 break;
732                         default:
733                                 break;
734                 }
735         }
736
737         return true;
738 }
739
740 /*
741  * get_type_func_class
742  *              Given the type OID, obtain its TYPEFUNC classification.
743  *
744  * This is intended to centralize a bunch of formerly ad-hoc code for
745  * classifying types.  The categories used here are useful for deciding
746  * how to handle functions returning the datatype.
747  */
748 static TypeFuncClass
749 get_type_func_class(Oid typid)
750 {
751         switch (get_typtype(typid))
752         {
753                 case TYPTYPE_COMPOSITE:
754                         return TYPEFUNC_COMPOSITE;
755                 case TYPTYPE_BASE:
756                 case TYPTYPE_DOMAIN:
757                 case TYPTYPE_ENUM:
758                 case TYPTYPE_RANGE:
759                         return TYPEFUNC_SCALAR;
760                 case TYPTYPE_PSEUDO:
761                         if (typid == RECORDOID)
762                                 return TYPEFUNC_RECORD;
763
764                         /*
765                          * We treat VOID and CSTRING as legitimate scalar datatypes,
766                          * mostly for the convenience of the JDBC driver (which wants to
767                          * be able to do "SELECT * FROM foo()" for all legitimately
768                          * user-callable functions).
769                          */
770                         if (typid == VOIDOID || typid == CSTRINGOID)
771                                 return TYPEFUNC_SCALAR;
772                         return TYPEFUNC_OTHER;
773         }
774         /* shouldn't get here, probably */
775         return TYPEFUNC_OTHER;
776 }
777
778
779 /*
780  * get_func_arg_info
781  *
782  * Fetch info about the argument types, names, and IN/OUT modes from the
783  * pg_proc tuple.  Return value is the total number of arguments.
784  * Other results are palloc'd.  *p_argtypes is always filled in, but
785  * *p_argnames and *p_argmodes will be set NULL in the default cases
786  * (no names, and all IN arguments, respectively).
787  *
788  * Note that this function simply fetches what is in the pg_proc tuple;
789  * it doesn't do any interpretation of polymorphic types.
790  */
791 int
792 get_func_arg_info(HeapTuple procTup,
793                                   Oid **p_argtypes, char ***p_argnames, char **p_argmodes)
794 {
795         Form_pg_proc procStruct = (Form_pg_proc) GETSTRUCT(procTup);
796         Datum           proallargtypes;
797         Datum           proargmodes;
798         Datum           proargnames;
799         bool            isNull;
800         ArrayType  *arr;
801         int                     numargs;
802         Datum      *elems;
803         int                     nelems;
804         int                     i;
805
806         /* First discover the total number of parameters and get their types */
807         proallargtypes = SysCacheGetAttr(PROCOID, procTup,
808                                                                          Anum_pg_proc_proallargtypes,
809                                                                          &isNull);
810         if (!isNull)
811         {
812                 /*
813                  * We expect the arrays to be 1-D arrays of the right types; verify
814                  * that.  For the OID and char arrays, we don't need to use
815                  * deconstruct_array() since the array data is just going to look like
816                  * a C array of values.
817                  */
818                 arr = DatumGetArrayTypeP(proallargtypes);               /* ensure not toasted */
819                 numargs = ARR_DIMS(arr)[0];
820                 if (ARR_NDIM(arr) != 1 ||
821                         numargs < 0 ||
822                         ARR_HASNULL(arr) ||
823                         ARR_ELEMTYPE(arr) != OIDOID)
824                         elog(ERROR, "proallargtypes is not a 1-D Oid array");
825                 Assert(numargs >= procStruct->pronargs);
826                 *p_argtypes = (Oid *) palloc(numargs * sizeof(Oid));
827                 memcpy(*p_argtypes, ARR_DATA_PTR(arr),
828                            numargs * sizeof(Oid));
829         }
830         else
831         {
832                 /* If no proallargtypes, use proargtypes */
833                 numargs = procStruct->proargtypes.dim1;
834                 Assert(numargs == procStruct->pronargs);
835                 *p_argtypes = (Oid *) palloc(numargs * sizeof(Oid));
836                 memcpy(*p_argtypes, procStruct->proargtypes.values,
837                            numargs * sizeof(Oid));
838         }
839
840         /* Get argument names, if available */
841         proargnames = SysCacheGetAttr(PROCOID, procTup,
842                                                                   Anum_pg_proc_proargnames,
843                                                                   &isNull);
844         if (isNull)
845                 *p_argnames = NULL;
846         else
847         {
848                 deconstruct_array(DatumGetArrayTypeP(proargnames),
849                                                   TEXTOID, -1, false, 'i',
850                                                   &elems, NULL, &nelems);
851                 if (nelems != numargs)  /* should not happen */
852                         elog(ERROR, "proargnames must have the same number of elements as the function has arguments");
853                 *p_argnames = (char **) palloc(sizeof(char *) * numargs);
854                 for (i = 0; i < numargs; i++)
855                         (*p_argnames)[i] = TextDatumGetCString(elems[i]);
856         }
857
858         /* Get argument modes, if available */
859         proargmodes = SysCacheGetAttr(PROCOID, procTup,
860                                                                   Anum_pg_proc_proargmodes,
861                                                                   &isNull);
862         if (isNull)
863                 *p_argmodes = NULL;
864         else
865         {
866                 arr = DatumGetArrayTypeP(proargmodes);  /* ensure not toasted */
867                 if (ARR_NDIM(arr) != 1 ||
868                         ARR_DIMS(arr)[0] != numargs ||
869                         ARR_HASNULL(arr) ||
870                         ARR_ELEMTYPE(arr) != CHAROID)
871                         elog(ERROR, "proargmodes is not a 1-D char array");
872                 *p_argmodes = (char *) palloc(numargs * sizeof(char));
873                 memcpy(*p_argmodes, ARR_DATA_PTR(arr),
874                            numargs * sizeof(char));
875         }
876
877         return numargs;
878 }
879
880
881 /*
882  * get_func_input_arg_names
883  *
884  * Extract the names of input arguments only, given a function's
885  * proargnames and proargmodes entries in Datum form.
886  *
887  * Returns the number of input arguments, which is the length of the
888  * palloc'd array returned to *arg_names.  Entries for unnamed args
889  * are set to NULL.  You don't get anything if proargnames is NULL.
890  */
891 int
892 get_func_input_arg_names(Datum proargnames, Datum proargmodes,
893                                                  char ***arg_names)
894 {
895         ArrayType  *arr;
896         int                     numargs;
897         Datum      *argnames;
898         char       *argmodes;
899         char      **inargnames;
900         int                     numinargs;
901         int                     i;
902
903         /* Do nothing if null proargnames */
904         if (proargnames == PointerGetDatum(NULL))
905         {
906                 *arg_names = NULL;
907                 return 0;
908         }
909
910         /*
911          * We expect the arrays to be 1-D arrays of the right types; verify that.
912          * For proargmodes, we don't need to use deconstruct_array() since the
913          * array data is just going to look like a C array of values.
914          */
915         arr = DatumGetArrayTypeP(proargnames);          /* ensure not toasted */
916         if (ARR_NDIM(arr) != 1 ||
917                 ARR_HASNULL(arr) ||
918                 ARR_ELEMTYPE(arr) != TEXTOID)
919                 elog(ERROR, "proargnames is not a 1-D text array");
920         deconstruct_array(arr, TEXTOID, -1, false, 'i',
921                                           &argnames, NULL, &numargs);
922         if (proargmodes != PointerGetDatum(NULL))
923         {
924                 arr = DatumGetArrayTypeP(proargmodes);  /* ensure not toasted */
925                 if (ARR_NDIM(arr) != 1 ||
926                         ARR_DIMS(arr)[0] != numargs ||
927                         ARR_HASNULL(arr) ||
928                         ARR_ELEMTYPE(arr) != CHAROID)
929                         elog(ERROR, "proargmodes is not a 1-D char array");
930                 argmodes = (char *) ARR_DATA_PTR(arr);
931         }
932         else
933                 argmodes = NULL;
934
935         /* zero elements probably shouldn't happen, but handle it gracefully */
936         if (numargs <= 0)
937         {
938                 *arg_names = NULL;
939                 return 0;
940         }
941
942         /* extract input-argument names */
943         inargnames = (char **) palloc(numargs * sizeof(char *));
944         numinargs = 0;
945         for (i = 0; i < numargs; i++)
946         {
947                 if (argmodes == NULL ||
948                         argmodes[i] == PROARGMODE_IN ||
949                         argmodes[i] == PROARGMODE_INOUT ||
950                         argmodes[i] == PROARGMODE_VARIADIC)
951                 {
952                         char       *pname = TextDatumGetCString(argnames[i]);
953
954                         if (pname[0] != '\0')
955                                 inargnames[numinargs] = pname;
956                         else
957                                 inargnames[numinargs] = NULL;
958                         numinargs++;
959                 }
960         }
961
962         *arg_names = inargnames;
963         return numinargs;
964 }
965
966
967 /*
968  * get_func_result_name
969  *
970  * If the function has exactly one output parameter, and that parameter
971  * is named, return the name (as a palloc'd string).  Else return NULL.
972  *
973  * This is used to determine the default output column name for functions
974  * returning scalar types.
975  */
976 char *
977 get_func_result_name(Oid functionId)
978 {
979         char       *result;
980         HeapTuple       procTuple;
981         Datum           proargmodes;
982         Datum           proargnames;
983         bool            isnull;
984         ArrayType  *arr;
985         int                     numargs;
986         char       *argmodes;
987         Datum      *argnames;
988         int                     numoutargs;
989         int                     nargnames;
990         int                     i;
991
992         /* First fetch the function's pg_proc row */
993         procTuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(functionId));
994         if (!HeapTupleIsValid(procTuple))
995                 elog(ERROR, "cache lookup failed for function %u", functionId);
996
997         /* If there are no named OUT parameters, return NULL */
998         if (heap_attisnull(procTuple, Anum_pg_proc_proargmodes) ||
999                 heap_attisnull(procTuple, Anum_pg_proc_proargnames))
1000                 result = NULL;
1001         else
1002         {
1003                 /* Get the data out of the tuple */
1004                 proargmodes = SysCacheGetAttr(PROCOID, procTuple,
1005                                                                           Anum_pg_proc_proargmodes,
1006                                                                           &isnull);
1007                 Assert(!isnull);
1008                 proargnames = SysCacheGetAttr(PROCOID, procTuple,
1009                                                                           Anum_pg_proc_proargnames,
1010                                                                           &isnull);
1011                 Assert(!isnull);
1012
1013                 /*
1014                  * We expect the arrays to be 1-D arrays of the right types; verify
1015                  * that.  For the char array, we don't need to use deconstruct_array()
1016                  * since the array data is just going to look like a C array of
1017                  * values.
1018                  */
1019                 arr = DatumGetArrayTypeP(proargmodes);  /* ensure not toasted */
1020                 numargs = ARR_DIMS(arr)[0];
1021                 if (ARR_NDIM(arr) != 1 ||
1022                         numargs < 0 ||
1023                         ARR_HASNULL(arr) ||
1024                         ARR_ELEMTYPE(arr) != CHAROID)
1025                         elog(ERROR, "proargmodes is not a 1-D char array");
1026                 argmodes = (char *) ARR_DATA_PTR(arr);
1027                 arr = DatumGetArrayTypeP(proargnames);  /* ensure not toasted */
1028                 if (ARR_NDIM(arr) != 1 ||
1029                         ARR_DIMS(arr)[0] != numargs ||
1030                         ARR_HASNULL(arr) ||
1031                         ARR_ELEMTYPE(arr) != TEXTOID)
1032                         elog(ERROR, "proargnames is not a 1-D text array");
1033                 deconstruct_array(arr, TEXTOID, -1, false, 'i',
1034                                                   &argnames, NULL, &nargnames);
1035                 Assert(nargnames == numargs);
1036
1037                 /* scan for output argument(s) */
1038                 result = NULL;
1039                 numoutargs = 0;
1040                 for (i = 0; i < numargs; i++)
1041                 {
1042                         if (argmodes[i] == PROARGMODE_IN ||
1043                                 argmodes[i] == PROARGMODE_VARIADIC)
1044                                 continue;
1045                         Assert(argmodes[i] == PROARGMODE_OUT ||
1046                                    argmodes[i] == PROARGMODE_INOUT ||
1047                                    argmodes[i] == PROARGMODE_TABLE);
1048                         if (++numoutargs > 1)
1049                         {
1050                                 /* multiple out args, so forget it */
1051                                 result = NULL;
1052                                 break;
1053                         }
1054                         result = TextDatumGetCString(argnames[i]);
1055                         if (result == NULL || result[0] == '\0')
1056                         {
1057                                 /* Parameter is not named, so forget it */
1058                                 result = NULL;
1059                                 break;
1060                         }
1061                 }
1062         }
1063
1064         ReleaseSysCache(procTuple);
1065
1066         return result;
1067 }
1068
1069
1070 /*
1071  * build_function_result_tupdesc_t
1072  *
1073  * Given a pg_proc row for a function, return a tuple descriptor for the
1074  * result rowtype, or NULL if the function does not have OUT parameters.
1075  *
1076  * Note that this does not handle resolution of polymorphic types;
1077  * that is deliberate.
1078  */
1079 TupleDesc
1080 build_function_result_tupdesc_t(HeapTuple procTuple)
1081 {
1082         Form_pg_proc procform = (Form_pg_proc) GETSTRUCT(procTuple);
1083         Datum           proallargtypes;
1084         Datum           proargmodes;
1085         Datum           proargnames;
1086         bool            isnull;
1087
1088         /* Return NULL if the function isn't declared to return RECORD */
1089         if (procform->prorettype != RECORDOID)
1090                 return NULL;
1091
1092         /* If there are no OUT parameters, return NULL */
1093         if (heap_attisnull(procTuple, Anum_pg_proc_proallargtypes) ||
1094                 heap_attisnull(procTuple, Anum_pg_proc_proargmodes))
1095                 return NULL;
1096
1097         /* Get the data out of the tuple */
1098         proallargtypes = SysCacheGetAttr(PROCOID, procTuple,
1099                                                                          Anum_pg_proc_proallargtypes,
1100                                                                          &isnull);
1101         Assert(!isnull);
1102         proargmodes = SysCacheGetAttr(PROCOID, procTuple,
1103                                                                   Anum_pg_proc_proargmodes,
1104                                                                   &isnull);
1105         Assert(!isnull);
1106         proargnames = SysCacheGetAttr(PROCOID, procTuple,
1107                                                                   Anum_pg_proc_proargnames,
1108                                                                   &isnull);
1109         if (isnull)
1110                 proargnames = PointerGetDatum(NULL);    /* just to be sure */
1111
1112         return build_function_result_tupdesc_d(proallargtypes,
1113                                                                                    proargmodes,
1114                                                                                    proargnames);
1115 }
1116
1117 /*
1118  * build_function_result_tupdesc_d
1119  *
1120  * Build a RECORD function's tupledesc from the pg_proc proallargtypes,
1121  * proargmodes, and proargnames arrays.  This is split out for the
1122  * convenience of ProcedureCreate, which needs to be able to compute the
1123  * tupledesc before actually creating the function.
1124  *
1125  * Returns NULL if there are not at least two OUT or INOUT arguments.
1126  */
1127 TupleDesc
1128 build_function_result_tupdesc_d(Datum proallargtypes,
1129                                                                 Datum proargmodes,
1130                                                                 Datum proargnames)
1131 {
1132         TupleDesc       desc;
1133         ArrayType  *arr;
1134         int                     numargs;
1135         Oid                *argtypes;
1136         char       *argmodes;
1137         Datum      *argnames = NULL;
1138         Oid                *outargtypes;
1139         char      **outargnames;
1140         int                     numoutargs;
1141         int                     nargnames;
1142         int                     i;
1143
1144         /* Can't have output args if columns are null */
1145         if (proallargtypes == PointerGetDatum(NULL) ||
1146                 proargmodes == PointerGetDatum(NULL))
1147                 return NULL;
1148
1149         /*
1150          * We expect the arrays to be 1-D arrays of the right types; verify that.
1151          * For the OID and char arrays, we don't need to use deconstruct_array()
1152          * since the array data is just going to look like a C array of values.
1153          */
1154         arr = DatumGetArrayTypeP(proallargtypes);       /* ensure not toasted */
1155         numargs = ARR_DIMS(arr)[0];
1156         if (ARR_NDIM(arr) != 1 ||
1157                 numargs < 0 ||
1158                 ARR_HASNULL(arr) ||
1159                 ARR_ELEMTYPE(arr) != OIDOID)
1160                 elog(ERROR, "proallargtypes is not a 1-D Oid array");
1161         argtypes = (Oid *) ARR_DATA_PTR(arr);
1162         arr = DatumGetArrayTypeP(proargmodes);          /* ensure not toasted */
1163         if (ARR_NDIM(arr) != 1 ||
1164                 ARR_DIMS(arr)[0] != numargs ||
1165                 ARR_HASNULL(arr) ||
1166                 ARR_ELEMTYPE(arr) != CHAROID)
1167                 elog(ERROR, "proargmodes is not a 1-D char array");
1168         argmodes = (char *) ARR_DATA_PTR(arr);
1169         if (proargnames != PointerGetDatum(NULL))
1170         {
1171                 arr = DatumGetArrayTypeP(proargnames);  /* ensure not toasted */
1172                 if (ARR_NDIM(arr) != 1 ||
1173                         ARR_DIMS(arr)[0] != numargs ||
1174                         ARR_HASNULL(arr) ||
1175                         ARR_ELEMTYPE(arr) != TEXTOID)
1176                         elog(ERROR, "proargnames is not a 1-D text array");
1177                 deconstruct_array(arr, TEXTOID, -1, false, 'i',
1178                                                   &argnames, NULL, &nargnames);
1179                 Assert(nargnames == numargs);
1180         }
1181
1182         /* zero elements probably shouldn't happen, but handle it gracefully */
1183         if (numargs <= 0)
1184                 return NULL;
1185
1186         /* extract output-argument types and names */
1187         outargtypes = (Oid *) palloc(numargs * sizeof(Oid));
1188         outargnames = (char **) palloc(numargs * sizeof(char *));
1189         numoutargs = 0;
1190         for (i = 0; i < numargs; i++)
1191         {
1192                 char       *pname;
1193
1194                 if (argmodes[i] == PROARGMODE_IN ||
1195                         argmodes[i] == PROARGMODE_VARIADIC)
1196                         continue;
1197                 Assert(argmodes[i] == PROARGMODE_OUT ||
1198                            argmodes[i] == PROARGMODE_INOUT ||
1199                            argmodes[i] == PROARGMODE_TABLE);
1200                 outargtypes[numoutargs] = argtypes[i];
1201                 if (argnames)
1202                         pname = TextDatumGetCString(argnames[i]);
1203                 else
1204                         pname = NULL;
1205                 if (pname == NULL || pname[0] == '\0')
1206                 {
1207                         /* Parameter is not named, so gin up a column name */
1208                         pname = psprintf("column%d", numoutargs + 1);
1209                 }
1210                 outargnames[numoutargs] = pname;
1211                 numoutargs++;
1212         }
1213
1214         /*
1215          * If there is no output argument, or only one, the function does not
1216          * return tuples.
1217          */
1218         if (numoutargs < 2)
1219                 return NULL;
1220
1221         desc = CreateTemplateTupleDesc(numoutargs, false);
1222         for (i = 0; i < numoutargs; i++)
1223         {
1224                 TupleDescInitEntry(desc, i + 1,
1225                                                    outargnames[i],
1226                                                    outargtypes[i],
1227                                                    -1,
1228                                                    0);
1229         }
1230
1231         return desc;
1232 }
1233
1234
1235 /*
1236  * RelationNameGetTupleDesc
1237  *
1238  * Given a (possibly qualified) relation name, build a TupleDesc.
1239  *
1240  * Note: while this works as advertised, it's seldom the best way to
1241  * build a tupdesc for a function's result type.  It's kept around
1242  * only for backwards compatibility with existing user-written code.
1243  */
1244 TupleDesc
1245 RelationNameGetTupleDesc(const char *relname)
1246 {
1247         RangeVar   *relvar;
1248         Relation        rel;
1249         TupleDesc       tupdesc;
1250         List       *relname_list;
1251
1252         /* Open relation and copy the tuple description */
1253         relname_list = stringToQualifiedNameList(relname);
1254         relvar = makeRangeVarFromNameList(relname_list);
1255         rel = relation_openrv(relvar, AccessShareLock);
1256         tupdesc = CreateTupleDescCopy(RelationGetDescr(rel));
1257         relation_close(rel, AccessShareLock);
1258
1259         return tupdesc;
1260 }
1261
1262 /*
1263  * TypeGetTupleDesc
1264  *
1265  * Given a type Oid, build a TupleDesc.  (In most cases you should be
1266  * using get_call_result_type or one of its siblings instead of this
1267  * routine, so that you can handle OUT parameters, RECORD result type,
1268  * and polymorphic results.)
1269  *
1270  * If the type is composite, *and* a colaliases List is provided, *and*
1271  * the List is of natts length, use the aliases instead of the relation
1272  * attnames.  (NB: this usage is deprecated since it may result in
1273  * creation of unnecessary transient record types.)
1274  *
1275  * If the type is a base type, a single item alias List is required.
1276  */
1277 TupleDesc
1278 TypeGetTupleDesc(Oid typeoid, List *colaliases)
1279 {
1280         TypeFuncClass functypclass = get_type_func_class(typeoid);
1281         TupleDesc       tupdesc = NULL;
1282
1283         /*
1284          * Build a suitable tupledesc representing the output rows
1285          */
1286         if (functypclass == TYPEFUNC_COMPOSITE)
1287         {
1288                 /* Composite data type, e.g. a table's row type */
1289                 tupdesc = lookup_rowtype_tupdesc_copy(typeoid, -1);
1290
1291                 if (colaliases != NIL)
1292                 {
1293                         int                     natts = tupdesc->natts;
1294                         int                     varattno;
1295
1296                         /* does the list length match the number of attributes? */
1297                         if (list_length(colaliases) != natts)
1298                                 ereport(ERROR,
1299                                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
1300                                                  errmsg("number of aliases does not match number of columns")));
1301
1302                         /* OK, use the aliases instead */
1303                         for (varattno = 0; varattno < natts; varattno++)
1304                         {
1305                                 char       *label = strVal(list_nth(colaliases, varattno));
1306
1307                                 if (label != NULL)
1308                                         namestrcpy(&(tupdesc->attrs[varattno]->attname), label);
1309                         }
1310
1311                         /* The tuple type is now an anonymous record type */
1312                         tupdesc->tdtypeid = RECORDOID;
1313                         tupdesc->tdtypmod = -1;
1314                 }
1315         }
1316         else if (functypclass == TYPEFUNC_SCALAR)
1317         {
1318                 /* Base data type, i.e. scalar */
1319                 char       *attname;
1320
1321                 /* the alias list is required for base types */
1322                 if (colaliases == NIL)
1323                         ereport(ERROR,
1324                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
1325                                          errmsg("no column alias was provided")));
1326
1327                 /* the alias list length must be 1 */
1328                 if (list_length(colaliases) != 1)
1329                         ereport(ERROR,
1330                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
1331                           errmsg("number of aliases does not match number of columns")));
1332
1333                 /* OK, get the column alias */
1334                 attname = strVal(linitial(colaliases));
1335
1336                 tupdesc = CreateTemplateTupleDesc(1, false);
1337                 TupleDescInitEntry(tupdesc,
1338                                                    (AttrNumber) 1,
1339                                                    attname,
1340                                                    typeoid,
1341                                                    -1,
1342                                                    0);
1343         }
1344         else if (functypclass == TYPEFUNC_RECORD)
1345         {
1346                 /* XXX can't support this because typmod wasn't passed in ... */
1347                 ereport(ERROR,
1348                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
1349                                  errmsg("could not determine row description for function returning record")));
1350         }
1351         else
1352         {
1353                 /* crummy error message, but parser should have caught this */
1354                 elog(ERROR, "function in FROM has unsupported return type");
1355         }
1356
1357         return tupdesc;
1358 }