]> granicus.if.org Git - postgresql/blob - src/backend/utils/fmgr/funcapi.c
Revise collation derivation method and expression-tree representation.
[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-2011, 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/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 "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/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         MemoryContextDelete(funcctx->multi_call_memory_ctx);
186 }
187
188
189 /*
190  * get_call_result_type
191  *              Given a function's call info record, determine the kind of datatype
192  *              it is supposed to return.  If resultTypeId isn't NULL, *resultTypeId
193  *              receives the actual datatype OID (this is mainly useful for scalar
194  *              result types).  If resultTupleDesc isn't NULL, *resultTupleDesc
195  *              receives a pointer to a TupleDesc when the result is of a composite
196  *              type, or NULL when it's a scalar result.
197  *
198  * One hard case that this handles is resolution of actual rowtypes for
199  * functions returning RECORD (from either the function's OUT parameter
200  * list, or a ReturnSetInfo context node).      TYPEFUNC_RECORD is returned
201  * only when we couldn't resolve the actual rowtype for lack of information.
202  *
203  * The other hard case that this handles is resolution of polymorphism.
204  * We will never return polymorphic pseudotypes (ANYELEMENT etc), either
205  * as a scalar result type or as a component of a rowtype.
206  *
207  * This function is relatively expensive --- in a function returning set,
208  * try to call it only the first time through.
209  */
210 TypeFuncClass
211 get_call_result_type(FunctionCallInfo fcinfo,
212                                          Oid *resultTypeId,
213                                          TupleDesc *resultTupleDesc)
214 {
215         return internal_get_result_type(fcinfo->flinfo->fn_oid,
216                                                                         fcinfo->flinfo->fn_expr,
217                                                                         (ReturnSetInfo *) fcinfo->resultinfo,
218                                                                         resultTypeId,
219                                                                         resultTupleDesc);
220 }
221
222 /*
223  * get_expr_result_type
224  *              As above, but work from a calling expression node tree
225  */
226 TypeFuncClass
227 get_expr_result_type(Node *expr,
228                                          Oid *resultTypeId,
229                                          TupleDesc *resultTupleDesc)
230 {
231         TypeFuncClass result;
232
233         if (expr && IsA(expr, FuncExpr))
234                 result = internal_get_result_type(((FuncExpr *) expr)->funcid,
235                                                                                   expr,
236                                                                                   NULL,
237                                                                                   resultTypeId,
238                                                                                   resultTupleDesc);
239         else if (expr && IsA(expr, OpExpr))
240                 result = internal_get_result_type(get_opcode(((OpExpr *) expr)->opno),
241                                                                                   expr,
242                                                                                   NULL,
243                                                                                   resultTypeId,
244                                                                                   resultTupleDesc);
245         else
246         {
247                 /* handle as a generic expression; no chance to resolve RECORD */
248                 Oid                     typid = exprType(expr);
249
250                 if (resultTypeId)
251                         *resultTypeId = typid;
252                 if (resultTupleDesc)
253                         *resultTupleDesc = NULL;
254                 result = get_type_func_class(typid);
255                 if (result == TYPEFUNC_COMPOSITE && resultTupleDesc)
256                         *resultTupleDesc = lookup_rowtype_tupdesc_copy(typid, -1);
257         }
258
259         return result;
260 }
261
262 /*
263  * get_func_result_type
264  *              As above, but work from a function's OID only
265  *
266  * This will not be able to resolve pure-RECORD results nor polymorphism.
267  */
268 TypeFuncClass
269 get_func_result_type(Oid functionId,
270                                          Oid *resultTypeId,
271                                          TupleDesc *resultTupleDesc)
272 {
273         return internal_get_result_type(functionId,
274                                                                         NULL,
275                                                                         NULL,
276                                                                         resultTypeId,
277                                                                         resultTupleDesc);
278 }
279
280 /*
281  * internal_get_result_type -- workhorse code implementing all the above
282  *
283  * funcid must always be supplied.      call_expr and rsinfo can be NULL if not
284  * available.  We will return TYPEFUNC_RECORD, and store NULL into
285  * *resultTupleDesc, if we cannot deduce the complete result rowtype from
286  * the available information.
287  */
288 static TypeFuncClass
289 internal_get_result_type(Oid funcid,
290                                                  Node *call_expr,
291                                                  ReturnSetInfo *rsinfo,
292                                                  Oid *resultTypeId,
293                                                  TupleDesc *resultTupleDesc)
294 {
295         TypeFuncClass result;
296         HeapTuple       tp;
297         Form_pg_proc procform;
298         Oid                     rettype;
299         TupleDesc       tupdesc;
300
301         /* First fetch the function's pg_proc row to inspect its rettype */
302         tp = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
303         if (!HeapTupleIsValid(tp))
304                 elog(ERROR, "cache lookup failed for function %u", funcid);
305         procform = (Form_pg_proc) GETSTRUCT(tp);
306
307         rettype = procform->prorettype;
308
309         /* Check for OUT parameters defining a RECORD result */
310         tupdesc = build_function_result_tupdesc_t(tp);
311         if (tupdesc)
312         {
313                 /*
314                  * It has OUT parameters, so it's basically like a regular composite
315                  * type, except we have to be able to resolve any polymorphic OUT
316                  * parameters.
317                  */
318                 if (resultTypeId)
319                         *resultTypeId = rettype;
320
321                 if (resolve_polymorphic_tupdesc(tupdesc,
322                                                                                 &procform->proargtypes,
323                                                                                 call_expr))
324                 {
325                         if (tupdesc->tdtypeid == RECORDOID &&
326                                 tupdesc->tdtypmod < 0)
327                                 assign_record_type_typmod(tupdesc);
328                         if (resultTupleDesc)
329                                 *resultTupleDesc = tupdesc;
330                         result = TYPEFUNC_COMPOSITE;
331                 }
332                 else
333                 {
334                         if (resultTupleDesc)
335                                 *resultTupleDesc = NULL;
336                         result = TYPEFUNC_RECORD;
337                 }
338
339                 ReleaseSysCache(tp);
340
341                 return result;
342         }
343
344         /*
345          * If scalar polymorphic result, try to resolve it.
346          */
347         if (IsPolymorphicType(rettype))
348         {
349                 Oid                     newrettype = exprType(call_expr);
350
351                 if (newrettype == InvalidOid)   /* this probably should not happen */
352                         ereport(ERROR,
353                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
354                                          errmsg("could not determine actual result type for function \"%s\" declared to return type %s",
355                                                         NameStr(procform->proname),
356                                                         format_type_be(rettype))));
357                 rettype = newrettype;
358         }
359
360         if (resultTypeId)
361                 *resultTypeId = rettype;
362         if (resultTupleDesc)
363                 *resultTupleDesc = NULL;        /* default result */
364
365         /* Classify the result type */
366         result = get_type_func_class(rettype);
367         switch (result)
368         {
369                 case TYPEFUNC_COMPOSITE:
370                         if (resultTupleDesc)
371                                 *resultTupleDesc = lookup_rowtype_tupdesc_copy(rettype, -1);
372                         /* Named composite types can't have any polymorphic columns */
373                         break;
374                 case TYPEFUNC_SCALAR:
375                         break;
376                 case TYPEFUNC_RECORD:
377                         /* We must get the tupledesc from call context */
378                         if (rsinfo && IsA(rsinfo, ReturnSetInfo) &&
379                                 rsinfo->expectedDesc != NULL)
380                         {
381                                 result = TYPEFUNC_COMPOSITE;
382                                 if (resultTupleDesc)
383                                         *resultTupleDesc = rsinfo->expectedDesc;
384                                 /* Assume no polymorphic columns here, either */
385                         }
386                         break;
387                 default:
388                         break;
389         }
390
391         ReleaseSysCache(tp);
392
393         return result;
394 }
395
396 /*
397  * Given the result tuple descriptor for a function with OUT parameters,
398  * replace any polymorphic columns (ANYELEMENT etc) with correct data types
399  * deduced from the input arguments. Returns TRUE if able to deduce all types,
400  * FALSE if not.
401  */
402 static bool
403 resolve_polymorphic_tupdesc(TupleDesc tupdesc, oidvector *declared_args,
404                                                         Node *call_expr)
405 {
406         int                     natts = tupdesc->natts;
407         int                     nargs = declared_args->dim1;
408         bool            have_anyelement_result = false;
409         bool            have_anyarray_result = false;
410         bool            have_anynonarray = false;
411         bool            have_anyenum = false;
412         Oid                     anyelement_type = InvalidOid;
413         Oid                     anyarray_type = InvalidOid;
414         Oid                     anycollation;
415         int                     i;
416
417         /* See if there are any polymorphic outputs; quick out if not */
418         for (i = 0; i < natts; i++)
419         {
420                 switch (tupdesc->attrs[i]->atttypid)
421                 {
422                         case ANYELEMENTOID:
423                                 have_anyelement_result = true;
424                                 break;
425                         case ANYARRAYOID:
426                                 have_anyarray_result = true;
427                                 break;
428                         case ANYNONARRAYOID:
429                                 have_anyelement_result = true;
430                                 have_anynonarray = true;
431                                 break;
432                         case ANYENUMOID:
433                                 have_anyelement_result = true;
434                                 have_anyenum = true;
435                                 break;
436                         default:
437                                 break;
438                 }
439         }
440         if (!have_anyelement_result && !have_anyarray_result)
441                 return true;
442
443         /*
444          * Otherwise, extract actual datatype(s) from input arguments.  (We assume
445          * the parser already validated consistency of the arguments.)
446          */
447         if (!call_expr)
448                 return false;                   /* no hope */
449
450         for (i = 0; i < nargs; i++)
451         {
452                 switch (declared_args->values[i])
453                 {
454                         case ANYELEMENTOID:
455                         case ANYNONARRAYOID:
456                         case ANYENUMOID:
457                                 if (!OidIsValid(anyelement_type))
458                                         anyelement_type = get_call_expr_argtype(call_expr, i);
459                                 break;
460                         case ANYARRAYOID:
461                                 if (!OidIsValid(anyarray_type))
462                                         anyarray_type = get_call_expr_argtype(call_expr, i);
463                                 break;
464                         default:
465                                 break;
466                 }
467         }
468
469         /* If nothing found, parser messed up */
470         if (!OidIsValid(anyelement_type) && !OidIsValid(anyarray_type))
471                 return false;
472
473         /* If needed, deduce one polymorphic type from the other */
474         if (have_anyelement_result && !OidIsValid(anyelement_type))
475                 anyelement_type = resolve_generic_type(ANYELEMENTOID,
476                                                                                            anyarray_type,
477                                                                                            ANYARRAYOID);
478         if (have_anyarray_result && !OidIsValid(anyarray_type))
479                 anyarray_type = resolve_generic_type(ANYARRAYOID,
480                                                                                          anyelement_type,
481                                                                                          ANYELEMENTOID);
482
483         /* Enforce ANYNONARRAY if needed */
484         if (have_anynonarray && type_is_array(anyelement_type))
485                 return false;
486
487         /* Enforce ANYENUM if needed */
488         if (have_anyenum && !type_is_enum(anyelement_type))
489                 return false;
490
491         /*
492          * Identify the collation to use for polymorphic OUT parameters.
493          * (It'll necessarily be the same for both anyelement and anyarray.)
494          */
495         anycollation = get_typcollation(OidIsValid(anyelement_type) ? anyelement_type : anyarray_type);
496         if (OidIsValid(anycollation))
497         {
498                 /*
499                  * The types are collatable, so consider whether to use a nondefault
500                  * collation.  We do so if we can identify the input collation used
501                  * for the function.
502                  */
503                 Oid             inputcollation = exprInputCollation(call_expr);
504
505                 if (OidIsValid(inputcollation))
506                         anycollation = inputcollation;
507         }
508
509         /* And finally replace the tuple column types as needed */
510         for (i = 0; i < natts; i++)
511         {
512                 switch (tupdesc->attrs[i]->atttypid)
513                 {
514                         case ANYELEMENTOID:
515                         case ANYNONARRAYOID:
516                         case ANYENUMOID:
517                                 TupleDescInitEntry(tupdesc, i + 1,
518                                                                    NameStr(tupdesc->attrs[i]->attname),
519                                                                    anyelement_type,
520                                                                    -1,
521                                                                    0);
522                                 TupleDescInitEntryCollation(tupdesc, i + 1, anycollation);
523                                 break;
524                         case ANYARRAYOID:
525                                 TupleDescInitEntry(tupdesc, i + 1,
526                                                                    NameStr(tupdesc->attrs[i]->attname),
527                                                                    anyarray_type,
528                                                                    -1,
529                                                                    0);
530                                 TupleDescInitEntryCollation(tupdesc, i + 1, anycollation);
531                                 break;
532                         default:
533                                 break;
534                 }
535         }
536
537         return true;
538 }
539
540 /*
541  * Given the declared argument types and modes for a function, replace any
542  * polymorphic types (ANYELEMENT etc) with correct data types deduced from the
543  * input arguments.  Returns TRUE if able to deduce all types, FALSE if not.
544  * This is the same logic as resolve_polymorphic_tupdesc, but with a different
545  * argument representation.
546  *
547  * argmodes may be NULL, in which case all arguments are assumed to be IN mode.
548  */
549 bool
550 resolve_polymorphic_argtypes(int numargs, Oid *argtypes, char *argmodes,
551                                                          Node *call_expr)
552 {
553         bool            have_anyelement_result = false;
554         bool            have_anyarray_result = false;
555         Oid                     anyelement_type = InvalidOid;
556         Oid                     anyarray_type = InvalidOid;
557         int                     inargno;
558         int                     i;
559
560         /* First pass: resolve polymorphic inputs, check for outputs */
561         inargno = 0;
562         for (i = 0; i < numargs; i++)
563         {
564                 char            argmode = argmodes ? argmodes[i] : PROARGMODE_IN;
565
566                 switch (argtypes[i])
567                 {
568                         case ANYELEMENTOID:
569                         case ANYNONARRAYOID:
570                         case ANYENUMOID:
571                                 if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
572                                         have_anyelement_result = true;
573                                 else
574                                 {
575                                         if (!OidIsValid(anyelement_type))
576                                         {
577                                                 anyelement_type = get_call_expr_argtype(call_expr,
578                                                                                                                                 inargno);
579                                                 if (!OidIsValid(anyelement_type))
580                                                         return false;
581                                         }
582                                         argtypes[i] = anyelement_type;
583                                 }
584                                 break;
585                         case ANYARRAYOID:
586                                 if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
587                                         have_anyarray_result = true;
588                                 else
589                                 {
590                                         if (!OidIsValid(anyarray_type))
591                                         {
592                                                 anyarray_type = get_call_expr_argtype(call_expr,
593                                                                                                                           inargno);
594                                                 if (!OidIsValid(anyarray_type))
595                                                         return false;
596                                         }
597                                         argtypes[i] = anyarray_type;
598                                 }
599                                 break;
600                         default:
601                                 break;
602                 }
603                 if (argmode != PROARGMODE_OUT && argmode != PROARGMODE_TABLE)
604                         inargno++;
605         }
606
607         /* Done? */
608         if (!have_anyelement_result && !have_anyarray_result)
609                 return true;
610
611         /* If no input polymorphics, parser messed up */
612         if (!OidIsValid(anyelement_type) && !OidIsValid(anyarray_type))
613                 return false;
614
615         /* If needed, deduce one polymorphic type from the other */
616         if (have_anyelement_result && !OidIsValid(anyelement_type))
617                 anyelement_type = resolve_generic_type(ANYELEMENTOID,
618                                                                                            anyarray_type,
619                                                                                            ANYARRAYOID);
620         if (have_anyarray_result && !OidIsValid(anyarray_type))
621                 anyarray_type = resolve_generic_type(ANYARRAYOID,
622                                                                                          anyelement_type,
623                                                                                          ANYELEMENTOID);
624
625         /* XXX do we need to enforce ANYNONARRAY or ANYENUM here?  I think not */
626
627         /* And finally replace the output column types as needed */
628         for (i = 0; i < numargs; i++)
629         {
630                 switch (argtypes[i])
631                 {
632                         case ANYELEMENTOID:
633                         case ANYNONARRAYOID:
634                         case ANYENUMOID:
635                                 argtypes[i] = anyelement_type;
636                                 break;
637                         case ANYARRAYOID:
638                                 argtypes[i] = anyarray_type;
639                                 break;
640                         default:
641                                 break;
642                 }
643         }
644
645         return true;
646 }
647
648 /*
649  * get_type_func_class
650  *              Given the type OID, obtain its TYPEFUNC classification.
651  *
652  * This is intended to centralize a bunch of formerly ad-hoc code for
653  * classifying types.  The categories used here are useful for deciding
654  * how to handle functions returning the datatype.
655  */
656 static TypeFuncClass
657 get_type_func_class(Oid typid)
658 {
659         switch (get_typtype(typid))
660         {
661                 case TYPTYPE_COMPOSITE:
662                         return TYPEFUNC_COMPOSITE;
663                 case TYPTYPE_BASE:
664                 case TYPTYPE_DOMAIN:
665                 case TYPTYPE_ENUM:
666                         return TYPEFUNC_SCALAR;
667                 case TYPTYPE_PSEUDO:
668                         if (typid == RECORDOID)
669                                 return TYPEFUNC_RECORD;
670
671                         /*
672                          * We treat VOID and CSTRING as legitimate scalar datatypes,
673                          * mostly for the convenience of the JDBC driver (which wants to
674                          * be able to do "SELECT * FROM foo()" for all legitimately
675                          * user-callable functions).
676                          */
677                         if (typid == VOIDOID || typid == CSTRINGOID)
678                                 return TYPEFUNC_SCALAR;
679                         return TYPEFUNC_OTHER;
680         }
681         /* shouldn't get here, probably */
682         return TYPEFUNC_OTHER;
683 }
684
685
686 /*
687  * get_func_arg_info
688  *
689  * Fetch info about the argument types, names, and IN/OUT modes from the
690  * pg_proc tuple.  Return value is the total number of arguments.
691  * Other results are palloc'd.  *p_argtypes is always filled in, but
692  * *p_argnames and *p_argmodes will be set NULL in the default cases
693  * (no names, and all IN arguments, respectively).
694  *
695  * Note that this function simply fetches what is in the pg_proc tuple;
696  * it doesn't do any interpretation of polymorphic types.
697  */
698 int
699 get_func_arg_info(HeapTuple procTup,
700                                   Oid **p_argtypes, char ***p_argnames, char **p_argmodes)
701 {
702         Form_pg_proc procStruct = (Form_pg_proc) GETSTRUCT(procTup);
703         Datum           proallargtypes;
704         Datum           proargmodes;
705         Datum           proargnames;
706         bool            isNull;
707         ArrayType  *arr;
708         int                     numargs;
709         Datum      *elems;
710         int                     nelems;
711         int                     i;
712
713         /* First discover the total number of parameters and get their types */
714         proallargtypes = SysCacheGetAttr(PROCOID, procTup,
715                                                                          Anum_pg_proc_proallargtypes,
716                                                                          &isNull);
717         if (!isNull)
718         {
719                 /*
720                  * We expect the arrays to be 1-D arrays of the right types; verify
721                  * that.  For the OID and char arrays, we don't need to use
722                  * deconstruct_array() since the array data is just going to look like
723                  * a C array of values.
724                  */
725                 arr = DatumGetArrayTypeP(proallargtypes);               /* ensure not toasted */
726                 numargs = ARR_DIMS(arr)[0];
727                 if (ARR_NDIM(arr) != 1 ||
728                         numargs < 0 ||
729                         ARR_HASNULL(arr) ||
730                         ARR_ELEMTYPE(arr) != OIDOID)
731                         elog(ERROR, "proallargtypes is not a 1-D Oid array");
732                 Assert(numargs >= procStruct->pronargs);
733                 *p_argtypes = (Oid *) palloc(numargs * sizeof(Oid));
734                 memcpy(*p_argtypes, ARR_DATA_PTR(arr),
735                            numargs * sizeof(Oid));
736         }
737         else
738         {
739                 /* If no proallargtypes, use proargtypes */
740                 numargs = procStruct->proargtypes.dim1;
741                 Assert(numargs == procStruct->pronargs);
742                 *p_argtypes = (Oid *) palloc(numargs * sizeof(Oid));
743                 memcpy(*p_argtypes, procStruct->proargtypes.values,
744                            numargs * sizeof(Oid));
745         }
746
747         /* Get argument names, if available */
748         proargnames = SysCacheGetAttr(PROCOID, procTup,
749                                                                   Anum_pg_proc_proargnames,
750                                                                   &isNull);
751         if (isNull)
752                 *p_argnames = NULL;
753         else
754         {
755                 deconstruct_array(DatumGetArrayTypeP(proargnames),
756                                                   TEXTOID, -1, false, 'i',
757                                                   &elems, NULL, &nelems);
758                 if (nelems != numargs)  /* should not happen */
759                         elog(ERROR, "proargnames must have the same number of elements as the function has arguments");
760                 *p_argnames = (char **) palloc(sizeof(char *) * numargs);
761                 for (i = 0; i < numargs; i++)
762                         (*p_argnames)[i] = TextDatumGetCString(elems[i]);
763         }
764
765         /* Get argument modes, if available */
766         proargmodes = SysCacheGetAttr(PROCOID, procTup,
767                                                                   Anum_pg_proc_proargmodes,
768                                                                   &isNull);
769         if (isNull)
770                 *p_argmodes = NULL;
771         else
772         {
773                 arr = DatumGetArrayTypeP(proargmodes);  /* ensure not toasted */
774                 if (ARR_NDIM(arr) != 1 ||
775                         ARR_DIMS(arr)[0] != numargs ||
776                         ARR_HASNULL(arr) ||
777                         ARR_ELEMTYPE(arr) != CHAROID)
778                         elog(ERROR, "proargmodes is not a 1-D char array");
779                 *p_argmodes = (char *) palloc(numargs * sizeof(char));
780                 memcpy(*p_argmodes, ARR_DATA_PTR(arr),
781                            numargs * sizeof(char));
782         }
783
784         return numargs;
785 }
786
787
788 /*
789  * get_func_input_arg_names
790  *
791  * Extract the names of input arguments only, given a function's
792  * proargnames and proargmodes entries in Datum form.
793  *
794  * Returns the number of input arguments, which is the length of the
795  * palloc'd array returned to *arg_names.  Entries for unnamed args
796  * are set to NULL.  You don't get anything if proargnames is NULL.
797  */
798 int
799 get_func_input_arg_names(Datum proargnames, Datum proargmodes,
800                                                  char ***arg_names)
801 {
802         ArrayType  *arr;
803         int                     numargs;
804         Datum      *argnames;
805         char       *argmodes;
806         char      **inargnames;
807         int                     numinargs;
808         int                     i;
809
810         /* Do nothing if null proargnames */
811         if (proargnames == PointerGetDatum(NULL))
812         {
813                 *arg_names = NULL;
814                 return 0;
815         }
816
817         /*
818          * We expect the arrays to be 1-D arrays of the right types; verify that.
819          * For proargmodes, we don't need to use deconstruct_array() since the
820          * array data is just going to look like a C array of values.
821          */
822         arr = DatumGetArrayTypeP(proargnames);          /* ensure not toasted */
823         if (ARR_NDIM(arr) != 1 ||
824                 ARR_HASNULL(arr) ||
825                 ARR_ELEMTYPE(arr) != TEXTOID)
826                 elog(ERROR, "proargnames is not a 1-D text array");
827         deconstruct_array(arr, TEXTOID, -1, false, 'i',
828                                           &argnames, NULL, &numargs);
829         if (proargmodes != PointerGetDatum(NULL))
830         {
831                 arr = DatumGetArrayTypeP(proargmodes);  /* ensure not toasted */
832                 if (ARR_NDIM(arr) != 1 ||
833                         ARR_DIMS(arr)[0] != numargs ||
834                         ARR_HASNULL(arr) ||
835                         ARR_ELEMTYPE(arr) != CHAROID)
836                         elog(ERROR, "proargmodes is not a 1-D char array");
837                 argmodes = (char *) ARR_DATA_PTR(arr);
838         }
839         else
840                 argmodes = NULL;
841
842         /* zero elements probably shouldn't happen, but handle it gracefully */
843         if (numargs <= 0)
844         {
845                 *arg_names = NULL;
846                 return 0;
847         }
848
849         /* extract input-argument names */
850         inargnames = (char **) palloc(numargs * sizeof(char *));
851         numinargs = 0;
852         for (i = 0; i < numargs; i++)
853         {
854                 if (argmodes == NULL ||
855                         argmodes[i] == PROARGMODE_IN ||
856                         argmodes[i] == PROARGMODE_INOUT ||
857                         argmodes[i] == PROARGMODE_VARIADIC)
858                 {
859                         char       *pname = TextDatumGetCString(argnames[i]);
860
861                         if (pname[0] != '\0')
862                                 inargnames[numinargs] = pname;
863                         else
864                                 inargnames[numinargs] = NULL;
865                         numinargs++;
866                 }
867         }
868
869         *arg_names = inargnames;
870         return numinargs;
871 }
872
873
874 /*
875  * get_func_result_name
876  *
877  * If the function has exactly one output parameter, and that parameter
878  * is named, return the name (as a palloc'd string).  Else return NULL.
879  *
880  * This is used to determine the default output column name for functions
881  * returning scalar types.
882  */
883 char *
884 get_func_result_name(Oid functionId)
885 {
886         char       *result;
887         HeapTuple       procTuple;
888         Datum           proargmodes;
889         Datum           proargnames;
890         bool            isnull;
891         ArrayType  *arr;
892         int                     numargs;
893         char       *argmodes;
894         Datum      *argnames;
895         int                     numoutargs;
896         int                     nargnames;
897         int                     i;
898
899         /* First fetch the function's pg_proc row */
900         procTuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(functionId));
901         if (!HeapTupleIsValid(procTuple))
902                 elog(ERROR, "cache lookup failed for function %u", functionId);
903
904         /* If there are no named OUT parameters, return NULL */
905         if (heap_attisnull(procTuple, Anum_pg_proc_proargmodes) ||
906                 heap_attisnull(procTuple, Anum_pg_proc_proargnames))
907                 result = NULL;
908         else
909         {
910                 /* Get the data out of the tuple */
911                 proargmodes = SysCacheGetAttr(PROCOID, procTuple,
912                                                                           Anum_pg_proc_proargmodes,
913                                                                           &isnull);
914                 Assert(!isnull);
915                 proargnames = SysCacheGetAttr(PROCOID, procTuple,
916                                                                           Anum_pg_proc_proargnames,
917                                                                           &isnull);
918                 Assert(!isnull);
919
920                 /*
921                  * We expect the arrays to be 1-D arrays of the right types; verify
922                  * that.  For the char array, we don't need to use deconstruct_array()
923                  * since the array data is just going to look like a C array of
924                  * values.
925                  */
926                 arr = DatumGetArrayTypeP(proargmodes);  /* ensure not toasted */
927                 numargs = ARR_DIMS(arr)[0];
928                 if (ARR_NDIM(arr) != 1 ||
929                         numargs < 0 ||
930                         ARR_HASNULL(arr) ||
931                         ARR_ELEMTYPE(arr) != CHAROID)
932                         elog(ERROR, "proargmodes is not a 1-D char array");
933                 argmodes = (char *) ARR_DATA_PTR(arr);
934                 arr = DatumGetArrayTypeP(proargnames);  /* ensure not toasted */
935                 if (ARR_NDIM(arr) != 1 ||
936                         ARR_DIMS(arr)[0] != numargs ||
937                         ARR_HASNULL(arr) ||
938                         ARR_ELEMTYPE(arr) != TEXTOID)
939                         elog(ERROR, "proargnames is not a 1-D text array");
940                 deconstruct_array(arr, TEXTOID, -1, false, 'i',
941                                                   &argnames, NULL, &nargnames);
942                 Assert(nargnames == numargs);
943
944                 /* scan for output argument(s) */
945                 result = NULL;
946                 numoutargs = 0;
947                 for (i = 0; i < numargs; i++)
948                 {
949                         if (argmodes[i] == PROARGMODE_IN ||
950                                 argmodes[i] == PROARGMODE_VARIADIC)
951                                 continue;
952                         Assert(argmodes[i] == PROARGMODE_OUT ||
953                                    argmodes[i] == PROARGMODE_INOUT ||
954                                    argmodes[i] == PROARGMODE_TABLE);
955                         if (++numoutargs > 1)
956                         {
957                                 /* multiple out args, so forget it */
958                                 result = NULL;
959                                 break;
960                         }
961                         result = TextDatumGetCString(argnames[i]);
962                         if (result == NULL || result[0] == '\0')
963                         {
964                                 /* Parameter is not named, so forget it */
965                                 result = NULL;
966                                 break;
967                         }
968                 }
969         }
970
971         ReleaseSysCache(procTuple);
972
973         return result;
974 }
975
976
977 /*
978  * build_function_result_tupdesc_t
979  *
980  * Given a pg_proc row for a function, return a tuple descriptor for the
981  * result rowtype, or NULL if the function does not have OUT parameters.
982  *
983  * Note that this does not handle resolution of polymorphic types;
984  * that is deliberate.
985  */
986 TupleDesc
987 build_function_result_tupdesc_t(HeapTuple procTuple)
988 {
989         Form_pg_proc procform = (Form_pg_proc) GETSTRUCT(procTuple);
990         Datum           proallargtypes;
991         Datum           proargmodes;
992         Datum           proargnames;
993         bool            isnull;
994
995         /* Return NULL if the function isn't declared to return RECORD */
996         if (procform->prorettype != RECORDOID)
997                 return NULL;
998
999         /* If there are no OUT parameters, return NULL */
1000         if (heap_attisnull(procTuple, Anum_pg_proc_proallargtypes) ||
1001                 heap_attisnull(procTuple, Anum_pg_proc_proargmodes))
1002                 return NULL;
1003
1004         /* Get the data out of the tuple */
1005         proallargtypes = SysCacheGetAttr(PROCOID, procTuple,
1006                                                                          Anum_pg_proc_proallargtypes,
1007                                                                          &isnull);
1008         Assert(!isnull);
1009         proargmodes = SysCacheGetAttr(PROCOID, procTuple,
1010                                                                   Anum_pg_proc_proargmodes,
1011                                                                   &isnull);
1012         Assert(!isnull);
1013         proargnames = SysCacheGetAttr(PROCOID, procTuple,
1014                                                                   Anum_pg_proc_proargnames,
1015                                                                   &isnull);
1016         if (isnull)
1017                 proargnames = PointerGetDatum(NULL);    /* just to be sure */
1018
1019         return build_function_result_tupdesc_d(proallargtypes,
1020                                                                                    proargmodes,
1021                                                                                    proargnames);
1022 }
1023
1024 /*
1025  * build_function_result_tupdesc_d
1026  *
1027  * Build a RECORD function's tupledesc from the pg_proc proallargtypes,
1028  * proargmodes, and proargnames arrays.  This is split out for the
1029  * convenience of ProcedureCreate, which needs to be able to compute the
1030  * tupledesc before actually creating the function.
1031  *
1032  * Returns NULL if there are not at least two OUT or INOUT arguments.
1033  */
1034 TupleDesc
1035 build_function_result_tupdesc_d(Datum proallargtypes,
1036                                                                 Datum proargmodes,
1037                                                                 Datum proargnames)
1038 {
1039         TupleDesc       desc;
1040         ArrayType  *arr;
1041         int                     numargs;
1042         Oid                *argtypes;
1043         char       *argmodes;
1044         Datum      *argnames = NULL;
1045         Oid                *outargtypes;
1046         char      **outargnames;
1047         int                     numoutargs;
1048         int                     nargnames;
1049         int                     i;
1050
1051         /* Can't have output args if columns are null */
1052         if (proallargtypes == PointerGetDatum(NULL) ||
1053                 proargmodes == PointerGetDatum(NULL))
1054                 return NULL;
1055
1056         /*
1057          * We expect the arrays to be 1-D arrays of the right types; verify that.
1058          * For the OID and char arrays, we don't need to use deconstruct_array()
1059          * since the array data is just going to look like a C array of values.
1060          */
1061         arr = DatumGetArrayTypeP(proallargtypes);       /* 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) != OIDOID)
1067                 elog(ERROR, "proallargtypes is not a 1-D Oid array");
1068         argtypes = (Oid *) ARR_DATA_PTR(arr);
1069         arr = DatumGetArrayTypeP(proargmodes);          /* ensure not toasted */
1070         if (ARR_NDIM(arr) != 1 ||
1071                 ARR_DIMS(arr)[0] != numargs ||
1072                 ARR_HASNULL(arr) ||
1073                 ARR_ELEMTYPE(arr) != CHAROID)
1074                 elog(ERROR, "proargmodes is not a 1-D char array");
1075         argmodes = (char *) ARR_DATA_PTR(arr);
1076         if (proargnames != PointerGetDatum(NULL))
1077         {
1078                 arr = DatumGetArrayTypeP(proargnames);  /* ensure not toasted */
1079                 if (ARR_NDIM(arr) != 1 ||
1080                         ARR_DIMS(arr)[0] != numargs ||
1081                         ARR_HASNULL(arr) ||
1082                         ARR_ELEMTYPE(arr) != TEXTOID)
1083                         elog(ERROR, "proargnames is not a 1-D text array");
1084                 deconstruct_array(arr, TEXTOID, -1, false, 'i',
1085                                                   &argnames, NULL, &nargnames);
1086                 Assert(nargnames == numargs);
1087         }
1088
1089         /* zero elements probably shouldn't happen, but handle it gracefully */
1090         if (numargs <= 0)
1091                 return NULL;
1092
1093         /* extract output-argument types and names */
1094         outargtypes = (Oid *) palloc(numargs * sizeof(Oid));
1095         outargnames = (char **) palloc(numargs * sizeof(char *));
1096         numoutargs = 0;
1097         for (i = 0; i < numargs; i++)
1098         {
1099                 char       *pname;
1100
1101                 if (argmodes[i] == PROARGMODE_IN ||
1102                         argmodes[i] == PROARGMODE_VARIADIC)
1103                         continue;
1104                 Assert(argmodes[i] == PROARGMODE_OUT ||
1105                            argmodes[i] == PROARGMODE_INOUT ||
1106                            argmodes[i] == PROARGMODE_TABLE);
1107                 outargtypes[numoutargs] = argtypes[i];
1108                 if (argnames)
1109                         pname = TextDatumGetCString(argnames[i]);
1110                 else
1111                         pname = NULL;
1112                 if (pname == NULL || pname[0] == '\0')
1113                 {
1114                         /* Parameter is not named, so gin up a column name */
1115                         pname = (char *) palloc(32);
1116                         snprintf(pname, 32, "column%d", numoutargs + 1);
1117                 }
1118                 outargnames[numoutargs] = pname;
1119                 numoutargs++;
1120         }
1121
1122         /*
1123          * If there is no output argument, or only one, the function does not
1124          * return tuples.
1125          */
1126         if (numoutargs < 2)
1127                 return NULL;
1128
1129         desc = CreateTemplateTupleDesc(numoutargs, false);
1130         for (i = 0; i < numoutargs; i++)
1131         {
1132                 TupleDescInitEntry(desc, i + 1,
1133                                                    outargnames[i],
1134                                                    outargtypes[i],
1135                                                    -1,
1136                                                    0);
1137         }
1138
1139         return desc;
1140 }
1141
1142
1143 /*
1144  * RelationNameGetTupleDesc
1145  *
1146  * Given a (possibly qualified) relation name, build a TupleDesc.
1147  *
1148  * Note: while this works as advertised, it's seldom the best way to
1149  * build a tupdesc for a function's result type.  It's kept around
1150  * only for backwards compatibility with existing user-written code.
1151  */
1152 TupleDesc
1153 RelationNameGetTupleDesc(const char *relname)
1154 {
1155         RangeVar   *relvar;
1156         Relation        rel;
1157         TupleDesc       tupdesc;
1158         List       *relname_list;
1159
1160         /* Open relation and copy the tuple description */
1161         relname_list = stringToQualifiedNameList(relname);
1162         relvar = makeRangeVarFromNameList(relname_list);
1163         rel = relation_openrv(relvar, AccessShareLock);
1164         tupdesc = CreateTupleDescCopy(RelationGetDescr(rel));
1165         relation_close(rel, AccessShareLock);
1166
1167         return tupdesc;
1168 }
1169
1170 /*
1171  * TypeGetTupleDesc
1172  *
1173  * Given a type Oid, build a TupleDesc.  (In most cases you should be
1174  * using get_call_result_type or one of its siblings instead of this
1175  * routine, so that you can handle OUT parameters, RECORD result type,
1176  * and polymorphic results.)
1177  *
1178  * If the type is composite, *and* a colaliases List is provided, *and*
1179  * the List is of natts length, use the aliases instead of the relation
1180  * attnames.  (NB: this usage is deprecated since it may result in
1181  * creation of unnecessary transient record types.)
1182  *
1183  * If the type is a base type, a single item alias List is required.
1184  */
1185 TupleDesc
1186 TypeGetTupleDesc(Oid typeoid, List *colaliases)
1187 {
1188         TypeFuncClass functypclass = get_type_func_class(typeoid);
1189         TupleDesc       tupdesc = NULL;
1190
1191         /*
1192          * Build a suitable tupledesc representing the output rows
1193          */
1194         if (functypclass == TYPEFUNC_COMPOSITE)
1195         {
1196                 /* Composite data type, e.g. a table's row type */
1197                 tupdesc = lookup_rowtype_tupdesc_copy(typeoid, -1);
1198
1199                 if (colaliases != NIL)
1200                 {
1201                         int                     natts = tupdesc->natts;
1202                         int                     varattno;
1203
1204                         /* does the list length match the number of attributes? */
1205                         if (list_length(colaliases) != natts)
1206                                 ereport(ERROR,
1207                                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
1208                                                  errmsg("number of aliases does not match number of columns")));
1209
1210                         /* OK, use the aliases instead */
1211                         for (varattno = 0; varattno < natts; varattno++)
1212                         {
1213                                 char       *label = strVal(list_nth(colaliases, varattno));
1214
1215                                 if (label != NULL)
1216                                         namestrcpy(&(tupdesc->attrs[varattno]->attname), label);
1217                         }
1218
1219                         /* The tuple type is now an anonymous record type */
1220                         tupdesc->tdtypeid = RECORDOID;
1221                         tupdesc->tdtypmod = -1;
1222                 }
1223         }
1224         else if (functypclass == TYPEFUNC_SCALAR)
1225         {
1226                 /* Base data type, i.e. scalar */
1227                 char       *attname;
1228
1229                 /* the alias list is required for base types */
1230                 if (colaliases == NIL)
1231                         ereport(ERROR,
1232                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
1233                                          errmsg("no column alias was provided")));
1234
1235                 /* the alias list length must be 1 */
1236                 if (list_length(colaliases) != 1)
1237                         ereport(ERROR,
1238                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
1239                           errmsg("number of aliases does not match number of columns")));
1240
1241                 /* OK, get the column alias */
1242                 attname = strVal(linitial(colaliases));
1243
1244                 tupdesc = CreateTemplateTupleDesc(1, false);
1245                 TupleDescInitEntry(tupdesc,
1246                                                    (AttrNumber) 1,
1247                                                    attname,
1248                                                    typeoid,
1249                                                    -1,
1250                                                    0);
1251         }
1252         else if (functypclass == TYPEFUNC_RECORD)
1253         {
1254                 /* XXX can't support this because typmod wasn't passed in ... */
1255                 ereport(ERROR,
1256                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
1257                                  errmsg("could not determine row description for function returning record")));
1258         }
1259         else
1260         {
1261                 /* crummy error message, but parser should have caught this */
1262                 elog(ERROR, "function in FROM has unsupported return type");
1263         }
1264
1265         return tupdesc;
1266 }