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