]> granicus.if.org Git - postgresql/blob - src/backend/utils/fmgr/funcapi.c
Implement SQL-spec RETURNS TABLE syntax for functions.
[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-2008, PostgreSQL Global Development Group
8  *
9  * IDENTIFICATION
10  *        $PostgreSQL: pgsql/src/backend/utils/fmgr/funcapi.c,v 1.41 2008/07/18 03:32:52 tgl Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14 #include "postgres.h"
15
16 #include "access/heapam.h"
17 #include "catalog/namespace.h"
18 #include "catalog/pg_proc.h"
19 #include "catalog/pg_type.h"
20 #include "funcapi.h"
21 #include "parser/parse_coerce.h"
22 #include "parser/parse_expr.h"
23 #include "utils/array.h"
24 #include "utils/builtins.h"
25 #include "utils/lsyscache.h"
26 #include "utils/memutils.h"
27 #include "utils/syscache.h"
28 #include "utils/typcache.h"
29
30
31 static void shutdown_MultiFuncCall(Datum arg);
32 static TypeFuncClass internal_get_result_type(Oid funcid,
33                                                  Node *call_expr,
34                                                  ReturnSetInfo *rsinfo,
35                                                  Oid *resultTypeId,
36                                                  TupleDesc *resultTupleDesc);
37 static bool resolve_polymorphic_tupdesc(TupleDesc tupdesc,
38                                                         oidvector *declared_args,
39                                                         Node *call_expr);
40 static TypeFuncClass get_type_func_class(Oid typid);
41
42
43 /*
44  * init_MultiFuncCall
45  * Create an empty FuncCallContext data structure
46  * and do some other basic Multi-function call setup
47  * and error checking
48  */
49 FuncCallContext *
50 init_MultiFuncCall(PG_FUNCTION_ARGS)
51 {
52         FuncCallContext *retval;
53
54         /*
55          * Bail if we're called in the wrong context
56          */
57         if (fcinfo->resultinfo == NULL || !IsA(fcinfo->resultinfo, ReturnSetInfo))
58                 ereport(ERROR,
59                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
60                                  errmsg("set-valued function called in context that cannot accept a set")));
61
62         if (fcinfo->flinfo->fn_extra == NULL)
63         {
64                 /*
65                  * First call
66                  */
67                 ReturnSetInfo  *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
68                 MemoryContext   multi_call_ctx;
69
70                 /*
71                  * Create a suitably long-lived context to hold cross-call data
72                  */
73                 multi_call_ctx = AllocSetContextCreate(fcinfo->flinfo->fn_mcxt,
74                                                                                            "SRF multi-call context",
75                                                                                            ALLOCSET_SMALL_MINSIZE,
76                                                                                            ALLOCSET_SMALL_INITSIZE,
77                                                                                            ALLOCSET_SMALL_MAXSIZE);
78
79                 /*
80                  * Allocate suitably long-lived space and zero it
81                  */
82                 retval = (FuncCallContext *)
83                         MemoryContextAllocZero(multi_call_ctx,
84                                                                    sizeof(FuncCallContext));
85
86                 /*
87                  * initialize the elements
88                  */
89                 retval->call_cntr = 0;
90                 retval->max_calls = 0;
91                 retval->slot = NULL;
92                 retval->user_fctx = NULL;
93                 retval->attinmeta = NULL;
94                 retval->tuple_desc = NULL;
95                 retval->multi_call_memory_ctx = multi_call_ctx;
96
97                 /*
98                  * save the pointer for cross-call use
99                  */
100                 fcinfo->flinfo->fn_extra = retval;
101
102                 /*
103                  * Ensure we will get shut down cleanly if the exprcontext is not run
104                  * to completion.
105                  */
106                 RegisterExprContextCallback(rsi->econtext,
107                                                                         shutdown_MultiFuncCall,
108                                                                         PointerGetDatum(fcinfo->flinfo));
109         }
110         else
111         {
112                 /* second and subsequent calls */
113                 elog(ERROR, "init_MultiFuncCall cannot be called more than once");
114
115                 /* never reached, but keep compiler happy */
116                 retval = NULL;
117         }
118
119         return retval;
120 }
121
122 /*
123  * per_MultiFuncCall
124  *
125  * Do Multi-function per-call setup
126  */
127 FuncCallContext *
128 per_MultiFuncCall(PG_FUNCTION_ARGS)
129 {
130         FuncCallContext *retval = (FuncCallContext *) fcinfo->flinfo->fn_extra;
131
132         /*
133          * Clear the TupleTableSlot, if present.  This is for safety's sake: the
134          * Slot will be in a long-lived context (it better be, if the
135          * FuncCallContext is pointing to it), but in most usage patterns the
136          * tuples stored in it will be in the function's per-tuple context. So at
137          * the beginning of each call, the Slot will hold a dangling pointer to an
138          * already-recycled tuple.      We clear it out here.
139          *
140          * Note: use of retval->slot is obsolete as of 8.0, and we expect that it
141          * will always be NULL.  This is just here for backwards compatibility in
142          * case someone creates a slot anyway.
143          */
144         if (retval->slot != NULL)
145                 ExecClearTuple(retval->slot);
146
147         return retval;
148 }
149
150 /*
151  * end_MultiFuncCall
152  * Clean up after init_MultiFuncCall
153  */
154 void
155 end_MultiFuncCall(PG_FUNCTION_ARGS, FuncCallContext *funcctx)
156 {
157         ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
158
159         /* Deregister the shutdown callback */
160         UnregisterExprContextCallback(rsi->econtext,
161                                                                   shutdown_MultiFuncCall,
162                                                                   PointerGetDatum(fcinfo->flinfo));
163
164         /* But use it to do the real work */
165         shutdown_MultiFuncCall(PointerGetDatum(fcinfo->flinfo));
166 }
167
168 /*
169  * shutdown_MultiFuncCall
170  * Shutdown function to clean up after init_MultiFuncCall
171  */
172 static void
173 shutdown_MultiFuncCall(Datum arg)
174 {
175         FmgrInfo   *flinfo = (FmgrInfo *) DatumGetPointer(arg);
176         FuncCallContext *funcctx = (FuncCallContext *) flinfo->fn_extra;
177
178         /* unbind from flinfo */
179         flinfo->fn_extra = NULL;
180
181         /*
182          * Delete context that holds all multi-call data, including the
183          * FuncCallContext itself
184          */
185         MemoryContextSwitchTo(flinfo->fn_mcxt);
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 = SearchSysCache(PROCOID,
304                                                 ObjectIdGetDatum(funcid),
305                                                 0, 0, 0);
306         if (!HeapTupleIsValid(tp))
307                 elog(ERROR, "cache lookup failed for function %u", funcid);
308         procform = (Form_pg_proc) GETSTRUCT(tp);
309
310         rettype = procform->prorettype;
311
312         /* Check for OUT parameters defining a RECORD result */
313         tupdesc = build_function_result_tupdesc_t(tp);
314         if (tupdesc)
315         {
316                 /*
317                  * It has OUT parameters, so it's basically like a regular composite
318                  * type, except we have to be able to resolve any polymorphic OUT
319                  * parameters.
320                  */
321                 if (resultTypeId)
322                         *resultTypeId = rettype;
323
324                 if (resolve_polymorphic_tupdesc(tupdesc,
325                                                                                 &procform->proargtypes,
326                                                                                 call_expr))
327                 {
328                         if (tupdesc->tdtypeid == RECORDOID &&
329                                 tupdesc->tdtypmod < 0)
330                                 assign_record_type_typmod(tupdesc);
331                         if (resultTupleDesc)
332                                 *resultTupleDesc = tupdesc;
333                         result = TYPEFUNC_COMPOSITE;
334                 }
335                 else
336                 {
337                         if (resultTupleDesc)
338                                 *resultTupleDesc = NULL;
339                         result = TYPEFUNC_RECORD;
340                 }
341
342                 ReleaseSysCache(tp);
343
344                 return result;
345         }
346
347         /*
348          * If scalar polymorphic result, try to resolve it.
349          */
350         if (IsPolymorphicType(rettype))
351         {
352                 Oid                     newrettype = exprType(call_expr);
353
354                 if (newrettype == InvalidOid)   /* this probably should not happen */
355                         ereport(ERROR,
356                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
357                                          errmsg("could not determine actual result type for function \"%s\" declared to return type %s",
358                                                         NameStr(procform->proname),
359                                                         format_type_be(rettype))));
360                 rettype = newrettype;
361         }
362
363         if (resultTypeId)
364                 *resultTypeId = rettype;
365         if (resultTupleDesc)
366                 *resultTupleDesc = NULL;        /* default result */
367
368         /* Classify the result type */
369         result = get_type_func_class(rettype);
370         switch (result)
371         {
372                 case TYPEFUNC_COMPOSITE:
373                         if (resultTupleDesc)
374                                 *resultTupleDesc = lookup_rowtype_tupdesc_copy(rettype, -1);
375                         /* Named composite types can't have any polymorphic columns */
376                         break;
377                 case TYPEFUNC_SCALAR:
378                         break;
379                 case TYPEFUNC_RECORD:
380                         /* We must get the tupledesc from call context */
381                         if (rsinfo && IsA(rsinfo, ReturnSetInfo) &&
382                                 rsinfo->expectedDesc != NULL)
383                         {
384                                 result = TYPEFUNC_COMPOSITE;
385                                 if (resultTupleDesc)
386                                         *resultTupleDesc = rsinfo->expectedDesc;
387                                 /* Assume no polymorphic columns here, either */
388                         }
389                         break;
390                 default:
391                         break;
392         }
393
394         ReleaseSysCache(tp);
395
396         return result;
397 }
398
399 /*
400  * Given the result tuple descriptor for a function with OUT parameters,
401  * replace any polymorphic columns (ANYELEMENT etc) with correct data types
402  * deduced from the input arguments. Returns TRUE if able to deduce all types,
403  * FALSE if not.
404  */
405 static bool
406 resolve_polymorphic_tupdesc(TupleDesc tupdesc, oidvector *declared_args,
407                                                         Node *call_expr)
408 {
409         int                     natts = tupdesc->natts;
410         int                     nargs = declared_args->dim1;
411         bool            have_anyelement_result = false;
412         bool            have_anyarray_result = false;
413         bool            have_anynonarray = false;
414         bool            have_anyenum = false;
415         Oid                     anyelement_type = InvalidOid;
416         Oid                     anyarray_type = InvalidOid;
417         int                     i;
418
419         /* See if there are any polymorphic outputs; quick out if not */
420         for (i = 0; i < natts; i++)
421         {
422                 switch (tupdesc->attrs[i]->atttypid)
423                 {
424                         case ANYELEMENTOID:
425                                 have_anyelement_result = true;
426                                 break;
427                         case ANYARRAYOID:
428                                 have_anyarray_result = true;
429                                 break;
430                         case ANYNONARRAYOID:
431                                 have_anyelement_result = true;
432                                 have_anynonarray = true;
433                                 break;
434                         case ANYENUMOID:
435                                 have_anyelement_result = true;
436                                 have_anyenum = true;
437                                 break;
438                         default:
439                                 break;
440                 }
441         }
442         if (!have_anyelement_result && !have_anyarray_result)
443                 return true;
444
445         /*
446          * Otherwise, extract actual datatype(s) from input arguments.  (We assume
447          * the parser already validated consistency of the arguments.)
448          */
449         if (!call_expr)
450                 return false;                   /* no hope */
451
452         for (i = 0; i < nargs; i++)
453         {
454                 switch (declared_args->values[i])
455                 {
456                         case ANYELEMENTOID:
457                         case ANYNONARRAYOID:
458                         case ANYENUMOID:
459                                 if (!OidIsValid(anyelement_type))
460                                         anyelement_type = get_call_expr_argtype(call_expr, i);
461                                 break;
462                         case ANYARRAYOID:
463                                 if (!OidIsValid(anyarray_type))
464                                         anyarray_type = get_call_expr_argtype(call_expr, i);
465                                 break;
466                         default:
467                                 break;
468                 }
469         }
470
471         /* If nothing found, parser messed up */
472         if (!OidIsValid(anyelement_type) && !OidIsValid(anyarray_type))
473                 return false;
474
475         /* If needed, deduce one polymorphic type from the other */
476         if (have_anyelement_result && !OidIsValid(anyelement_type))
477                 anyelement_type = resolve_generic_type(ANYELEMENTOID,
478                                                                                            anyarray_type,
479                                                                                            ANYARRAYOID);
480         if (have_anyarray_result && !OidIsValid(anyarray_type))
481                 anyarray_type = resolve_generic_type(ANYARRAYOID,
482                                                                                          anyelement_type,
483                                                                                          ANYELEMENTOID);
484
485         /* Enforce ANYNONARRAY if needed */
486         if (have_anynonarray && type_is_array(anyelement_type))
487                 return false;
488
489         /* Enforce ANYENUM if needed */
490         if (have_anyenum && !type_is_enum(anyelement_type))
491                 return false;
492
493         /* And finally replace the tuple column types as needed */
494         for (i = 0; i < natts; i++)
495         {
496                 switch (tupdesc->attrs[i]->atttypid)
497                 {
498                         case ANYELEMENTOID:
499                         case ANYNONARRAYOID:
500                         case ANYENUMOID:
501                                 TupleDescInitEntry(tupdesc, i + 1,
502                                                                    NameStr(tupdesc->attrs[i]->attname),
503                                                                    anyelement_type,
504                                                                    -1,
505                                                                    0);
506                                 break;
507                         case ANYARRAYOID:
508                                 TupleDescInitEntry(tupdesc, i + 1,
509                                                                    NameStr(tupdesc->attrs[i]->attname),
510                                                                    anyarray_type,
511                                                                    -1,
512                                                                    0);
513                                 break;
514                         default:
515                                 break;
516                 }
517         }
518
519         return true;
520 }
521
522 /*
523  * Given the declared argument types and modes for a function, replace any
524  * polymorphic types (ANYELEMENT etc) with correct data types deduced from the
525  * input arguments.  Returns TRUE if able to deduce all types, FALSE if not.
526  * This is the same logic as resolve_polymorphic_tupdesc, but with a different
527  * argument representation.
528  *
529  * argmodes may be NULL, in which case all arguments are assumed to be IN mode.
530  */
531 bool
532 resolve_polymorphic_argtypes(int numargs, Oid *argtypes, char *argmodes,
533                                                          Node *call_expr)
534 {
535         bool            have_anyelement_result = false;
536         bool            have_anyarray_result = false;
537         Oid                     anyelement_type = InvalidOid;
538         Oid                     anyarray_type = InvalidOid;
539         int                     inargno;
540         int                     i;
541
542         /* First pass: resolve polymorphic inputs, check for outputs */
543         inargno = 0;
544         for (i = 0; i < numargs; i++)
545         {
546                 char            argmode = argmodes ? argmodes[i] : PROARGMODE_IN;
547
548                 switch (argtypes[i])
549                 {
550                         case ANYELEMENTOID:
551                         case ANYNONARRAYOID:
552                         case ANYENUMOID:
553                                 if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
554                                         have_anyelement_result = true;
555                                 else
556                                 {
557                                         if (!OidIsValid(anyelement_type))
558                                         {
559                                                 anyelement_type = get_call_expr_argtype(call_expr,
560                                                                                                                                 inargno);
561                                                 if (!OidIsValid(anyelement_type))
562                                                         return false;
563                                         }
564                                         argtypes[i] = anyelement_type;
565                                 }
566                                 break;
567                         case ANYARRAYOID:
568                                 if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
569                                         have_anyarray_result = true;
570                                 else
571                                 {
572                                         if (!OidIsValid(anyarray_type))
573                                         {
574                                                 anyarray_type = get_call_expr_argtype(call_expr,
575                                                                                                                           inargno);
576                                                 if (!OidIsValid(anyarray_type))
577                                                         return false;
578                                         }
579                                         argtypes[i] = anyarray_type;
580                                 }
581                                 break;
582                         default:
583                                 break;
584                 }
585                 if (argmode != PROARGMODE_OUT && argmode != PROARGMODE_TABLE)
586                         inargno++;
587         }
588
589         /* Done? */
590         if (!have_anyelement_result && !have_anyarray_result)
591                 return true;
592
593         /* If no input polymorphics, parser messed up */
594         if (!OidIsValid(anyelement_type) && !OidIsValid(anyarray_type))
595                 return false;
596
597         /* If needed, deduce one polymorphic type from the other */
598         if (have_anyelement_result && !OidIsValid(anyelement_type))
599                 anyelement_type = resolve_generic_type(ANYELEMENTOID,
600                                                                                            anyarray_type,
601                                                                                            ANYARRAYOID);
602         if (have_anyarray_result && !OidIsValid(anyarray_type))
603                 anyarray_type = resolve_generic_type(ANYARRAYOID,
604                                                                                          anyelement_type,
605                                                                                          ANYELEMENTOID);
606
607         /* XXX do we need to enforce ANYNONARRAY or ANYENUM here?  I think not */
608
609         /* And finally replace the output column types as needed */
610         for (i = 0; i < numargs; i++)
611         {
612                 switch (argtypes[i])
613                 {
614                         case ANYELEMENTOID:
615                         case ANYNONARRAYOID:
616                         case ANYENUMOID:
617                                 argtypes[i] = anyelement_type;
618                                 break;
619                         case ANYARRAYOID:
620                                 argtypes[i] = anyarray_type;
621                                 break;
622                         default:
623                                 break;
624                 }
625         }
626
627         return true;
628 }
629
630 /*
631  * get_type_func_class
632  *              Given the type OID, obtain its TYPEFUNC classification.
633  *
634  * This is intended to centralize a bunch of formerly ad-hoc code for
635  * classifying types.  The categories used here are useful for deciding
636  * how to handle functions returning the datatype.
637  */
638 static TypeFuncClass
639 get_type_func_class(Oid typid)
640 {
641         switch (get_typtype(typid))
642         {
643                 case TYPTYPE_COMPOSITE:
644                         return TYPEFUNC_COMPOSITE;
645                 case TYPTYPE_BASE:
646                 case TYPTYPE_DOMAIN:
647                 case TYPTYPE_ENUM:
648                         return TYPEFUNC_SCALAR;
649                 case TYPTYPE_PSEUDO:
650                         if (typid == RECORDOID)
651                                 return TYPEFUNC_RECORD;
652
653                         /*
654                          * We treat VOID and CSTRING as legitimate scalar datatypes,
655                          * mostly for the convenience of the JDBC driver (which wants to
656                          * be able to do "SELECT * FROM foo()" for all legitimately
657                          * user-callable functions).
658                          */
659                         if (typid == VOIDOID || typid == CSTRINGOID)
660                                 return TYPEFUNC_SCALAR;
661                         return TYPEFUNC_OTHER;
662         }
663         /* shouldn't get here, probably */
664         return TYPEFUNC_OTHER;
665 }
666
667
668 /*
669  * get_func_arg_info
670  *
671  * Fetch info about the argument types, names, and IN/OUT modes from the
672  * pg_proc tuple.  Return value is the total number of arguments.
673  * Other results are palloc'd.  *p_argtypes is always filled in, but
674  * *p_argnames and *p_argmodes will be set NULL in the default cases
675  * (no names, and all IN arguments, respectively).
676  *
677  * Note that this function simply fetches what is in the pg_proc tuple;
678  * it doesn't do any interpretation of polymorphic types.
679  */
680 int
681 get_func_arg_info(HeapTuple procTup,
682                                   Oid **p_argtypes, char ***p_argnames, char **p_argmodes)
683 {
684         Form_pg_proc procStruct = (Form_pg_proc) GETSTRUCT(procTup);
685         Datum           proallargtypes;
686         Datum           proargmodes;
687         Datum           proargnames;
688         bool            isNull;
689         ArrayType  *arr;
690         int                     numargs;
691         Datum      *elems;
692         int                     nelems;
693         int                     i;
694
695         /* First discover the total number of parameters and get their types */
696         proallargtypes = SysCacheGetAttr(PROCOID, procTup,
697                                                                          Anum_pg_proc_proallargtypes,
698                                                                          &isNull);
699         if (!isNull)
700         {
701                 /*
702                  * We expect the arrays to be 1-D arrays of the right types; verify
703                  * that.  For the OID and char arrays, we don't need to use
704                  * deconstruct_array() since the array data is just going to look like
705                  * a C array of values.
706                  */
707                 arr = DatumGetArrayTypeP(proallargtypes);               /* ensure not toasted */
708                 numargs = ARR_DIMS(arr)[0];
709                 if (ARR_NDIM(arr) != 1 ||
710                         numargs < 0 ||
711                         ARR_HASNULL(arr) ||
712                         ARR_ELEMTYPE(arr) != OIDOID)
713                         elog(ERROR, "proallargtypes is not a 1-D Oid array");
714                 Assert(numargs >= procStruct->pronargs);
715                 *p_argtypes = (Oid *) palloc(numargs * sizeof(Oid));
716                 memcpy(*p_argtypes, ARR_DATA_PTR(arr),
717                            numargs * sizeof(Oid));
718         }
719         else
720         {
721                 /* If no proallargtypes, use proargtypes */
722                 numargs = procStruct->proargtypes.dim1;
723                 Assert(numargs == procStruct->pronargs);
724                 *p_argtypes = (Oid *) palloc(numargs * sizeof(Oid));
725                 memcpy(*p_argtypes, procStruct->proargtypes.values,
726                            numargs * sizeof(Oid));
727         }
728
729         /* Get argument names, if available */
730         proargnames = SysCacheGetAttr(PROCOID, procTup,
731                                                                   Anum_pg_proc_proargnames,
732                                                                   &isNull);
733         if (isNull)
734                 *p_argnames = NULL;
735         else
736         {
737                 deconstruct_array(DatumGetArrayTypeP(proargnames),
738                                                   TEXTOID, -1, false, 'i',
739                                                   &elems, NULL, &nelems);
740                 if (nelems != numargs)  /* should not happen */
741                         elog(ERROR, "proargnames must have the same number of elements as the function has arguments");
742                 *p_argnames = (char **) palloc(sizeof(char *) * numargs);
743                 for (i = 0; i < numargs; i++)
744                         (*p_argnames)[i] = TextDatumGetCString(elems[i]);
745         }
746
747         /* Get argument modes, if available */
748         proargmodes = SysCacheGetAttr(PROCOID, procTup,
749                                                                   Anum_pg_proc_proargmodes,
750                                                                   &isNull);
751         if (isNull)
752                 *p_argmodes = NULL;
753         else
754         {
755                 arr = DatumGetArrayTypeP(proargmodes);  /* ensure not toasted */
756                 if (ARR_NDIM(arr) != 1 ||
757                         ARR_DIMS(arr)[0] != numargs ||
758                         ARR_HASNULL(arr) ||
759                         ARR_ELEMTYPE(arr) != CHAROID)
760                         elog(ERROR, "proargmodes is not a 1-D char array");
761                 *p_argmodes = (char *) palloc(numargs * sizeof(char));
762                 memcpy(*p_argmodes, ARR_DATA_PTR(arr),
763                            numargs * sizeof(char));
764         }
765
766         return numargs;
767 }
768
769
770 /*
771  * get_func_result_name
772  *
773  * If the function has exactly one output parameter, and that parameter
774  * is named, return the name (as a palloc'd string).  Else return NULL.
775  *
776  * This is used to determine the default output column name for functions
777  * returning scalar types.
778  */
779 char *
780 get_func_result_name(Oid functionId)
781 {
782         char       *result;
783         HeapTuple       procTuple;
784         Datum           proargmodes;
785         Datum           proargnames;
786         bool            isnull;
787         ArrayType  *arr;
788         int                     numargs;
789         char       *argmodes;
790         Datum      *argnames;
791         int                     numoutargs;
792         int                     nargnames;
793         int                     i;
794
795         /* First fetch the function's pg_proc row */
796         procTuple = SearchSysCache(PROCOID,
797                                                            ObjectIdGetDatum(functionId),
798                                                            0, 0, 0);
799         if (!HeapTupleIsValid(procTuple))
800                 elog(ERROR, "cache lookup failed for function %u", functionId);
801
802         /* If there are no named OUT parameters, return NULL */
803         if (heap_attisnull(procTuple, Anum_pg_proc_proargmodes) ||
804                 heap_attisnull(procTuple, Anum_pg_proc_proargnames))
805                 result = NULL;
806         else
807         {
808                 /* Get the data out of the tuple */
809                 proargmodes = SysCacheGetAttr(PROCOID, procTuple,
810                                                                           Anum_pg_proc_proargmodes,
811                                                                           &isnull);
812                 Assert(!isnull);
813                 proargnames = SysCacheGetAttr(PROCOID, procTuple,
814                                                                           Anum_pg_proc_proargnames,
815                                                                           &isnull);
816                 Assert(!isnull);
817
818                 /*
819                  * We expect the arrays to be 1-D arrays of the right types; verify
820                  * that.  For the char array, we don't need to use deconstruct_array()
821                  * since the array data is just going to look like a C array of
822                  * values.
823                  */
824                 arr = DatumGetArrayTypeP(proargmodes);  /* ensure not toasted */
825                 numargs = ARR_DIMS(arr)[0];
826                 if (ARR_NDIM(arr) != 1 ||
827                         numargs < 0 ||
828                         ARR_HASNULL(arr) ||
829                         ARR_ELEMTYPE(arr) != CHAROID)
830                         elog(ERROR, "proargmodes is not a 1-D char array");
831                 argmodes = (char *) ARR_DATA_PTR(arr);
832                 arr = DatumGetArrayTypeP(proargnames);  /* ensure not toasted */
833                 if (ARR_NDIM(arr) != 1 ||
834                         ARR_DIMS(arr)[0] != numargs ||
835                         ARR_HASNULL(arr) ||
836                         ARR_ELEMTYPE(arr) != TEXTOID)
837                         elog(ERROR, "proargnames is not a 1-D text array");
838                 deconstruct_array(arr, TEXTOID, -1, false, 'i',
839                                                   &argnames, NULL, &nargnames);
840                 Assert(nargnames == numargs);
841
842                 /* scan for output argument(s) */
843                 result = NULL;
844                 numoutargs = 0;
845                 for (i = 0; i < numargs; i++)
846                 {
847                         if (argmodes[i] == PROARGMODE_IN ||
848                                 argmodes[i] == PROARGMODE_VARIADIC)
849                                 continue;
850                         Assert(argmodes[i] == PROARGMODE_OUT ||
851                                    argmodes[i] == PROARGMODE_INOUT ||
852                                    argmodes[i] == PROARGMODE_TABLE);
853                         if (++numoutargs > 1)
854                         {
855                                 /* multiple out args, so forget it */
856                                 result = NULL;
857                                 break;
858                         }
859                         result = TextDatumGetCString(argnames[i]);
860                         if (result == NULL || result[0] == '\0')
861                         {
862                                 /* Parameter is not named, so forget it */
863                                 result = NULL;
864                                 break;
865                         }
866                 }
867         }
868
869         ReleaseSysCache(procTuple);
870
871         return result;
872 }
873
874
875 /*
876  * build_function_result_tupdesc_t
877  *
878  * Given a pg_proc row for a function, return a tuple descriptor for the
879  * result rowtype, or NULL if the function does not have OUT parameters.
880  *
881  * Note that this does not handle resolution of polymorphic types;
882  * that is deliberate.
883  */
884 TupleDesc
885 build_function_result_tupdesc_t(HeapTuple procTuple)
886 {
887         Form_pg_proc procform = (Form_pg_proc) GETSTRUCT(procTuple);
888         Datum           proallargtypes;
889         Datum           proargmodes;
890         Datum           proargnames;
891         bool            isnull;
892
893         /* Return NULL if the function isn't declared to return RECORD */
894         if (procform->prorettype != RECORDOID)
895                 return NULL;
896
897         /* If there are no OUT parameters, return NULL */
898         if (heap_attisnull(procTuple, Anum_pg_proc_proallargtypes) ||
899                 heap_attisnull(procTuple, Anum_pg_proc_proargmodes))
900                 return NULL;
901
902         /* Get the data out of the tuple */
903         proallargtypes = SysCacheGetAttr(PROCOID, procTuple,
904                                                                          Anum_pg_proc_proallargtypes,
905                                                                          &isnull);
906         Assert(!isnull);
907         proargmodes = SysCacheGetAttr(PROCOID, procTuple,
908                                                                   Anum_pg_proc_proargmodes,
909                                                                   &isnull);
910         Assert(!isnull);
911         proargnames = SysCacheGetAttr(PROCOID, procTuple,
912                                                                   Anum_pg_proc_proargnames,
913                                                                   &isnull);
914         if (isnull)
915                 proargnames = PointerGetDatum(NULL);    /* just to be sure */
916
917         return build_function_result_tupdesc_d(proallargtypes,
918                                                                                    proargmodes,
919                                                                                    proargnames);
920 }
921
922 /*
923  * build_function_result_tupdesc_d
924  *
925  * Build a RECORD function's tupledesc from the pg_proc proallargtypes,
926  * proargmodes, and proargnames arrays.  This is split out for the
927  * convenience of ProcedureCreate, which needs to be able to compute the
928  * tupledesc before actually creating the function.
929  *
930  * Returns NULL if there are not at least two OUT or INOUT arguments.
931  */
932 TupleDesc
933 build_function_result_tupdesc_d(Datum proallargtypes,
934                                                                 Datum proargmodes,
935                                                                 Datum proargnames)
936 {
937         TupleDesc       desc;
938         ArrayType  *arr;
939         int                     numargs;
940         Oid                *argtypes;
941         char       *argmodes;
942         Datum      *argnames = NULL;
943         Oid                *outargtypes;
944         char      **outargnames;
945         int                     numoutargs;
946         int                     nargnames;
947         int                     i;
948
949         /* Can't have output args if columns are null */
950         if (proallargtypes == PointerGetDatum(NULL) ||
951                 proargmodes == PointerGetDatum(NULL))
952                 return NULL;
953
954         /*
955          * We expect the arrays to be 1-D arrays of the right types; verify that.
956          * For the OID and char arrays, we don't need to use deconstruct_array()
957          * since the array data is just going to look like a C array of values.
958          */
959         arr = DatumGetArrayTypeP(proallargtypes);       /* ensure not toasted */
960         numargs = ARR_DIMS(arr)[0];
961         if (ARR_NDIM(arr) != 1 ||
962                 numargs < 0 ||
963                 ARR_HASNULL(arr) ||
964                 ARR_ELEMTYPE(arr) != OIDOID)
965                 elog(ERROR, "proallargtypes is not a 1-D Oid array");
966         argtypes = (Oid *) ARR_DATA_PTR(arr);
967         arr = DatumGetArrayTypeP(proargmodes);          /* ensure not toasted */
968         if (ARR_NDIM(arr) != 1 ||
969                 ARR_DIMS(arr)[0] != numargs ||
970                 ARR_HASNULL(arr) ||
971                 ARR_ELEMTYPE(arr) != CHAROID)
972                 elog(ERROR, "proargmodes is not a 1-D char array");
973         argmodes = (char *) ARR_DATA_PTR(arr);
974         if (proargnames != PointerGetDatum(NULL))
975         {
976                 arr = DatumGetArrayTypeP(proargnames);  /* ensure not toasted */
977                 if (ARR_NDIM(arr) != 1 ||
978                         ARR_DIMS(arr)[0] != numargs ||
979                         ARR_HASNULL(arr) ||
980                         ARR_ELEMTYPE(arr) != TEXTOID)
981                         elog(ERROR, "proargnames is not a 1-D text array");
982                 deconstruct_array(arr, TEXTOID, -1, false, 'i',
983                                                   &argnames, NULL, &nargnames);
984                 Assert(nargnames == numargs);
985         }
986
987         /* zero elements probably shouldn't happen, but handle it gracefully */
988         if (numargs <= 0)
989                 return NULL;
990
991         /* extract output-argument types and names */
992         outargtypes = (Oid *) palloc(numargs * sizeof(Oid));
993         outargnames = (char **) palloc(numargs * sizeof(char *));
994         numoutargs = 0;
995         for (i = 0; i < numargs; i++)
996         {
997                 char       *pname;
998
999                 if (argmodes[i] == PROARGMODE_IN ||
1000                         argmodes[i] == PROARGMODE_VARIADIC)
1001                         continue;
1002                 Assert(argmodes[i] == PROARGMODE_OUT ||
1003                            argmodes[i] == PROARGMODE_INOUT ||
1004                            argmodes[i] == PROARGMODE_TABLE);
1005                 outargtypes[numoutargs] = argtypes[i];
1006                 if (argnames)
1007                         pname = TextDatumGetCString(argnames[i]);
1008                 else
1009                         pname = NULL;
1010                 if (pname == NULL || pname[0] == '\0')
1011                 {
1012                         /* Parameter is not named, so gin up a column name */
1013                         pname = (char *) palloc(32);
1014                         snprintf(pname, 32, "column%d", numoutargs + 1);
1015                 }
1016                 outargnames[numoutargs] = pname;
1017                 numoutargs++;
1018         }
1019
1020         /*
1021          * If there is no output argument, or only one, the function does not
1022          * return tuples.
1023          */
1024         if (numoutargs < 2)
1025                 return NULL;
1026
1027         desc = CreateTemplateTupleDesc(numoutargs, false);
1028         for (i = 0; i < numoutargs; i++)
1029         {
1030                 TupleDescInitEntry(desc, i + 1,
1031                                                    outargnames[i],
1032                                                    outargtypes[i],
1033                                                    -1,
1034                                                    0);
1035         }
1036
1037         return desc;
1038 }
1039
1040
1041 /*
1042  * RelationNameGetTupleDesc
1043  *
1044  * Given a (possibly qualified) relation name, build a TupleDesc.
1045  *
1046  * Note: while this works as advertised, it's seldom the best way to
1047  * build a tupdesc for a function's result type.  It's kept around
1048  * only for backwards compatibility with existing user-written code.
1049  */
1050 TupleDesc
1051 RelationNameGetTupleDesc(const char *relname)
1052 {
1053         RangeVar   *relvar;
1054         Relation        rel;
1055         TupleDesc       tupdesc;
1056         List       *relname_list;
1057
1058         /* Open relation and copy the tuple description */
1059         relname_list = stringToQualifiedNameList(relname);
1060         relvar = makeRangeVarFromNameList(relname_list);
1061         rel = relation_openrv(relvar, AccessShareLock);
1062         tupdesc = CreateTupleDescCopy(RelationGetDescr(rel));
1063         relation_close(rel, AccessShareLock);
1064
1065         return tupdesc;
1066 }
1067
1068 /*
1069  * TypeGetTupleDesc
1070  *
1071  * Given a type Oid, build a TupleDesc.  (In most cases you should be
1072  * using get_call_result_type or one of its siblings instead of this
1073  * routine, so that you can handle OUT parameters, RECORD result type,
1074  * and polymorphic results.)
1075  *
1076  * If the type is composite, *and* a colaliases List is provided, *and*
1077  * the List is of natts length, use the aliases instead of the relation
1078  * attnames.  (NB: this usage is deprecated since it may result in
1079  * creation of unnecessary transient record types.)
1080  *
1081  * If the type is a base type, a single item alias List is required.
1082  */
1083 TupleDesc
1084 TypeGetTupleDesc(Oid typeoid, List *colaliases)
1085 {
1086         TypeFuncClass functypclass = get_type_func_class(typeoid);
1087         TupleDesc       tupdesc = NULL;
1088
1089         /*
1090          * Build a suitable tupledesc representing the output rows
1091          */
1092         if (functypclass == TYPEFUNC_COMPOSITE)
1093         {
1094                 /* Composite data type, e.g. a table's row type */
1095                 tupdesc = lookup_rowtype_tupdesc_copy(typeoid, -1);
1096
1097                 if (colaliases != NIL)
1098                 {
1099                         int                     natts = tupdesc->natts;
1100                         int                     varattno;
1101
1102                         /* does the list length match the number of attributes? */
1103                         if (list_length(colaliases) != natts)
1104                                 ereport(ERROR,
1105                                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
1106                                                  errmsg("number of aliases does not match number of columns")));
1107
1108                         /* OK, use the aliases instead */
1109                         for (varattno = 0; varattno < natts; varattno++)
1110                         {
1111                                 char       *label = strVal(list_nth(colaliases, varattno));
1112
1113                                 if (label != NULL)
1114                                         namestrcpy(&(tupdesc->attrs[varattno]->attname), label);
1115                         }
1116
1117                         /* The tuple type is now an anonymous record type */
1118                         tupdesc->tdtypeid = RECORDOID;
1119                         tupdesc->tdtypmod = -1;
1120                 }
1121         }
1122         else if (functypclass == TYPEFUNC_SCALAR)
1123         {
1124                 /* Base data type, i.e. scalar */
1125                 char       *attname;
1126
1127                 /* the alias list is required for base types */
1128                 if (colaliases == NIL)
1129                         ereport(ERROR,
1130                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
1131                                          errmsg("no column alias was provided")));
1132
1133                 /* the alias list length must be 1 */
1134                 if (list_length(colaliases) != 1)
1135                         ereport(ERROR,
1136                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
1137                           errmsg("number of aliases does not match number of columns")));
1138
1139                 /* OK, get the column alias */
1140                 attname = strVal(linitial(colaliases));
1141
1142                 tupdesc = CreateTemplateTupleDesc(1, false);
1143                 TupleDescInitEntry(tupdesc,
1144                                                    (AttrNumber) 1,
1145                                                    attname,
1146                                                    typeoid,
1147                                                    -1,
1148                                                    0);
1149         }
1150         else if (functypclass == TYPEFUNC_RECORD)
1151         {
1152                 /* XXX can't support this because typmod wasn't passed in ... */
1153                 ereport(ERROR,
1154                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
1155                                  errmsg("could not determine row description for function returning record")));
1156         }
1157         else
1158         {
1159                 /* crummy error message, but parser should have caught this */
1160                 elog(ERROR, "function in FROM has unsupported return type");
1161         }
1162
1163         return tupdesc;
1164 }