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