]> granicus.if.org Git - postgresql/blob - src/backend/utils/fmgr/funcapi.c
pgindent run for 9.5
[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-2015, 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  * get_func_trftypes
882  *
883  * Returns a number of transformated types used by function.
884  */
885 int
886 get_func_trftypes(HeapTuple procTup,
887                                   Oid **p_trftypes)
888 {
889         Datum           protrftypes;
890         ArrayType  *arr;
891         int                     nelems;
892         bool            isNull;
893
894         protrftypes = SysCacheGetAttr(PROCOID, procTup,
895                                                                   Anum_pg_proc_protrftypes,
896                                                                   &isNull);
897         if (!isNull)
898         {
899                 /*
900                  * We expect the arrays to be 1-D arrays of the right types; verify
901                  * that.  For the OID and char arrays, we don't need to use
902                  * deconstruct_array() since the array data is just going to look like
903                  * a C array of values.
904                  */
905                 arr = DatumGetArrayTypeP(protrftypes);  /* ensure not toasted */
906                 nelems = ARR_DIMS(arr)[0];
907                 if (ARR_NDIM(arr) != 1 ||
908                         nelems < 0 ||
909                         ARR_HASNULL(arr) ||
910                         ARR_ELEMTYPE(arr) != OIDOID)
911                         elog(ERROR, "protrftypes is not a 1-D Oid array");
912                 Assert(nelems >= ((Form_pg_proc) GETSTRUCT(procTup))->pronargs);
913                 *p_trftypes = (Oid *) palloc(nelems * sizeof(Oid));
914                 memcpy(*p_trftypes, ARR_DATA_PTR(arr),
915                            nelems * sizeof(Oid));
916
917                 return nelems;
918         }
919         else
920                 return 0;
921 }
922
923 /*
924  * get_func_input_arg_names
925  *
926  * Extract the names of input arguments only, given a function's
927  * proargnames and proargmodes entries in Datum form.
928  *
929  * Returns the number of input arguments, which is the length of the
930  * palloc'd array returned to *arg_names.  Entries for unnamed args
931  * are set to NULL.  You don't get anything if proargnames is NULL.
932  */
933 int
934 get_func_input_arg_names(Datum proargnames, Datum proargmodes,
935                                                  char ***arg_names)
936 {
937         ArrayType  *arr;
938         int                     numargs;
939         Datum      *argnames;
940         char       *argmodes;
941         char      **inargnames;
942         int                     numinargs;
943         int                     i;
944
945         /* Do nothing if null proargnames */
946         if (proargnames == PointerGetDatum(NULL))
947         {
948                 *arg_names = NULL;
949                 return 0;
950         }
951
952         /*
953          * We expect the arrays to be 1-D arrays of the right types; verify that.
954          * For proargmodes, we don't need to use deconstruct_array() since the
955          * array data is just going to look like a C array of values.
956          */
957         arr = DatumGetArrayTypeP(proargnames);          /* ensure not toasted */
958         if (ARR_NDIM(arr) != 1 ||
959                 ARR_HASNULL(arr) ||
960                 ARR_ELEMTYPE(arr) != TEXTOID)
961                 elog(ERROR, "proargnames is not a 1-D text array");
962         deconstruct_array(arr, TEXTOID, -1, false, 'i',
963                                           &argnames, NULL, &numargs);
964         if (proargmodes != PointerGetDatum(NULL))
965         {
966                 arr = DatumGetArrayTypeP(proargmodes);  /* ensure not toasted */
967                 if (ARR_NDIM(arr) != 1 ||
968                         ARR_DIMS(arr)[0] != numargs ||
969                         ARR_HASNULL(arr) ||
970                         ARR_ELEMTYPE(arr) != CHAROID)
971                         elog(ERROR, "proargmodes is not a 1-D char array");
972                 argmodes = (char *) ARR_DATA_PTR(arr);
973         }
974         else
975                 argmodes = NULL;
976
977         /* zero elements probably shouldn't happen, but handle it gracefully */
978         if (numargs <= 0)
979         {
980                 *arg_names = NULL;
981                 return 0;
982         }
983
984         /* extract input-argument names */
985         inargnames = (char **) palloc(numargs * sizeof(char *));
986         numinargs = 0;
987         for (i = 0; i < numargs; i++)
988         {
989                 if (argmodes == NULL ||
990                         argmodes[i] == PROARGMODE_IN ||
991                         argmodes[i] == PROARGMODE_INOUT ||
992                         argmodes[i] == PROARGMODE_VARIADIC)
993                 {
994                         char       *pname = TextDatumGetCString(argnames[i]);
995
996                         if (pname[0] != '\0')
997                                 inargnames[numinargs] = pname;
998                         else
999                                 inargnames[numinargs] = NULL;
1000                         numinargs++;
1001                 }
1002         }
1003
1004         *arg_names = inargnames;
1005         return numinargs;
1006 }
1007
1008
1009 /*
1010  * get_func_result_name
1011  *
1012  * If the function has exactly one output parameter, and that parameter
1013  * is named, return the name (as a palloc'd string).  Else return NULL.
1014  *
1015  * This is used to determine the default output column name for functions
1016  * returning scalar types.
1017  */
1018 char *
1019 get_func_result_name(Oid functionId)
1020 {
1021         char       *result;
1022         HeapTuple       procTuple;
1023         Datum           proargmodes;
1024         Datum           proargnames;
1025         bool            isnull;
1026         ArrayType  *arr;
1027         int                     numargs;
1028         char       *argmodes;
1029         Datum      *argnames;
1030         int                     numoutargs;
1031         int                     nargnames;
1032         int                     i;
1033
1034         /* First fetch the function's pg_proc row */
1035         procTuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(functionId));
1036         if (!HeapTupleIsValid(procTuple))
1037                 elog(ERROR, "cache lookup failed for function %u", functionId);
1038
1039         /* If there are no named OUT parameters, return NULL */
1040         if (heap_attisnull(procTuple, Anum_pg_proc_proargmodes) ||
1041                 heap_attisnull(procTuple, Anum_pg_proc_proargnames))
1042                 result = NULL;
1043         else
1044         {
1045                 /* Get the data out of the tuple */
1046                 proargmodes = SysCacheGetAttr(PROCOID, procTuple,
1047                                                                           Anum_pg_proc_proargmodes,
1048                                                                           &isnull);
1049                 Assert(!isnull);
1050                 proargnames = SysCacheGetAttr(PROCOID, procTuple,
1051                                                                           Anum_pg_proc_proargnames,
1052                                                                           &isnull);
1053                 Assert(!isnull);
1054
1055                 /*
1056                  * We expect the arrays to be 1-D arrays of the right types; verify
1057                  * that.  For the char array, we don't need to use deconstruct_array()
1058                  * since the array data is just going to look like a C array of
1059                  * values.
1060                  */
1061                 arr = DatumGetArrayTypeP(proargmodes);  /* ensure not toasted */
1062                 numargs = ARR_DIMS(arr)[0];
1063                 if (ARR_NDIM(arr) != 1 ||
1064                         numargs < 0 ||
1065                         ARR_HASNULL(arr) ||
1066                         ARR_ELEMTYPE(arr) != CHAROID)
1067                         elog(ERROR, "proargmodes is not a 1-D char array");
1068                 argmodes = (char *) ARR_DATA_PTR(arr);
1069                 arr = DatumGetArrayTypeP(proargnames);  /* ensure not toasted */
1070                 if (ARR_NDIM(arr) != 1 ||
1071                         ARR_DIMS(arr)[0] != numargs ||
1072                         ARR_HASNULL(arr) ||
1073                         ARR_ELEMTYPE(arr) != TEXTOID)
1074                         elog(ERROR, "proargnames is not a 1-D text array");
1075                 deconstruct_array(arr, TEXTOID, -1, false, 'i',
1076                                                   &argnames, NULL, &nargnames);
1077                 Assert(nargnames == numargs);
1078
1079                 /* scan for output argument(s) */
1080                 result = NULL;
1081                 numoutargs = 0;
1082                 for (i = 0; i < numargs; i++)
1083                 {
1084                         if (argmodes[i] == PROARGMODE_IN ||
1085                                 argmodes[i] == PROARGMODE_VARIADIC)
1086                                 continue;
1087                         Assert(argmodes[i] == PROARGMODE_OUT ||
1088                                    argmodes[i] == PROARGMODE_INOUT ||
1089                                    argmodes[i] == PROARGMODE_TABLE);
1090                         if (++numoutargs > 1)
1091                         {
1092                                 /* multiple out args, so forget it */
1093                                 result = NULL;
1094                                 break;
1095                         }
1096                         result = TextDatumGetCString(argnames[i]);
1097                         if (result == NULL || result[0] == '\0')
1098                         {
1099                                 /* Parameter is not named, so forget it */
1100                                 result = NULL;
1101                                 break;
1102                         }
1103                 }
1104         }
1105
1106         ReleaseSysCache(procTuple);
1107
1108         return result;
1109 }
1110
1111
1112 /*
1113  * build_function_result_tupdesc_t
1114  *
1115  * Given a pg_proc row for a function, return a tuple descriptor for the
1116  * result rowtype, or NULL if the function does not have OUT parameters.
1117  *
1118  * Note that this does not handle resolution of polymorphic types;
1119  * that is deliberate.
1120  */
1121 TupleDesc
1122 build_function_result_tupdesc_t(HeapTuple procTuple)
1123 {
1124         Form_pg_proc procform = (Form_pg_proc) GETSTRUCT(procTuple);
1125         Datum           proallargtypes;
1126         Datum           proargmodes;
1127         Datum           proargnames;
1128         bool            isnull;
1129
1130         /* Return NULL if the function isn't declared to return RECORD */
1131         if (procform->prorettype != RECORDOID)
1132                 return NULL;
1133
1134         /* If there are no OUT parameters, return NULL */
1135         if (heap_attisnull(procTuple, Anum_pg_proc_proallargtypes) ||
1136                 heap_attisnull(procTuple, Anum_pg_proc_proargmodes))
1137                 return NULL;
1138
1139         /* Get the data out of the tuple */
1140         proallargtypes = SysCacheGetAttr(PROCOID, procTuple,
1141                                                                          Anum_pg_proc_proallargtypes,
1142                                                                          &isnull);
1143         Assert(!isnull);
1144         proargmodes = SysCacheGetAttr(PROCOID, procTuple,
1145                                                                   Anum_pg_proc_proargmodes,
1146                                                                   &isnull);
1147         Assert(!isnull);
1148         proargnames = SysCacheGetAttr(PROCOID, procTuple,
1149                                                                   Anum_pg_proc_proargnames,
1150                                                                   &isnull);
1151         if (isnull)
1152                 proargnames = PointerGetDatum(NULL);    /* just to be sure */
1153
1154         return build_function_result_tupdesc_d(proallargtypes,
1155                                                                                    proargmodes,
1156                                                                                    proargnames);
1157 }
1158
1159 /*
1160  * build_function_result_tupdesc_d
1161  *
1162  * Build a RECORD function's tupledesc from the pg_proc proallargtypes,
1163  * proargmodes, and proargnames arrays.  This is split out for the
1164  * convenience of ProcedureCreate, which needs to be able to compute the
1165  * tupledesc before actually creating the function.
1166  *
1167  * Returns NULL if there are not at least two OUT or INOUT arguments.
1168  */
1169 TupleDesc
1170 build_function_result_tupdesc_d(Datum proallargtypes,
1171                                                                 Datum proargmodes,
1172                                                                 Datum proargnames)
1173 {
1174         TupleDesc       desc;
1175         ArrayType  *arr;
1176         int                     numargs;
1177         Oid                *argtypes;
1178         char       *argmodes;
1179         Datum      *argnames = NULL;
1180         Oid                *outargtypes;
1181         char      **outargnames;
1182         int                     numoutargs;
1183         int                     nargnames;
1184         int                     i;
1185
1186         /* Can't have output args if columns are null */
1187         if (proallargtypes == PointerGetDatum(NULL) ||
1188                 proargmodes == PointerGetDatum(NULL))
1189                 return NULL;
1190
1191         /*
1192          * We expect the arrays to be 1-D arrays of the right types; verify that.
1193          * For the OID and char arrays, we don't need to use deconstruct_array()
1194          * since the array data is just going to look like a C array of values.
1195          */
1196         arr = DatumGetArrayTypeP(proallargtypes);       /* ensure not toasted */
1197         numargs = ARR_DIMS(arr)[0];
1198         if (ARR_NDIM(arr) != 1 ||
1199                 numargs < 0 ||
1200                 ARR_HASNULL(arr) ||
1201                 ARR_ELEMTYPE(arr) != OIDOID)
1202                 elog(ERROR, "proallargtypes is not a 1-D Oid array");
1203         argtypes = (Oid *) ARR_DATA_PTR(arr);
1204         arr = DatumGetArrayTypeP(proargmodes);          /* ensure not toasted */
1205         if (ARR_NDIM(arr) != 1 ||
1206                 ARR_DIMS(arr)[0] != numargs ||
1207                 ARR_HASNULL(arr) ||
1208                 ARR_ELEMTYPE(arr) != CHAROID)
1209                 elog(ERROR, "proargmodes is not a 1-D char array");
1210         argmodes = (char *) ARR_DATA_PTR(arr);
1211         if (proargnames != PointerGetDatum(NULL))
1212         {
1213                 arr = DatumGetArrayTypeP(proargnames);  /* ensure not toasted */
1214                 if (ARR_NDIM(arr) != 1 ||
1215                         ARR_DIMS(arr)[0] != numargs ||
1216                         ARR_HASNULL(arr) ||
1217                         ARR_ELEMTYPE(arr) != TEXTOID)
1218                         elog(ERROR, "proargnames is not a 1-D text array");
1219                 deconstruct_array(arr, TEXTOID, -1, false, 'i',
1220                                                   &argnames, NULL, &nargnames);
1221                 Assert(nargnames == numargs);
1222         }
1223
1224         /* zero elements probably shouldn't happen, but handle it gracefully */
1225         if (numargs <= 0)
1226                 return NULL;
1227
1228         /* extract output-argument types and names */
1229         outargtypes = (Oid *) palloc(numargs * sizeof(Oid));
1230         outargnames = (char **) palloc(numargs * sizeof(char *));
1231         numoutargs = 0;
1232         for (i = 0; i < numargs; i++)
1233         {
1234                 char       *pname;
1235
1236                 if (argmodes[i] == PROARGMODE_IN ||
1237                         argmodes[i] == PROARGMODE_VARIADIC)
1238                         continue;
1239                 Assert(argmodes[i] == PROARGMODE_OUT ||
1240                            argmodes[i] == PROARGMODE_INOUT ||
1241                            argmodes[i] == PROARGMODE_TABLE);
1242                 outargtypes[numoutargs] = argtypes[i];
1243                 if (argnames)
1244                         pname = TextDatumGetCString(argnames[i]);
1245                 else
1246                         pname = NULL;
1247                 if (pname == NULL || pname[0] == '\0')
1248                 {
1249                         /* Parameter is not named, so gin up a column name */
1250                         pname = psprintf("column%d", numoutargs + 1);
1251                 }
1252                 outargnames[numoutargs] = pname;
1253                 numoutargs++;
1254         }
1255
1256         /*
1257          * If there is no output argument, or only one, the function does not
1258          * return tuples.
1259          */
1260         if (numoutargs < 2)
1261                 return NULL;
1262
1263         desc = CreateTemplateTupleDesc(numoutargs, false);
1264         for (i = 0; i < numoutargs; i++)
1265         {
1266                 TupleDescInitEntry(desc, i + 1,
1267                                                    outargnames[i],
1268                                                    outargtypes[i],
1269                                                    -1,
1270                                                    0);
1271         }
1272
1273         return desc;
1274 }
1275
1276
1277 /*
1278  * RelationNameGetTupleDesc
1279  *
1280  * Given a (possibly qualified) relation name, build a TupleDesc.
1281  *
1282  * Note: while this works as advertised, it's seldom the best way to
1283  * build a tupdesc for a function's result type.  It's kept around
1284  * only for backwards compatibility with existing user-written code.
1285  */
1286 TupleDesc
1287 RelationNameGetTupleDesc(const char *relname)
1288 {
1289         RangeVar   *relvar;
1290         Relation        rel;
1291         TupleDesc       tupdesc;
1292         List       *relname_list;
1293
1294         /* Open relation and copy the tuple description */
1295         relname_list = stringToQualifiedNameList(relname);
1296         relvar = makeRangeVarFromNameList(relname_list);
1297         rel = relation_openrv(relvar, AccessShareLock);
1298         tupdesc = CreateTupleDescCopy(RelationGetDescr(rel));
1299         relation_close(rel, AccessShareLock);
1300
1301         return tupdesc;
1302 }
1303
1304 /*
1305  * TypeGetTupleDesc
1306  *
1307  * Given a type Oid, build a TupleDesc.  (In most cases you should be
1308  * using get_call_result_type or one of its siblings instead of this
1309  * routine, so that you can handle OUT parameters, RECORD result type,
1310  * and polymorphic results.)
1311  *
1312  * If the type is composite, *and* a colaliases List is provided, *and*
1313  * the List is of natts length, use the aliases instead of the relation
1314  * attnames.  (NB: this usage is deprecated since it may result in
1315  * creation of unnecessary transient record types.)
1316  *
1317  * If the type is a base type, a single item alias List is required.
1318  */
1319 TupleDesc
1320 TypeGetTupleDesc(Oid typeoid, List *colaliases)
1321 {
1322         TypeFuncClass functypclass = get_type_func_class(typeoid);
1323         TupleDesc       tupdesc = NULL;
1324
1325         /*
1326          * Build a suitable tupledesc representing the output rows
1327          */
1328         if (functypclass == TYPEFUNC_COMPOSITE)
1329         {
1330                 /* Composite data type, e.g. a table's row type */
1331                 tupdesc = lookup_rowtype_tupdesc_copy(typeoid, -1);
1332
1333                 if (colaliases != NIL)
1334                 {
1335                         int                     natts = tupdesc->natts;
1336                         int                     varattno;
1337
1338                         /* does the list length match the number of attributes? */
1339                         if (list_length(colaliases) != natts)
1340                                 ereport(ERROR,
1341                                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
1342                                                  errmsg("number of aliases does not match number of columns")));
1343
1344                         /* OK, use the aliases instead */
1345                         for (varattno = 0; varattno < natts; varattno++)
1346                         {
1347                                 char       *label = strVal(list_nth(colaliases, varattno));
1348
1349                                 if (label != NULL)
1350                                         namestrcpy(&(tupdesc->attrs[varattno]->attname), label);
1351                         }
1352
1353                         /* The tuple type is now an anonymous record type */
1354                         tupdesc->tdtypeid = RECORDOID;
1355                         tupdesc->tdtypmod = -1;
1356                 }
1357         }
1358         else if (functypclass == TYPEFUNC_SCALAR)
1359         {
1360                 /* Base data type, i.e. scalar */
1361                 char       *attname;
1362
1363                 /* the alias list is required for base types */
1364                 if (colaliases == NIL)
1365                         ereport(ERROR,
1366                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
1367                                          errmsg("no column alias was provided")));
1368
1369                 /* the alias list length must be 1 */
1370                 if (list_length(colaliases) != 1)
1371                         ereport(ERROR,
1372                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
1373                           errmsg("number of aliases does not match number of columns")));
1374
1375                 /* OK, get the column alias */
1376                 attname = strVal(linitial(colaliases));
1377
1378                 tupdesc = CreateTemplateTupleDesc(1, false);
1379                 TupleDescInitEntry(tupdesc,
1380                                                    (AttrNumber) 1,
1381                                                    attname,
1382                                                    typeoid,
1383                                                    -1,
1384                                                    0);
1385         }
1386         else if (functypclass == TYPEFUNC_RECORD)
1387         {
1388                 /* XXX can't support this because typmod wasn't passed in ... */
1389                 ereport(ERROR,
1390                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
1391                                  errmsg("could not determine row description for function returning record")));
1392         }
1393         else
1394         {
1395                 /* crummy error message, but parser should have caught this */
1396                 elog(ERROR, "function in FROM has unsupported return type");
1397         }
1398
1399         return tupdesc;
1400 }