PostgreSQL Source Code  git master
funcapi.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * funcapi.c
4  * Utility and convenience functions for fmgr functions that return
5  * sets and/or composite types, or deal with VARIADIC inputs.
6  *
7  * Copyright (c) 2002-2019, PostgreSQL Global Development Group
8  *
9  * IDENTIFICATION
10  * src/backend/utils/fmgr/funcapi.c
11  *
12  *-------------------------------------------------------------------------
13  */
14 #include "postgres.h"
15 
16 #include "access/htup_details.h"
17 #include "access/relation.h"
18 #include "catalog/namespace.h"
19 #include "catalog/pg_proc.h"
20 #include "catalog/pg_type.h"
21 #include "funcapi.h"
22 #include "nodes/nodeFuncs.h"
23 #include "parser/parse_coerce.h"
24 #include "utils/array.h"
25 #include "utils/builtins.h"
26 #include "utils/lsyscache.h"
27 #include "utils/memutils.h"
28 #include "utils/regproc.h"
29 #include "utils/rel.h"
30 #include "utils/syscache.h"
31 #include "utils/typcache.h"
32 
33 
34 static void shutdown_MultiFuncCall(Datum arg);
36  Node *call_expr,
37  ReturnSetInfo *rsinfo,
38  Oid *resultTypeId,
39  TupleDesc *resultTupleDesc);
40 static bool resolve_polymorphic_tupdesc(TupleDesc tupdesc,
41  oidvector *declared_args,
42  Node *call_expr);
43 static TypeFuncClass get_type_func_class(Oid typid, Oid *base_typeid);
44 
45 
46 /*
47  * init_MultiFuncCall
48  * Create an empty FuncCallContext data structure
49  * and do some other basic Multi-function call setup
50  * and error checking
51  */
54 {
55  FuncCallContext *retval;
56 
57  /*
58  * Bail if we're called in the wrong context
59  */
60  if (fcinfo->resultinfo == NULL || !IsA(fcinfo->resultinfo, ReturnSetInfo))
61  ereport(ERROR,
62  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
63  errmsg("set-valued function called in context that cannot accept a set")));
64 
65  if (fcinfo->flinfo->fn_extra == NULL)
66  {
67  /*
68  * First call
69  */
70  ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
71  MemoryContext multi_call_ctx;
72 
73  /*
74  * Create a suitably long-lived context to hold cross-call data
75  */
76  multi_call_ctx = AllocSetContextCreate(fcinfo->flinfo->fn_mcxt,
77  "SRF multi-call context",
79 
80  /*
81  * Allocate suitably long-lived space and zero it
82  */
83  retval = (FuncCallContext *)
84  MemoryContextAllocZero(multi_call_ctx,
85  sizeof(FuncCallContext));
86 
87  /*
88  * initialize the elements
89  */
90  retval->call_cntr = 0;
91  retval->max_calls = 0;
92  retval->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  */
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  */
129 {
130  FuncCallContext *retval = (FuncCallContext *) fcinfo->flinfo->fn_extra;
131 
132  return retval;
133 }
134 
135 /*
136  * end_MultiFuncCall
137  * Clean up after init_MultiFuncCall
138  */
139 void
141 {
142  ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
143 
144  /* Deregister the shutdown callback */
147  PointerGetDatum(fcinfo->flinfo));
148 
149  /* But use it to do the real work */
150  shutdown_MultiFuncCall(PointerGetDatum(fcinfo->flinfo));
151 }
152 
153 /*
154  * shutdown_MultiFuncCall
155  * Shutdown function to clean up after init_MultiFuncCall
156  */
157 static void
159 {
160  FmgrInfo *flinfo = (FmgrInfo *) DatumGetPointer(arg);
161  FuncCallContext *funcctx = (FuncCallContext *) flinfo->fn_extra;
162 
163  /* unbind from flinfo */
164  flinfo->fn_extra = NULL;
165 
166  /*
167  * Delete context that holds all multi-call data, including the
168  * FuncCallContext itself
169  */
171 }
172 
173 
174 /*
175  * get_call_result_type
176  * Given a function's call info record, determine the kind of datatype
177  * it is supposed to return. If resultTypeId isn't NULL, *resultTypeId
178  * receives the actual datatype OID (this is mainly useful for scalar
179  * result types). If resultTupleDesc isn't NULL, *resultTupleDesc
180  * receives a pointer to a TupleDesc when the result is of a composite
181  * type, or NULL when it's a scalar result.
182  *
183  * One hard case that this handles is resolution of actual rowtypes for
184  * functions returning RECORD (from either the function's OUT parameter
185  * list, or a ReturnSetInfo context node). TYPEFUNC_RECORD is returned
186  * only when we couldn't resolve the actual rowtype for lack of information.
187  *
188  * The other hard case that this handles is resolution of polymorphism.
189  * We will never return polymorphic pseudotypes (ANYELEMENT etc), either
190  * as a scalar result type or as a component of a rowtype.
191  *
192  * This function is relatively expensive --- in a function returning set,
193  * try to call it only the first time through.
194  */
197  Oid *resultTypeId,
198  TupleDesc *resultTupleDesc)
199 {
200  return internal_get_result_type(fcinfo->flinfo->fn_oid,
201  fcinfo->flinfo->fn_expr,
202  (ReturnSetInfo *) fcinfo->resultinfo,
203  resultTypeId,
204  resultTupleDesc);
205 }
206 
207 /*
208  * get_expr_result_type
209  * As above, but work from a calling expression node tree
210  */
213  Oid *resultTypeId,
214  TupleDesc *resultTupleDesc)
215 {
216  TypeFuncClass result;
217 
218  if (expr && IsA(expr, FuncExpr))
219  result = internal_get_result_type(((FuncExpr *) expr)->funcid,
220  expr,
221  NULL,
222  resultTypeId,
223  resultTupleDesc);
224  else if (expr && IsA(expr, OpExpr))
225  result = internal_get_result_type(get_opcode(((OpExpr *) expr)->opno),
226  expr,
227  NULL,
228  resultTypeId,
229  resultTupleDesc);
230  else if (expr && IsA(expr, RowExpr) &&
231  ((RowExpr *) expr)->row_typeid == RECORDOID)
232  {
233  /* We can resolve the record type by generating the tupdesc directly */
234  RowExpr *rexpr = (RowExpr *) expr;
235  TupleDesc tupdesc;
236  AttrNumber i = 1;
237  ListCell *lcc,
238  *lcn;
239 
240  tupdesc = CreateTemplateTupleDesc(list_length(rexpr->args));
241  Assert(list_length(rexpr->args) == list_length(rexpr->colnames));
242  forboth(lcc, rexpr->args, lcn, rexpr->colnames)
243  {
244  Node *col = (Node *) lfirst(lcc);
245  char *colname = strVal(lfirst(lcn));
246 
247  TupleDescInitEntry(tupdesc, i,
248  colname,
249  exprType(col),
250  exprTypmod(col),
251  0);
252  TupleDescInitEntryCollation(tupdesc, i,
253  exprCollation(col));
254  i++;
255  }
256  if (resultTypeId)
257  *resultTypeId = rexpr->row_typeid;
258  if (resultTupleDesc)
259  *resultTupleDesc = BlessTupleDesc(tupdesc);
260  return TYPEFUNC_COMPOSITE;
261  }
262  else
263  {
264  /* handle as a generic expression; no chance to resolve RECORD */
265  Oid typid = exprType(expr);
266  Oid base_typid;
267 
268  if (resultTypeId)
269  *resultTypeId = typid;
270  if (resultTupleDesc)
271  *resultTupleDesc = NULL;
272  result = get_type_func_class(typid, &base_typid);
273  if ((result == TYPEFUNC_COMPOSITE ||
274  result == TYPEFUNC_COMPOSITE_DOMAIN) &&
275  resultTupleDesc)
276  *resultTupleDesc = lookup_rowtype_tupdesc_copy(base_typid, -1);
277  }
278 
279  return result;
280 }
281 
282 /*
283  * get_func_result_type
284  * As above, but work from a function's OID only
285  *
286  * This will not be able to resolve pure-RECORD results nor polymorphism.
287  */
290  Oid *resultTypeId,
291  TupleDesc *resultTupleDesc)
292 {
293  return internal_get_result_type(functionId,
294  NULL,
295  NULL,
296  resultTypeId,
297  resultTupleDesc);
298 }
299 
300 /*
301  * internal_get_result_type -- workhorse code implementing all the above
302  *
303  * funcid must always be supplied. call_expr and rsinfo can be NULL if not
304  * available. We will return TYPEFUNC_RECORD, and store NULL into
305  * *resultTupleDesc, if we cannot deduce the complete result rowtype from
306  * the available information.
307  */
308 static TypeFuncClass
310  Node *call_expr,
311  ReturnSetInfo *rsinfo,
312  Oid *resultTypeId,
313  TupleDesc *resultTupleDesc)
314 {
315  TypeFuncClass result;
316  HeapTuple tp;
317  Form_pg_proc procform;
318  Oid rettype;
319  Oid base_rettype;
320  TupleDesc tupdesc;
321 
322  /* First fetch the function's pg_proc row to inspect its rettype */
324  if (!HeapTupleIsValid(tp))
325  elog(ERROR, "cache lookup failed for function %u", funcid);
326  procform = (Form_pg_proc) GETSTRUCT(tp);
327 
328  rettype = procform->prorettype;
329 
330  /* Check for OUT parameters defining a RECORD result */
331  tupdesc = build_function_result_tupdesc_t(tp);
332  if (tupdesc)
333  {
334  /*
335  * It has OUT parameters, so it's basically like a regular composite
336  * type, except we have to be able to resolve any polymorphic OUT
337  * parameters.
338  */
339  if (resultTypeId)
340  *resultTypeId = rettype;
341 
342  if (resolve_polymorphic_tupdesc(tupdesc,
343  &procform->proargtypes,
344  call_expr))
345  {
346  if (tupdesc->tdtypeid == RECORDOID &&
347  tupdesc->tdtypmod < 0)
348  assign_record_type_typmod(tupdesc);
349  if (resultTupleDesc)
350  *resultTupleDesc = tupdesc;
351  result = TYPEFUNC_COMPOSITE;
352  }
353  else
354  {
355  if (resultTupleDesc)
356  *resultTupleDesc = NULL;
357  result = TYPEFUNC_RECORD;
358  }
359 
360  ReleaseSysCache(tp);
361 
362  return result;
363  }
364 
365  /*
366  * If scalar polymorphic result, try to resolve it.
367  */
368  if (IsPolymorphicType(rettype))
369  {
370  Oid newrettype = exprType(call_expr);
371 
372  if (newrettype == InvalidOid) /* this probably should not happen */
373  ereport(ERROR,
374  (errcode(ERRCODE_DATATYPE_MISMATCH),
375  errmsg("could not determine actual result type for function \"%s\" declared to return type %s",
376  NameStr(procform->proname),
377  format_type_be(rettype))));
378  rettype = newrettype;
379  }
380 
381  if (resultTypeId)
382  *resultTypeId = rettype;
383  if (resultTupleDesc)
384  *resultTupleDesc = NULL; /* default result */
385 
386  /* Classify the result type */
387  result = get_type_func_class(rettype, &base_rettype);
388  switch (result)
389  {
390  case TYPEFUNC_COMPOSITE:
392  if (resultTupleDesc)
393  *resultTupleDesc = lookup_rowtype_tupdesc_copy(base_rettype, -1);
394  /* Named composite types can't have any polymorphic columns */
395  break;
396  case TYPEFUNC_SCALAR:
397  break;
398  case TYPEFUNC_RECORD:
399  /* We must get the tupledesc from call context */
400  if (rsinfo && IsA(rsinfo, ReturnSetInfo) &&
401  rsinfo->expectedDesc != NULL)
402  {
403  result = TYPEFUNC_COMPOSITE;
404  if (resultTupleDesc)
405  *resultTupleDesc = rsinfo->expectedDesc;
406  /* Assume no polymorphic columns here, either */
407  }
408  break;
409  default:
410  break;
411  }
412 
413  ReleaseSysCache(tp);
414 
415  return result;
416 }
417 
418 /*
419  * get_expr_result_tupdesc
420  * Get a tupdesc describing the result of a composite-valued expression
421  *
422  * If expression is not composite or rowtype can't be determined, returns NULL
423  * if noError is true, else throws error.
424  *
425  * This is a simpler version of get_expr_result_type() for use when the caller
426  * is only interested in determinate rowtype results.
427  */
428 TupleDesc
429 get_expr_result_tupdesc(Node *expr, bool noError)
430 {
431  TupleDesc tupleDesc;
432  TypeFuncClass functypclass;
433 
434  functypclass = get_expr_result_type(expr, NULL, &tupleDesc);
435 
436  if (functypclass == TYPEFUNC_COMPOSITE ||
437  functypclass == TYPEFUNC_COMPOSITE_DOMAIN)
438  return tupleDesc;
439 
440  if (!noError)
441  {
442  Oid exprTypeId = exprType(expr);
443 
444  if (exprTypeId != RECORDOID)
445  ereport(ERROR,
446  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
447  errmsg("type %s is not composite",
448  format_type_be(exprTypeId))));
449  else
450  ereport(ERROR,
451  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
452  errmsg("record type has not been registered")));
453  }
454 
455  return NULL;
456 }
457 
458 /*
459  * Given the result tuple descriptor for a function with OUT parameters,
460  * replace any polymorphic columns (ANYELEMENT etc) with correct data types
461  * deduced from the input arguments. Returns true if able to deduce all types,
462  * false if not.
463  */
464 static bool
466  Node *call_expr)
467 {
468  int natts = tupdesc->natts;
469  int nargs = declared_args->dim1;
470  bool have_anyelement_result = false;
471  bool have_anyarray_result = false;
472  bool have_anyrange_result = false;
473  bool have_anynonarray = false;
474  bool have_anyenum = false;
475  Oid anyelement_type = InvalidOid;
476  Oid anyarray_type = InvalidOid;
477  Oid anyrange_type = InvalidOid;
478  Oid anycollation = InvalidOid;
479  int i;
480 
481  /* See if there are any polymorphic outputs; quick out if not */
482  for (i = 0; i < natts; i++)
483  {
484  switch (TupleDescAttr(tupdesc, i)->atttypid)
485  {
486  case ANYELEMENTOID:
487  have_anyelement_result = true;
488  break;
489  case ANYARRAYOID:
490  have_anyarray_result = true;
491  break;
492  case ANYNONARRAYOID:
493  have_anyelement_result = true;
494  have_anynonarray = true;
495  break;
496  case ANYENUMOID:
497  have_anyelement_result = true;
498  have_anyenum = true;
499  break;
500  case ANYRANGEOID:
501  have_anyrange_result = true;
502  break;
503  default:
504  break;
505  }
506  }
507  if (!have_anyelement_result && !have_anyarray_result &&
508  !have_anyrange_result)
509  return true;
510 
511  /*
512  * Otherwise, extract actual datatype(s) from input arguments. (We assume
513  * the parser already validated consistency of the arguments.)
514  */
515  if (!call_expr)
516  return false; /* no hope */
517 
518  for (i = 0; i < nargs; i++)
519  {
520  switch (declared_args->values[i])
521  {
522  case ANYELEMENTOID:
523  case ANYNONARRAYOID:
524  case ANYENUMOID:
525  if (!OidIsValid(anyelement_type))
526  anyelement_type = get_call_expr_argtype(call_expr, i);
527  break;
528  case ANYARRAYOID:
529  if (!OidIsValid(anyarray_type))
530  anyarray_type = get_call_expr_argtype(call_expr, i);
531  break;
532  case ANYRANGEOID:
533  if (!OidIsValid(anyrange_type))
534  anyrange_type = get_call_expr_argtype(call_expr, i);
535  break;
536  default:
537  break;
538  }
539  }
540 
541  /* If nothing found, parser messed up */
542  if (!OidIsValid(anyelement_type) && !OidIsValid(anyarray_type) &&
543  !OidIsValid(anyrange_type))
544  return false;
545 
546  /* If needed, deduce one polymorphic type from others */
547  if (have_anyelement_result && !OidIsValid(anyelement_type))
548  {
549  if (OidIsValid(anyarray_type))
550  anyelement_type = resolve_generic_type(ANYELEMENTOID,
551  anyarray_type,
552  ANYARRAYOID);
553  if (OidIsValid(anyrange_type))
554  {
555  Oid subtype = resolve_generic_type(ANYELEMENTOID,
556  anyrange_type,
557  ANYRANGEOID);
558 
559  /* check for inconsistent array and range results */
560  if (OidIsValid(anyelement_type) && anyelement_type != subtype)
561  return false;
562  anyelement_type = subtype;
563  }
564  }
565 
566  if (have_anyarray_result && !OidIsValid(anyarray_type))
567  anyarray_type = resolve_generic_type(ANYARRAYOID,
568  anyelement_type,
569  ANYELEMENTOID);
570 
571  /*
572  * We can't deduce a range type from other polymorphic inputs, because
573  * there may be multiple range types for the same subtype.
574  */
575  if (have_anyrange_result && !OidIsValid(anyrange_type))
576  return false;
577 
578  /* Enforce ANYNONARRAY if needed */
579  if (have_anynonarray && type_is_array(anyelement_type))
580  return false;
581 
582  /* Enforce ANYENUM if needed */
583  if (have_anyenum && !type_is_enum(anyelement_type))
584  return false;
585 
586  /*
587  * Identify the collation to use for polymorphic OUT parameters. (It'll
588  * necessarily be the same for both anyelement and anyarray.) Note that
589  * range types are not collatable, so any possible internal collation of a
590  * range type is not considered here.
591  */
592  if (OidIsValid(anyelement_type))
593  anycollation = get_typcollation(anyelement_type);
594  else if (OidIsValid(anyarray_type))
595  anycollation = get_typcollation(anyarray_type);
596 
597  if (OidIsValid(anycollation))
598  {
599  /*
600  * The types are collatable, so consider whether to use a nondefault
601  * collation. We do so if we can identify the input collation used
602  * for the function.
603  */
604  Oid inputcollation = exprInputCollation(call_expr);
605 
606  if (OidIsValid(inputcollation))
607  anycollation = inputcollation;
608  }
609 
610  /* And finally replace the tuple column types as needed */
611  for (i = 0; i < natts; i++)
612  {
613  Form_pg_attribute att = TupleDescAttr(tupdesc, i);
614 
615  switch (att->atttypid)
616  {
617  case ANYELEMENTOID:
618  case ANYNONARRAYOID:
619  case ANYENUMOID:
620  TupleDescInitEntry(tupdesc, i + 1,
621  NameStr(att->attname),
622  anyelement_type,
623  -1,
624  0);
625  TupleDescInitEntryCollation(tupdesc, i + 1, anycollation);
626  break;
627  case ANYARRAYOID:
628  TupleDescInitEntry(tupdesc, i + 1,
629  NameStr(att->attname),
630  anyarray_type,
631  -1,
632  0);
633  TupleDescInitEntryCollation(tupdesc, i + 1, anycollation);
634  break;
635  case ANYRANGEOID:
636  TupleDescInitEntry(tupdesc, i + 1,
637  NameStr(att->attname),
638  anyrange_type,
639  -1,
640  0);
641  /* no collation should be attached to a range type */
642  break;
643  default:
644  break;
645  }
646  }
647 
648  return true;
649 }
650 
651 /*
652  * Given the declared argument types and modes for a function, replace any
653  * polymorphic types (ANYELEMENT etc) with correct data types deduced from the
654  * input arguments. Returns true if able to deduce all types, false if not.
655  * This is the same logic as resolve_polymorphic_tupdesc, but with a different
656  * argument representation.
657  *
658  * argmodes may be NULL, in which case all arguments are assumed to be IN mode.
659  */
660 bool
661 resolve_polymorphic_argtypes(int numargs, Oid *argtypes, char *argmodes,
662  Node *call_expr)
663 {
664  bool have_anyelement_result = false;
665  bool have_anyarray_result = false;
666  bool have_anyrange_result = false;
667  Oid anyelement_type = InvalidOid;
668  Oid anyarray_type = InvalidOid;
669  Oid anyrange_type = InvalidOid;
670  int inargno;
671  int i;
672 
673  /* First pass: resolve polymorphic inputs, check for outputs */
674  inargno = 0;
675  for (i = 0; i < numargs; i++)
676  {
677  char argmode = argmodes ? argmodes[i] : PROARGMODE_IN;
678 
679  switch (argtypes[i])
680  {
681  case ANYELEMENTOID:
682  case ANYNONARRAYOID:
683  case ANYENUMOID:
684  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
685  have_anyelement_result = true;
686  else
687  {
688  if (!OidIsValid(anyelement_type))
689  {
690  anyelement_type = get_call_expr_argtype(call_expr,
691  inargno);
692  if (!OidIsValid(anyelement_type))
693  return false;
694  }
695  argtypes[i] = anyelement_type;
696  }
697  break;
698  case ANYARRAYOID:
699  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
700  have_anyarray_result = true;
701  else
702  {
703  if (!OidIsValid(anyarray_type))
704  {
705  anyarray_type = get_call_expr_argtype(call_expr,
706  inargno);
707  if (!OidIsValid(anyarray_type))
708  return false;
709  }
710  argtypes[i] = anyarray_type;
711  }
712  break;
713  case ANYRANGEOID:
714  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
715  have_anyrange_result = true;
716  else
717  {
718  if (!OidIsValid(anyrange_type))
719  {
720  anyrange_type = get_call_expr_argtype(call_expr,
721  inargno);
722  if (!OidIsValid(anyrange_type))
723  return false;
724  }
725  argtypes[i] = anyrange_type;
726  }
727  break;
728  default:
729  break;
730  }
731  if (argmode != PROARGMODE_OUT && argmode != PROARGMODE_TABLE)
732  inargno++;
733  }
734 
735  /* Done? */
736  if (!have_anyelement_result && !have_anyarray_result &&
737  !have_anyrange_result)
738  return true;
739 
740  /* If no input polymorphics, parser messed up */
741  if (!OidIsValid(anyelement_type) && !OidIsValid(anyarray_type) &&
742  !OidIsValid(anyrange_type))
743  return false;
744 
745  /* If needed, deduce one polymorphic type from others */
746  if (have_anyelement_result && !OidIsValid(anyelement_type))
747  {
748  if (OidIsValid(anyarray_type))
749  anyelement_type = resolve_generic_type(ANYELEMENTOID,
750  anyarray_type,
751  ANYARRAYOID);
752  if (OidIsValid(anyrange_type))
753  {
754  Oid subtype = resolve_generic_type(ANYELEMENTOID,
755  anyrange_type,
756  ANYRANGEOID);
757 
758  /* check for inconsistent array and range results */
759  if (OidIsValid(anyelement_type) && anyelement_type != subtype)
760  return false;
761  anyelement_type = subtype;
762  }
763  }
764 
765  if (have_anyarray_result && !OidIsValid(anyarray_type))
766  anyarray_type = resolve_generic_type(ANYARRAYOID,
767  anyelement_type,
768  ANYELEMENTOID);
769 
770  /*
771  * We can't deduce a range type from other polymorphic inputs, because
772  * there may be multiple range types for the same subtype.
773  */
774  if (have_anyrange_result && !OidIsValid(anyrange_type))
775  return false;
776 
777  /* XXX do we need to enforce ANYNONARRAY or ANYENUM here? I think not */
778 
779  /* And finally replace the output column types as needed */
780  for (i = 0; i < numargs; i++)
781  {
782  switch (argtypes[i])
783  {
784  case ANYELEMENTOID:
785  case ANYNONARRAYOID:
786  case ANYENUMOID:
787  argtypes[i] = anyelement_type;
788  break;
789  case ANYARRAYOID:
790  argtypes[i] = anyarray_type;
791  break;
792  case ANYRANGEOID:
793  argtypes[i] = anyrange_type;
794  break;
795  default:
796  break;
797  }
798  }
799 
800  return true;
801 }
802 
803 /*
804  * get_type_func_class
805  * Given the type OID, obtain its TYPEFUNC classification.
806  * Also, if it's a domain, return the base type OID.
807  *
808  * This is intended to centralize a bunch of formerly ad-hoc code for
809  * classifying types. The categories used here are useful for deciding
810  * how to handle functions returning the datatype.
811  */
812 static TypeFuncClass
813 get_type_func_class(Oid typid, Oid *base_typeid)
814 {
815  *base_typeid = typid;
816 
817  switch (get_typtype(typid))
818  {
819  case TYPTYPE_COMPOSITE:
820  return TYPEFUNC_COMPOSITE;
821  case TYPTYPE_BASE:
822  case TYPTYPE_ENUM:
823  case TYPTYPE_RANGE:
824  return TYPEFUNC_SCALAR;
825  case TYPTYPE_DOMAIN:
826  *base_typeid = typid = getBaseType(typid);
827  if (get_typtype(typid) == TYPTYPE_COMPOSITE)
829  else /* domain base type can't be a pseudotype */
830  return TYPEFUNC_SCALAR;
831  case TYPTYPE_PSEUDO:
832  if (typid == RECORDOID)
833  return TYPEFUNC_RECORD;
834 
835  /*
836  * We treat VOID and CSTRING as legitimate scalar datatypes,
837  * mostly for the convenience of the JDBC driver (which wants to
838  * be able to do "SELECT * FROM foo()" for all legitimately
839  * user-callable functions).
840  */
841  if (typid == VOIDOID || typid == CSTRINGOID)
842  return TYPEFUNC_SCALAR;
843  return TYPEFUNC_OTHER;
844  }
845  /* shouldn't get here, probably */
846  return TYPEFUNC_OTHER;
847 }
848 
849 
850 /*
851  * get_func_arg_info
852  *
853  * Fetch info about the argument types, names, and IN/OUT modes from the
854  * pg_proc tuple. Return value is the total number of arguments.
855  * Other results are palloc'd. *p_argtypes is always filled in, but
856  * *p_argnames and *p_argmodes will be set NULL in the default cases
857  * (no names, and all IN arguments, respectively).
858  *
859  * Note that this function simply fetches what is in the pg_proc tuple;
860  * it doesn't do any interpretation of polymorphic types.
861  */
862 int
864  Oid **p_argtypes, char ***p_argnames, char **p_argmodes)
865 {
866  Form_pg_proc procStruct = (Form_pg_proc) GETSTRUCT(procTup);
867  Datum proallargtypes;
868  Datum proargmodes;
869  Datum proargnames;
870  bool isNull;
871  ArrayType *arr;
872  int numargs;
873  Datum *elems;
874  int nelems;
875  int i;
876 
877  /* First discover the total number of parameters and get their types */
878  proallargtypes = SysCacheGetAttr(PROCOID, procTup,
879  Anum_pg_proc_proallargtypes,
880  &isNull);
881  if (!isNull)
882  {
883  /*
884  * We expect the arrays to be 1-D arrays of the right types; verify
885  * that. For the OID and char arrays, we don't need to use
886  * deconstruct_array() since the array data is just going to look like
887  * a C array of values.
888  */
889  arr = DatumGetArrayTypeP(proallargtypes); /* ensure not toasted */
890  numargs = ARR_DIMS(arr)[0];
891  if (ARR_NDIM(arr) != 1 ||
892  numargs < 0 ||
893  ARR_HASNULL(arr) ||
894  ARR_ELEMTYPE(arr) != OIDOID)
895  elog(ERROR, "proallargtypes is not a 1-D Oid array");
896  Assert(numargs >= procStruct->pronargs);
897  *p_argtypes = (Oid *) palloc(numargs * sizeof(Oid));
898  memcpy(*p_argtypes, ARR_DATA_PTR(arr),
899  numargs * sizeof(Oid));
900  }
901  else
902  {
903  /* If no proallargtypes, use proargtypes */
904  numargs = procStruct->proargtypes.dim1;
905  Assert(numargs == procStruct->pronargs);
906  *p_argtypes = (Oid *) palloc(numargs * sizeof(Oid));
907  memcpy(*p_argtypes, procStruct->proargtypes.values,
908  numargs * sizeof(Oid));
909  }
910 
911  /* Get argument names, if available */
912  proargnames = SysCacheGetAttr(PROCOID, procTup,
913  Anum_pg_proc_proargnames,
914  &isNull);
915  if (isNull)
916  *p_argnames = NULL;
917  else
918  {
920  TEXTOID, -1, false, 'i',
921  &elems, NULL, &nelems);
922  if (nelems != numargs) /* should not happen */
923  elog(ERROR, "proargnames must have the same number of elements as the function has arguments");
924  *p_argnames = (char **) palloc(sizeof(char *) * numargs);
925  for (i = 0; i < numargs; i++)
926  (*p_argnames)[i] = TextDatumGetCString(elems[i]);
927  }
928 
929  /* Get argument modes, if available */
930  proargmodes = SysCacheGetAttr(PROCOID, procTup,
931  Anum_pg_proc_proargmodes,
932  &isNull);
933  if (isNull)
934  *p_argmodes = NULL;
935  else
936  {
937  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
938  if (ARR_NDIM(arr) != 1 ||
939  ARR_DIMS(arr)[0] != numargs ||
940  ARR_HASNULL(arr) ||
941  ARR_ELEMTYPE(arr) != CHAROID)
942  elog(ERROR, "proargmodes is not a 1-D char array");
943  *p_argmodes = (char *) palloc(numargs * sizeof(char));
944  memcpy(*p_argmodes, ARR_DATA_PTR(arr),
945  numargs * sizeof(char));
946  }
947 
948  return numargs;
949 }
950 
951 /*
952  * get_func_trftypes
953  *
954  * Returns the number of transformed types used by function.
955  */
956 int
958  Oid **p_trftypes)
959 {
960  Datum protrftypes;
961  ArrayType *arr;
962  int nelems;
963  bool isNull;
964 
965  protrftypes = SysCacheGetAttr(PROCOID, procTup,
966  Anum_pg_proc_protrftypes,
967  &isNull);
968  if (!isNull)
969  {
970  /*
971  * We expect the arrays to be 1-D arrays of the right types; verify
972  * that. For the OID and char arrays, we don't need to use
973  * deconstruct_array() since the array data is just going to look like
974  * a C array of values.
975  */
976  arr = DatumGetArrayTypeP(protrftypes); /* ensure not toasted */
977  nelems = ARR_DIMS(arr)[0];
978  if (ARR_NDIM(arr) != 1 ||
979  nelems < 0 ||
980  ARR_HASNULL(arr) ||
981  ARR_ELEMTYPE(arr) != OIDOID)
982  elog(ERROR, "protrftypes is not a 1-D Oid array");
983  Assert(nelems >= ((Form_pg_proc) GETSTRUCT(procTup))->pronargs);
984  *p_trftypes = (Oid *) palloc(nelems * sizeof(Oid));
985  memcpy(*p_trftypes, ARR_DATA_PTR(arr),
986  nelems * sizeof(Oid));
987 
988  return nelems;
989  }
990  else
991  return 0;
992 }
993 
994 /*
995  * get_func_input_arg_names
996  *
997  * Extract the names of input arguments only, given a function's
998  * proargnames and proargmodes entries in Datum form.
999  *
1000  * Returns the number of input arguments, which is the length of the
1001  * palloc'd array returned to *arg_names. Entries for unnamed args
1002  * are set to NULL. You don't get anything if proargnames is NULL.
1003  */
1004 int
1005 get_func_input_arg_names(Datum proargnames, Datum proargmodes,
1006  char ***arg_names)
1007 {
1008  ArrayType *arr;
1009  int numargs;
1010  Datum *argnames;
1011  char *argmodes;
1012  char **inargnames;
1013  int numinargs;
1014  int i;
1015 
1016  /* Do nothing if null proargnames */
1017  if (proargnames == PointerGetDatum(NULL))
1018  {
1019  *arg_names = NULL;
1020  return 0;
1021  }
1022 
1023  /*
1024  * We expect the arrays to be 1-D arrays of the right types; verify that.
1025  * For proargmodes, we don't need to use deconstruct_array() since the
1026  * array data is just going to look like a C array of values.
1027  */
1028  arr = DatumGetArrayTypeP(proargnames); /* ensure not toasted */
1029  if (ARR_NDIM(arr) != 1 ||
1030  ARR_HASNULL(arr) ||
1031  ARR_ELEMTYPE(arr) != TEXTOID)
1032  elog(ERROR, "proargnames is not a 1-D text array");
1033  deconstruct_array(arr, TEXTOID, -1, false, 'i',
1034  &argnames, NULL, &numargs);
1035  if (proargmodes != PointerGetDatum(NULL))
1036  {
1037  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
1038  if (ARR_NDIM(arr) != 1 ||
1039  ARR_DIMS(arr)[0] != numargs ||
1040  ARR_HASNULL(arr) ||
1041  ARR_ELEMTYPE(arr) != CHAROID)
1042  elog(ERROR, "proargmodes is not a 1-D char array");
1043  argmodes = (char *) ARR_DATA_PTR(arr);
1044  }
1045  else
1046  argmodes = NULL;
1047 
1048  /* zero elements probably shouldn't happen, but handle it gracefully */
1049  if (numargs <= 0)
1050  {
1051  *arg_names = NULL;
1052  return 0;
1053  }
1054 
1055  /* extract input-argument names */
1056  inargnames = (char **) palloc(numargs * sizeof(char *));
1057  numinargs = 0;
1058  for (i = 0; i < numargs; i++)
1059  {
1060  if (argmodes == NULL ||
1061  argmodes[i] == PROARGMODE_IN ||
1062  argmodes[i] == PROARGMODE_INOUT ||
1063  argmodes[i] == PROARGMODE_VARIADIC)
1064  {
1065  char *pname = TextDatumGetCString(argnames[i]);
1066 
1067  if (pname[0] != '\0')
1068  inargnames[numinargs] = pname;
1069  else
1070  inargnames[numinargs] = NULL;
1071  numinargs++;
1072  }
1073  }
1074 
1075  *arg_names = inargnames;
1076  return numinargs;
1077 }
1078 
1079 
1080 /*
1081  * get_func_result_name
1082  *
1083  * If the function has exactly one output parameter, and that parameter
1084  * is named, return the name (as a palloc'd string). Else return NULL.
1085  *
1086  * This is used to determine the default output column name for functions
1087  * returning scalar types.
1088  */
1089 char *
1091 {
1092  char *result;
1093  HeapTuple procTuple;
1094  Datum proargmodes;
1095  Datum proargnames;
1096  bool isnull;
1097  ArrayType *arr;
1098  int numargs;
1099  char *argmodes;
1100  Datum *argnames;
1101  int numoutargs;
1102  int nargnames;
1103  int i;
1104 
1105  /* First fetch the function's pg_proc row */
1106  procTuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(functionId));
1107  if (!HeapTupleIsValid(procTuple))
1108  elog(ERROR, "cache lookup failed for function %u", functionId);
1109 
1110  /* If there are no named OUT parameters, return NULL */
1111  if (heap_attisnull(procTuple, Anum_pg_proc_proargmodes, NULL) ||
1112  heap_attisnull(procTuple, Anum_pg_proc_proargnames, NULL))
1113  result = NULL;
1114  else
1115  {
1116  /* Get the data out of the tuple */
1117  proargmodes = SysCacheGetAttr(PROCOID, procTuple,
1118  Anum_pg_proc_proargmodes,
1119  &isnull);
1120  Assert(!isnull);
1121  proargnames = SysCacheGetAttr(PROCOID, procTuple,
1122  Anum_pg_proc_proargnames,
1123  &isnull);
1124  Assert(!isnull);
1125 
1126  /*
1127  * We expect the arrays to be 1-D arrays of the right types; verify
1128  * that. For the char array, we don't need to use deconstruct_array()
1129  * since the array data is just going to look like a C array of
1130  * values.
1131  */
1132  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
1133  numargs = ARR_DIMS(arr)[0];
1134  if (ARR_NDIM(arr) != 1 ||
1135  numargs < 0 ||
1136  ARR_HASNULL(arr) ||
1137  ARR_ELEMTYPE(arr) != CHAROID)
1138  elog(ERROR, "proargmodes is not a 1-D char array");
1139  argmodes = (char *) ARR_DATA_PTR(arr);
1140  arr = DatumGetArrayTypeP(proargnames); /* ensure not toasted */
1141  if (ARR_NDIM(arr) != 1 ||
1142  ARR_DIMS(arr)[0] != numargs ||
1143  ARR_HASNULL(arr) ||
1144  ARR_ELEMTYPE(arr) != TEXTOID)
1145  elog(ERROR, "proargnames is not a 1-D text array");
1146  deconstruct_array(arr, TEXTOID, -1, false, 'i',
1147  &argnames, NULL, &nargnames);
1148  Assert(nargnames == numargs);
1149 
1150  /* scan for output argument(s) */
1151  result = NULL;
1152  numoutargs = 0;
1153  for (i = 0; i < numargs; i++)
1154  {
1155  if (argmodes[i] == PROARGMODE_IN ||
1156  argmodes[i] == PROARGMODE_VARIADIC)
1157  continue;
1158  Assert(argmodes[i] == PROARGMODE_OUT ||
1159  argmodes[i] == PROARGMODE_INOUT ||
1160  argmodes[i] == PROARGMODE_TABLE);
1161  if (++numoutargs > 1)
1162  {
1163  /* multiple out args, so forget it */
1164  result = NULL;
1165  break;
1166  }
1167  result = TextDatumGetCString(argnames[i]);
1168  if (result == NULL || result[0] == '\0')
1169  {
1170  /* Parameter is not named, so forget it */
1171  result = NULL;
1172  break;
1173  }
1174  }
1175  }
1176 
1177  ReleaseSysCache(procTuple);
1178 
1179  return result;
1180 }
1181 
1182 
1183 /*
1184  * build_function_result_tupdesc_t
1185  *
1186  * Given a pg_proc row for a function, return a tuple descriptor for the
1187  * result rowtype, or NULL if the function does not have OUT parameters.
1188  *
1189  * Note that this does not handle resolution of polymorphic types;
1190  * that is deliberate.
1191  */
1192 TupleDesc
1194 {
1195  Form_pg_proc procform = (Form_pg_proc) GETSTRUCT(procTuple);
1196  Datum proallargtypes;
1197  Datum proargmodes;
1198  Datum proargnames;
1199  bool isnull;
1200 
1201  /* Return NULL if the function isn't declared to return RECORD */
1202  if (procform->prorettype != RECORDOID)
1203  return NULL;
1204 
1205  /* If there are no OUT parameters, return NULL */
1206  if (heap_attisnull(procTuple, Anum_pg_proc_proallargtypes, NULL) ||
1207  heap_attisnull(procTuple, Anum_pg_proc_proargmodes, NULL))
1208  return NULL;
1209 
1210  /* Get the data out of the tuple */
1211  proallargtypes = SysCacheGetAttr(PROCOID, procTuple,
1212  Anum_pg_proc_proallargtypes,
1213  &isnull);
1214  Assert(!isnull);
1215  proargmodes = SysCacheGetAttr(PROCOID, procTuple,
1216  Anum_pg_proc_proargmodes,
1217  &isnull);
1218  Assert(!isnull);
1219  proargnames = SysCacheGetAttr(PROCOID, procTuple,
1220  Anum_pg_proc_proargnames,
1221  &isnull);
1222  if (isnull)
1223  proargnames = PointerGetDatum(NULL); /* just to be sure */
1224 
1225  return build_function_result_tupdesc_d(procform->prokind,
1226  proallargtypes,
1227  proargmodes,
1228  proargnames);
1229 }
1230 
1231 /*
1232  * build_function_result_tupdesc_d
1233  *
1234  * Build a RECORD function's tupledesc from the pg_proc proallargtypes,
1235  * proargmodes, and proargnames arrays. This is split out for the
1236  * convenience of ProcedureCreate, which needs to be able to compute the
1237  * tupledesc before actually creating the function.
1238  *
1239  * For functions (but not for procedures), returns NULL if there are not at
1240  * least two OUT or INOUT arguments.
1241  */
1242 TupleDesc
1244  Datum proallargtypes,
1245  Datum proargmodes,
1246  Datum proargnames)
1247 {
1248  TupleDesc desc;
1249  ArrayType *arr;
1250  int numargs;
1251  Oid *argtypes;
1252  char *argmodes;
1253  Datum *argnames = NULL;
1254  Oid *outargtypes;
1255  char **outargnames;
1256  int numoutargs;
1257  int nargnames;
1258  int i;
1259 
1260  /* Can't have output args if columns are null */
1261  if (proallargtypes == PointerGetDatum(NULL) ||
1262  proargmodes == PointerGetDatum(NULL))
1263  return NULL;
1264 
1265  /*
1266  * We expect the arrays to be 1-D arrays of the right types; verify that.
1267  * For the OID and char arrays, we don't need to use deconstruct_array()
1268  * since the array data is just going to look like a C array of values.
1269  */
1270  arr = DatumGetArrayTypeP(proallargtypes); /* ensure not toasted */
1271  numargs = ARR_DIMS(arr)[0];
1272  if (ARR_NDIM(arr) != 1 ||
1273  numargs < 0 ||
1274  ARR_HASNULL(arr) ||
1275  ARR_ELEMTYPE(arr) != OIDOID)
1276  elog(ERROR, "proallargtypes is not a 1-D Oid array");
1277  argtypes = (Oid *) ARR_DATA_PTR(arr);
1278  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
1279  if (ARR_NDIM(arr) != 1 ||
1280  ARR_DIMS(arr)[0] != numargs ||
1281  ARR_HASNULL(arr) ||
1282  ARR_ELEMTYPE(arr) != CHAROID)
1283  elog(ERROR, "proargmodes is not a 1-D char array");
1284  argmodes = (char *) ARR_DATA_PTR(arr);
1285  if (proargnames != PointerGetDatum(NULL))
1286  {
1287  arr = DatumGetArrayTypeP(proargnames); /* ensure not toasted */
1288  if (ARR_NDIM(arr) != 1 ||
1289  ARR_DIMS(arr)[0] != numargs ||
1290  ARR_HASNULL(arr) ||
1291  ARR_ELEMTYPE(arr) != TEXTOID)
1292  elog(ERROR, "proargnames is not a 1-D text array");
1293  deconstruct_array(arr, TEXTOID, -1, false, 'i',
1294  &argnames, NULL, &nargnames);
1295  Assert(nargnames == numargs);
1296  }
1297 
1298  /* zero elements probably shouldn't happen, but handle it gracefully */
1299  if (numargs <= 0)
1300  return NULL;
1301 
1302  /* extract output-argument types and names */
1303  outargtypes = (Oid *) palloc(numargs * sizeof(Oid));
1304  outargnames = (char **) palloc(numargs * sizeof(char *));
1305  numoutargs = 0;
1306  for (i = 0; i < numargs; i++)
1307  {
1308  char *pname;
1309 
1310  if (argmodes[i] == PROARGMODE_IN ||
1311  argmodes[i] == PROARGMODE_VARIADIC)
1312  continue;
1313  Assert(argmodes[i] == PROARGMODE_OUT ||
1314  argmodes[i] == PROARGMODE_INOUT ||
1315  argmodes[i] == PROARGMODE_TABLE);
1316  outargtypes[numoutargs] = argtypes[i];
1317  if (argnames)
1318  pname = TextDatumGetCString(argnames[i]);
1319  else
1320  pname = NULL;
1321  if (pname == NULL || pname[0] == '\0')
1322  {
1323  /* Parameter is not named, so gin up a column name */
1324  pname = psprintf("column%d", numoutargs + 1);
1325  }
1326  outargnames[numoutargs] = pname;
1327  numoutargs++;
1328  }
1329 
1330  /*
1331  * If there is no output argument, or only one, the function does not
1332  * return tuples.
1333  */
1334  if (numoutargs < 2 && prokind != PROKIND_PROCEDURE)
1335  return NULL;
1336 
1337  desc = CreateTemplateTupleDesc(numoutargs);
1338  for (i = 0; i < numoutargs; i++)
1339  {
1340  TupleDescInitEntry(desc, i + 1,
1341  outargnames[i],
1342  outargtypes[i],
1343  -1,
1344  0);
1345  }
1346 
1347  return desc;
1348 }
1349 
1350 
1351 /*
1352  * RelationNameGetTupleDesc
1353  *
1354  * Given a (possibly qualified) relation name, build a TupleDesc.
1355  *
1356  * Note: while this works as advertised, it's seldom the best way to
1357  * build a tupdesc for a function's result type. It's kept around
1358  * only for backwards compatibility with existing user-written code.
1359  */
1360 TupleDesc
1362 {
1363  RangeVar *relvar;
1364  Relation rel;
1365  TupleDesc tupdesc;
1366  List *relname_list;
1367 
1368  /* Open relation and copy the tuple description */
1369  relname_list = stringToQualifiedNameList(relname);
1370  relvar = makeRangeVarFromNameList(relname_list);
1371  rel = relation_openrv(relvar, AccessShareLock);
1372  tupdesc = CreateTupleDescCopy(RelationGetDescr(rel));
1374 
1375  return tupdesc;
1376 }
1377 
1378 /*
1379  * TypeGetTupleDesc
1380  *
1381  * Given a type Oid, build a TupleDesc. (In most cases you should be
1382  * using get_call_result_type or one of its siblings instead of this
1383  * routine, so that you can handle OUT parameters, RECORD result type,
1384  * and polymorphic results.)
1385  *
1386  * If the type is composite, *and* a colaliases List is provided, *and*
1387  * the List is of natts length, use the aliases instead of the relation
1388  * attnames. (NB: this usage is deprecated since it may result in
1389  * creation of unnecessary transient record types.)
1390  *
1391  * If the type is a base type, a single item alias List is required.
1392  */
1393 TupleDesc
1394 TypeGetTupleDesc(Oid typeoid, List *colaliases)
1395 {
1396  Oid base_typeoid;
1397  TypeFuncClass functypclass = get_type_func_class(typeoid, &base_typeoid);
1398  TupleDesc tupdesc = NULL;
1399 
1400  /*
1401  * Build a suitable tupledesc representing the output rows. We
1402  * intentionally do not support TYPEFUNC_COMPOSITE_DOMAIN here, as it's
1403  * unlikely that legacy callers of this obsolete function would be
1404  * prepared to apply domain constraints.
1405  */
1406  if (functypclass == TYPEFUNC_COMPOSITE)
1407  {
1408  /* Composite data type, e.g. a table's row type */
1409  tupdesc = lookup_rowtype_tupdesc_copy(base_typeoid, -1);
1410 
1411  if (colaliases != NIL)
1412  {
1413  int natts = tupdesc->natts;
1414  int varattno;
1415 
1416  /* does the list length match the number of attributes? */
1417  if (list_length(colaliases) != natts)
1418  ereport(ERROR,
1419  (errcode(ERRCODE_DATATYPE_MISMATCH),
1420  errmsg("number of aliases does not match number of columns")));
1421 
1422  /* OK, use the aliases instead */
1423  for (varattno = 0; varattno < natts; varattno++)
1424  {
1425  char *label = strVal(list_nth(colaliases, varattno));
1426  Form_pg_attribute attr = TupleDescAttr(tupdesc, varattno);
1427 
1428  if (label != NULL)
1429  namestrcpy(&(attr->attname), label);
1430  }
1431 
1432  /* The tuple type is now an anonymous record type */
1433  tupdesc->tdtypeid = RECORDOID;
1434  tupdesc->tdtypmod = -1;
1435  }
1436  }
1437  else if (functypclass == TYPEFUNC_SCALAR)
1438  {
1439  /* Base data type, i.e. scalar */
1440  char *attname;
1441 
1442  /* the alias list is required for base types */
1443  if (colaliases == NIL)
1444  ereport(ERROR,
1445  (errcode(ERRCODE_DATATYPE_MISMATCH),
1446  errmsg("no column alias was provided")));
1447 
1448  /* the alias list length must be 1 */
1449  if (list_length(colaliases) != 1)
1450  ereport(ERROR,
1451  (errcode(ERRCODE_DATATYPE_MISMATCH),
1452  errmsg("number of aliases does not match number of columns")));
1453 
1454  /* OK, get the column alias */
1455  attname = strVal(linitial(colaliases));
1456 
1457  tupdesc = CreateTemplateTupleDesc(1);
1458  TupleDescInitEntry(tupdesc,
1459  (AttrNumber) 1,
1460  attname,
1461  typeoid,
1462  -1,
1463  0);
1464  }
1465  else if (functypclass == TYPEFUNC_RECORD)
1466  {
1467  /* XXX can't support this because typmod wasn't passed in ... */
1468  ereport(ERROR,
1469  (errcode(ERRCODE_DATATYPE_MISMATCH),
1470  errmsg("could not determine row description for function returning record")));
1471  }
1472  else
1473  {
1474  /* crummy error message, but parser should have caught this */
1475  elog(ERROR, "function in FROM has unsupported return type");
1476  }
1477 
1478  return tupdesc;
1479 }
1480 
1481 /*
1482  * extract_variadic_args
1483  *
1484  * Extract a set of argument values, types and NULL markers for a given
1485  * input function which makes use of a VARIADIC input whose argument list
1486  * depends on the caller context. When doing a VARIADIC call, the caller
1487  * has provided one argument made of an array of values, so deconstruct the
1488  * array data before using it for the next processing. If no VARIADIC call
1489  * is used, just fill in the status data based on all the arguments given
1490  * by the caller.
1491  *
1492  * This function returns the number of arguments generated, or -1 in the
1493  * case of "VARIADIC NULL".
1494  */
1495 int
1496 extract_variadic_args(FunctionCallInfo fcinfo, int variadic_start,
1497  bool convert_unknown, Datum **args, Oid **types,
1498  bool **nulls)
1499 {
1500  bool variadic = get_fn_expr_variadic(fcinfo->flinfo);
1501  Datum *args_res;
1502  bool *nulls_res;
1503  Oid *types_res;
1504  int nargs,
1505  i;
1506 
1507  *args = NULL;
1508  *types = NULL;
1509  *nulls = NULL;
1510 
1511  if (variadic)
1512  {
1514  Oid element_type;
1515  bool typbyval;
1516  char typalign;
1517  int16 typlen;
1518 
1519  Assert(PG_NARGS() == variadic_start + 1);
1520 
1521  if (PG_ARGISNULL(variadic_start))
1522  return -1;
1523 
1524  array_in = PG_GETARG_ARRAYTYPE_P(variadic_start);
1525  element_type = ARR_ELEMTYPE(array_in);
1526 
1527  get_typlenbyvalalign(element_type,
1528  &typlen, &typbyval, &typalign);
1529  deconstruct_array(array_in, element_type, typlen, typbyval,
1530  typalign, &args_res, &nulls_res,
1531  &nargs);
1532 
1533  /* All the elements of the array have the same type */
1534  types_res = (Oid *) palloc0(nargs * sizeof(Oid));
1535  for (i = 0; i < nargs; i++)
1536  types_res[i] = element_type;
1537  }
1538  else
1539  {
1540  nargs = PG_NARGS() - variadic_start;
1541  Assert(nargs > 0);
1542  nulls_res = (bool *) palloc0(nargs * sizeof(bool));
1543  args_res = (Datum *) palloc0(nargs * sizeof(Datum));
1544  types_res = (Oid *) palloc0(nargs * sizeof(Oid));
1545 
1546  for (i = 0; i < nargs; i++)
1547  {
1548  nulls_res[i] = PG_ARGISNULL(i + variadic_start);
1549  types_res[i] = get_fn_expr_argtype(fcinfo->flinfo,
1550  i + variadic_start);
1551 
1552  /*
1553  * Turn a constant (more or less literal) value that's of unknown
1554  * type into text if required. Unknowns come in as a cstring
1555  * pointer. Note: for functions declared as taking type "any", the
1556  * parser will not do any type conversion on unknown-type literals
1557  * (that is, undecorated strings or NULLs).
1558  */
1559  if (convert_unknown &&
1560  types_res[i] == UNKNOWNOID &&
1561  get_fn_expr_arg_stable(fcinfo->flinfo, i + variadic_start))
1562  {
1563  types_res[i] = TEXTOID;
1564 
1565  if (PG_ARGISNULL(i + variadic_start))
1566  args_res[i] = (Datum) 0;
1567  else
1568  args_res[i] =
1569  CStringGetTextDatum(PG_GETARG_POINTER(i + variadic_start));
1570  }
1571  else
1572  {
1573  /* no conversion needed, just take the datum as given */
1574  args_res[i] = PG_GETARG_DATUM(i + variadic_start);
1575  }
1576 
1577  if (!OidIsValid(types_res[i]) ||
1578  (convert_unknown && types_res[i] == UNKNOWNOID))
1579  ereport(ERROR,
1580  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1581  errmsg("could not determine data type for argument %d",
1582  i + 1)));
1583  }
1584  }
1585 
1586  /* Fill in results */
1587  *args = args_res;
1588  *nulls = nulls_res;
1589  *types = types_res;
1590 
1591  return nargs;
1592 }
bool resolve_polymorphic_argtypes(int numargs, Oid *argtypes, char *argmodes, Node *call_expr)
Definition: funcapi.c:661
uint64 call_cntr
Definition: funcapi.h:66
signed short int16
Definition: c.h:346
#define NIL
Definition: pg_list.h:65
Definition: c.h:589
Definition: fmgr.h:56
List * args
Definition: primnodes.h:1008
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:110
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
#define AllocSetContextCreate
Definition: memutils.h:170
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:196
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:419
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
int extract_variadic_args(FunctionCallInfo fcinfo, int variadic_start, bool convert_unknown, Datum **args, Oid **types, bool **nulls)
Definition: funcapi.c:1496
Oid resolve_generic_type(Oid declared_type, Oid context_actual_type, Oid context_declared_type)
bool get_fn_expr_arg_stable(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1882
struct typedefs * types
Definition: ecpg.c:29
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:44
#define RelationGetDescr(relation)
Definition: rel.h:449
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:275
void UnregisterExprContextCallback(ExprContext *econtext, ExprContextCallbackFunction function, Datum arg)
Definition: execUtils.c:889
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2049
#define PointerGetDatum(X)
Definition: postgres.h:556
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:263
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:202
TupleDesc build_function_result_tupdesc_d(char prokind, Datum proallargtypes, Datum proargmodes, Datum proargnames)
Definition: funcapi.c:1243
int get_func_arg_info(HeapTuple procTup, Oid **p_argtypes, char ***p_argnames, char **p_argmodes)
Definition: funcapi.c:863
#define AccessShareLock
Definition: lockdefs.h:36
Definition: nodes.h:525
#define strVal(v)
Definition: value.h:54
bool get_fn_expr_variadic(FmgrInfo *flinfo)
Definition: fmgr.c:1951
int errcode(int sqlerrcode)
Definition: elog.c:608
char get_typtype(Oid typid)
Definition: lsyscache.c:2407
Oid get_call_expr_argtype(Node *expr, int argnum)
Definition: fmgr.c:1836
char * format_type_be(Oid type_oid)
Definition: format_type.c:326
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
Definition: heaptuple.c:359
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:3054
int16 pronargs
Definition: pg_proc.h:82
NameData relname
Definition: pg_class.h:35
unsigned int Oid
Definition: postgres_ext.h:31
int namestrcpy(Name name, const char *str)
Definition: name.c:250
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:212
#define OidIsValid(objectId)
Definition: c.h:639
static void shutdown_MultiFuncCall(Datum arg)
Definition: funcapi.c:158
TupleDesc tuple_desc
Definition: funcapi.h:113
void assign_record_type_typmod(TupleDesc tupDesc)
Definition: typcache.c:1772
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
TupleDesc expectedDesc
Definition: execnodes.h:302
TupleDesc build_function_result_tupdesc_t(HeapTuple procTuple)
Definition: funcapi.c:1193
char typalign
Definition: pg_type.h:170
#define linitial(l)
Definition: pg_list.h:195
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
NameData attname
Definition: pg_attribute.h:40
List * colnames
Definition: primnodes.h:1024
TupleDesc get_expr_result_tupdesc(Node *expr, bool noError)
Definition: funcapi.c:429
fmNodePtr resultinfo
Definition: fmgr.h:89
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1817
int32 tdtypmod
Definition: tupdesc.h:83
#define ARR_DIMS(a)
Definition: array.h:282
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition: tupdesc.c:769
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2052
#define ARR_DATA_PTR(a)
Definition: array.h:310
Oid values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:597
int dim1
Definition: c.h:595
AttInMetadata * attinmeta
Definition: funcapi.h:92
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
static TypeFuncClass get_type_func_class(Oid typid, Oid *base_typeid)
Definition: funcapi.c:813
#define ARR_HASNULL(a)
Definition: array.h:279
TypeFuncClass
Definition: funcapi.h:147
int get_func_trftypes(HeapTuple procTup, Oid **p_trftypes)
Definition: funcapi.c:957
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:603
Oid atttypid
Definition: pg_attribute.h:49
#define ereport(elevel, rest)
Definition: elog.h:141
TypeFuncClass get_func_result_type(Oid functionId, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:289
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
int get_func_input_arg_names(Datum proargnames, Datum proargmodes, char ***arg_names)
Definition: funcapi.c:1005
#define TextDatumGetCString(d)
Definition: builtins.h:84
void * palloc0(Size size)
Definition: mcxt.c:980
uintptr_t Datum
Definition: postgres.h:367
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
FmgrInfo * flinfo
Definition: fmgr.h:87
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1377
static char * label
Definition: pg_basebackup.c:86
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: relation.c:138
FuncCallContext * per_MultiFuncCall(PG_FUNCTION_ARGS)
Definition: funcapi.c:128
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:134
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:839
void end_MultiFuncCall(PG_FUNCTION_ARGS, FuncCallContext *funcctx)
Definition: funcapi.c:140
#define InvalidOid
Definition: postgres_ext.h:36
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1092
Oid fn_oid
Definition: fmgr.h:59
Oid get_typcollation(Oid typid)
Definition: lsyscache.c:2823
#define PG_ARGISNULL(n)
Definition: fmgr.h:204
Oid exprInputCollation(const Node *expr)
Definition: nodeFuncs.c:924
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
#define Assert(condition)
Definition: c.h:733
#define lfirst(lc)
Definition: pg_list.h:190
void RegisterExprContextCallback(ExprContext *econtext, ExprContextCallbackFunction function, Datum arg)
Definition: execUtils.c:863
fmNodePtr fn_expr
Definition: fmgr.h:66
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:102
bool type_is_enum(Oid typid)
Definition: lsyscache.c:2456
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
Oid row_typeid
Definition: primnodes.h:1009
static int list_length(const List *l)
Definition: pg_list.h:169
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:719
#define type_is_array(typid)
Definition: lsyscache.h:184
#define PG_NARGS()
Definition: fmgr.h:198
void * fn_extra
Definition: fmgr.h:64
#define ARR_NDIM(a)
Definition: array.h:278
#define DatumGetPointer(X)
Definition: postgres.h:549
TupleDesc TypeGetTupleDesc(Oid typeoid, List *colaliases)
Definition: funcapi.c:1394
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3461
ExprContext * econtext
Definition: execnodes.h:301
Oid tdtypeid
Definition: tupdesc.h:82
List * stringToQualifiedNameList(const char *string)
Definition: regproc.c:1687
void * user_fctx
Definition: funcapi.h:83
FuncCallContext * init_MultiFuncCall(PG_FUNCTION_ARGS)
Definition: funcapi.c:53
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228
int i
#define NameStr(name)
Definition: c.h:610
#define CStringGetTextDatum(s)
Definition: builtins.h:83
void * arg
TupleDesc RelationNameGetTupleDesc(const char *relname)
Definition: funcapi.c:1361
#define PG_FUNCTION_ARGS
Definition: fmgr.h:188
static bool resolve_polymorphic_tupdesc(TupleDesc tupdesc, oidvector *declared_args, Node *call_expr)
Definition: funcapi.c:465
static TypeFuncClass internal_get_result_type(Oid funcid, Node *call_expr, ReturnSetInfo *rsinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:309
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2299
Definition: pg_list.h:50
#define ARR_ELEMTYPE(a)
Definition: array.h:280
int16 AttrNumber
Definition: attnum.h:21
Datum array_in(PG_FUNCTION_ARGS)
Definition: arrayfuncs.c:172
uint64 max_calls
Definition: funcapi.h:75
char * get_func_result_name(Oid functionId)
Definition: funcapi.c:1090
TupleDesc lookup_rowtype_tupdesc_copy(Oid type_id, int32 typmod)
Definition: typcache.c:1686
#define DatumGetArrayTypeP(X)
Definition: array.h:249