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