1 /*-------------------------------------------------------------------------
4 * Support routines for scanning RangeFunctions (functions in rangetable).
6 * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/executor/nodeFunctionscan.c
13 *-------------------------------------------------------------------------
17 * ExecFunctionScan scans a function.
18 * ExecFunctionNext retrieve next tuple in sequential order.
19 * ExecInitFunctionScan creates and initializes a functionscan node.
20 * ExecEndFunctionScan releases any storage allocated.
21 * ExecReScanFunctionScan rescans the function
25 #include "catalog/pg_type.h"
26 #include "executor/nodeFunctionscan.h"
28 #include "nodes/nodeFuncs.h"
29 #include "utils/builtins.h"
30 #include "utils/memutils.h"
34 * Runtime data for each function being scanned.
36 typedef struct FunctionScanPerFuncState
38 ExprState *funcexpr; /* state of the expression being evaluated */
39 TupleDesc tupdesc; /* desc of the function result type */
40 int colcount; /* expected number of result columns */
41 Tuplestorestate *tstore; /* holds the function result set */
42 int64 rowcount; /* # of rows in result set, -1 if not known */
43 TupleTableSlot *func_slot; /* function result slot (or NULL) */
44 } FunctionScanPerFuncState;
46 static TupleTableSlot *FunctionNext(FunctionScanState *node);
49 /* ----------------------------------------------------------------
51 * ----------------------------------------------------------------
53 /* ----------------------------------------------------------------
56 * This is a workhorse for ExecFunctionScan
57 * ----------------------------------------------------------------
59 static TupleTableSlot *
60 FunctionNext(FunctionScanState *node)
63 ScanDirection direction;
64 TupleTableSlot *scanslot;
71 * get information from the estate and scan state
73 estate = node->ss.ps.state;
74 direction = estate->es_direction;
75 scanslot = node->ss.ss_ScanTupleSlot;
80 * Fast path for the trivial case: the function return type and scan
81 * result type are the same, so we fetch the function result straight
82 * into the scan result slot. No need to update ordinality or
85 Tuplestorestate *tstore = node->funcstates[0].tstore;
88 * If first time through, read all tuples from function and put them
89 * in a tuplestore. Subsequent calls just fetch tuples from
94 node->funcstates[0].tstore = tstore =
95 ExecMakeTableFunctionResult(node->funcstates[0].funcexpr,
96 node->ss.ps.ps_ExprContext,
98 node->funcstates[0].tupdesc,
99 node->eflags & EXEC_FLAG_BACKWARD);
102 * paranoia - cope if the function, which may have constructed the
103 * tuplestore itself, didn't leave it pointing at the start. This
104 * call is fast, so the overhead shouldn't be an issue.
106 tuplestore_rescan(tstore);
110 * Get the next tuple from tuplestore.
112 (void) tuplestore_gettupleslot(tstore,
113 ScanDirectionIsForward(direction),
120 * Increment or decrement ordinal counter before checking for end-of-data,
121 * so that we can move off either end of the result by 1 (and no more than
122 * 1) without losing correct count. See PortalRunSelect for why we can
123 * assume that we won't be called repeatedly in the end-of-data state.
125 oldpos = node->ordinal;
126 if (ScanDirectionIsForward(direction))
132 * Main loop over functions.
134 * We fetch the function results into func_slots (which match the function
135 * return types), and then copy the values to scanslot (which matches the
136 * scan result type), setting the ordinal column (if any) as well.
138 ExecClearTuple(scanslot);
141 for (funcno = 0; funcno < node->nfuncs; funcno++)
143 FunctionScanPerFuncState *fs = &node->funcstates[funcno];
147 * If first time through, read all tuples from function and put them
148 * in a tuplestore. Subsequent calls just fetch tuples from
151 if (fs->tstore == NULL)
154 ExecMakeTableFunctionResult(fs->funcexpr,
155 node->ss.ps.ps_ExprContext,
158 node->eflags & EXEC_FLAG_BACKWARD);
161 * paranoia - cope if the function, which may have constructed the
162 * tuplestore itself, didn't leave it pointing at the start. This
163 * call is fast, so the overhead shouldn't be an issue.
165 tuplestore_rescan(fs->tstore);
169 * Get the next tuple from tuplestore.
171 * If we have a rowcount for the function, and we know the previous
172 * read position was out of bounds, don't try the read. This allows
173 * backward scan to work when there are mixed row counts present.
175 if (fs->rowcount != -1 && fs->rowcount < oldpos)
176 ExecClearTuple(fs->func_slot);
178 (void) tuplestore_gettupleslot(fs->tstore,
179 ScanDirectionIsForward(direction),
183 if (TupIsNull(fs->func_slot))
186 * If we ran out of data for this function in the forward
187 * direction then we now know how many rows it returned. We need
188 * to know this in order to handle backwards scans. The row count
189 * we store is actually 1+ the actual number, because we have to
190 * position the tuplestore 1 off its end sometimes.
192 if (ScanDirectionIsForward(direction) && fs->rowcount == -1)
193 fs->rowcount = node->ordinal;
196 * populate the result cols with nulls
198 for (i = 0; i < fs->colcount; i++)
200 scanslot->tts_values[att] = (Datum) 0;
201 scanslot->tts_isnull[att] = true;
208 * we have a result, so just copy it to the result cols.
210 slot_getallattrs(fs->func_slot);
212 for (i = 0; i < fs->colcount; i++)
214 scanslot->tts_values[att] = fs->func_slot->tts_values[i];
215 scanslot->tts_isnull[att] = fs->func_slot->tts_isnull[i];
220 * We're not done until every function result is exhausted; we pad
221 * the shorter results with nulls until then.
228 * ordinal col is always last, per spec.
230 if (node->ordinality)
232 scanslot->tts_values[att] = Int64GetDatumFast(node->ordinal);
233 scanslot->tts_isnull[att] = false;
237 * If alldone, we just return the previously-cleared scanslot. Otherwise,
238 * finish creating the virtual tuple.
241 ExecStoreVirtualTuple(scanslot);
247 * FunctionRecheck -- access method routine to recheck a tuple in EvalPlanQual
250 FunctionRecheck(FunctionScanState *node, TupleTableSlot *slot)
252 /* nothing to check */
256 /* ----------------------------------------------------------------
257 * ExecFunctionScan(node)
259 * Scans the function sequentially and returns the next qualifying
261 * We call the ExecScan() routine and pass it the appropriate
262 * access method functions.
263 * ----------------------------------------------------------------
266 ExecFunctionScan(FunctionScanState *node)
268 return ExecScan(&node->ss,
269 (ExecScanAccessMtd) FunctionNext,
270 (ExecScanRecheckMtd) FunctionRecheck);
273 /* ----------------------------------------------------------------
274 * ExecInitFunctionScan
275 * ----------------------------------------------------------------
278 ExecInitFunctionScan(FunctionScan *node, EState *estate, int eflags)
280 FunctionScanState *scanstate;
281 int nfuncs = list_length(node->functions);
282 TupleDesc scan_tupdesc;
287 /* check for unsupported flags */
288 Assert(!(eflags & EXEC_FLAG_MARK));
291 * FunctionScan should not have any children.
293 Assert(outerPlan(node) == NULL);
294 Assert(innerPlan(node) == NULL);
297 * create new ScanState for node
299 scanstate = makeNode(FunctionScanState);
300 scanstate->ss.ps.plan = (Plan *) node;
301 scanstate->ss.ps.state = estate;
302 scanstate->eflags = eflags;
305 * are we adding an ordinality column?
307 scanstate->ordinality = node->funcordinality;
309 scanstate->nfuncs = nfuncs;
310 if (nfuncs == 1 && !node->funcordinality)
311 scanstate->simple = true;
313 scanstate->simple = false;
316 * Ordinal 0 represents the "before the first row" position.
318 * We need to track ordinal position even when not adding an ordinality
319 * column to the result, in order to handle backwards scanning properly
320 * with multiple functions with different result sizes. (We can't position
321 * any individual function's tuplestore any more than 1 place beyond its
322 * end, so when scanning backwards, we need to know when to start
323 * including the function in the scan again.)
325 scanstate->ordinal = 0;
328 * Miscellaneous initialization
330 * create expression context for node
332 ExecAssignExprContext(estate, &scanstate->ss.ps);
335 * tuple table initialization
337 ExecInitResultTupleSlot(estate, &scanstate->ss.ps);
338 ExecInitScanTupleSlot(estate, &scanstate->ss);
341 * initialize child expressions
343 scanstate->ss.ps.targetlist = (List *)
344 ExecInitExpr((Expr *) node->scan.plan.targetlist,
345 (PlanState *) scanstate);
346 scanstate->ss.ps.qual = (List *)
347 ExecInitExpr((Expr *) node->scan.plan.qual,
348 (PlanState *) scanstate);
350 scanstate->funcstates = palloc(nfuncs * sizeof(FunctionScanPerFuncState));
354 foreach(lc, node->functions)
356 RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
357 Node *funcexpr = rtfunc->funcexpr;
358 int colcount = rtfunc->funccolcount;
359 FunctionScanPerFuncState *fs = &scanstate->funcstates[i];
360 TypeFuncClass functypclass;
364 fs->funcexpr = ExecInitExpr((Expr *) funcexpr, (PlanState *) scanstate);
367 * Don't allocate the tuplestores; the actual calls to the functions
368 * do that. NULL means that we have not called the function yet (or
369 * need to call it again after a rescan).
375 * Now determine if the function returns a simple or composite type,
376 * and build an appropriate tupdesc. Note that in the composite case,
377 * the function may now return more columns than it did when the plan
378 * was made; we have to ignore any columns beyond "colcount".
380 functypclass = get_expr_result_type(funcexpr,
384 if (functypclass == TYPEFUNC_COMPOSITE)
386 /* Composite data type, e.g. a table's row type */
388 Assert(tupdesc->natts >= colcount);
389 /* Must copy it out of typcache for safety */
390 tupdesc = CreateTupleDescCopy(tupdesc);
392 else if (functypclass == TYPEFUNC_SCALAR)
394 /* Base data type, i.e. scalar */
395 tupdesc = CreateTemplateTupleDesc(1, false);
396 TupleDescInitEntry(tupdesc,
398 NULL, /* don't care about the name here */
402 TupleDescInitEntryCollation(tupdesc,
404 exprCollation(funcexpr));
406 else if (functypclass == TYPEFUNC_RECORD)
408 tupdesc = BuildDescFromLists(rtfunc->funccolnames,
409 rtfunc->funccoltypes,
410 rtfunc->funccoltypmods,
411 rtfunc->funccolcollations);
414 * For RECORD results, make sure a typmod has been assigned. (The
415 * function should do this for itself, but let's cover things in
418 BlessTupleDesc(tupdesc);
422 /* crummy error message, but parser should have caught this */
423 elog(ERROR, "function in FROM has unsupported return type");
426 fs->tupdesc = tupdesc;
427 fs->colcount = colcount;
430 * We only need separate slots for the function results if we are
431 * doing ordinality or multiple functions; otherwise, we'll fetch
432 * function results directly into the scan slot.
434 if (!scanstate->simple)
436 fs->func_slot = ExecInitExtraTupleSlot(estate);
437 ExecSetSlotDescriptor(fs->func_slot, fs->tupdesc);
440 fs->func_slot = NULL;
447 * Create the combined TupleDesc
449 * If there is just one function without ordinality, the scan result
450 * tupdesc is the same as the function result tupdesc --- except that we
451 * may stuff new names into it below, so drop any rowtype label.
453 if (scanstate->simple)
455 scan_tupdesc = CreateTupleDescCopy(scanstate->funcstates[0].tupdesc);
456 scan_tupdesc->tdtypeid = RECORDOID;
457 scan_tupdesc->tdtypmod = -1;
461 AttrNumber attno = 0;
463 if (node->funcordinality)
466 scan_tupdesc = CreateTemplateTupleDesc(natts, false);
468 for (i = 0; i < nfuncs; i++)
470 TupleDesc tupdesc = scanstate->funcstates[i].tupdesc;
471 int colcount = scanstate->funcstates[i].colcount;
474 for (j = 1; j <= colcount; j++)
475 TupleDescCopyEntry(scan_tupdesc, ++attno, tupdesc, j);
478 /* If doing ordinality, add a column of type "bigint" at the end */
479 if (node->funcordinality)
481 TupleDescInitEntry(scan_tupdesc,
483 NULL, /* don't care about the name here */
489 Assert(attno == natts);
492 ExecAssignScanType(&scanstate->ss, scan_tupdesc);
495 * Initialize result tuple type and projection info.
497 ExecAssignResultTypeFromTL(&scanstate->ss.ps);
498 ExecAssignScanProjectionInfo(&scanstate->ss);
501 * Create a memory context that ExecMakeTableFunctionResult can use to
502 * evaluate function arguments in. We can't use the per-tuple context for
503 * this because it gets reset too often; but we don't want to leak
504 * evaluation results into the query-lifespan context either. We just
505 * need one context, because we evaluate each function separately.
507 scanstate->argcontext = AllocSetContextCreate(CurrentMemoryContext,
508 "Table function arguments",
509 ALLOCSET_DEFAULT_SIZES);
514 /* ----------------------------------------------------------------
515 * ExecEndFunctionScan
517 * frees any storage allocated through C routines.
518 * ----------------------------------------------------------------
521 ExecEndFunctionScan(FunctionScanState *node)
526 * Free the exprcontext
528 ExecFreeExprContext(&node->ss.ps);
531 * clean out the tuple table
533 ExecClearTuple(node->ss.ps.ps_ResultTupleSlot);
534 ExecClearTuple(node->ss.ss_ScanTupleSlot);
537 * Release slots and tuplestore resources
539 for (i = 0; i < node->nfuncs; i++)
541 FunctionScanPerFuncState *fs = &node->funcstates[i];
544 ExecClearTuple(fs->func_slot);
546 if (fs->tstore != NULL)
548 tuplestore_end(node->funcstates[i].tstore);
554 /* ----------------------------------------------------------------
555 * ExecReScanFunctionScan
557 * Rescans the relation.
558 * ----------------------------------------------------------------
561 ExecReScanFunctionScan(FunctionScanState *node)
563 FunctionScan *scan = (FunctionScan *) node->ss.ps.plan;
565 Bitmapset *chgparam = node->ss.ps.chgParam;
567 ExecClearTuple(node->ss.ps.ps_ResultTupleSlot);
568 for (i = 0; i < node->nfuncs; i++)
570 FunctionScanPerFuncState *fs = &node->funcstates[i];
573 ExecClearTuple(fs->func_slot);
576 ExecScanReScan(&node->ss);
579 * Here we have a choice whether to drop the tuplestores (and recompute
580 * the function outputs) or just rescan them. We must recompute if an
581 * expression contains changed parameters, else we rescan.
583 * XXX maybe we should recompute if the function is volatile? But in
584 * general the executor doesn't conditionalize its actions on that.
591 foreach(lc, scan->functions)
593 RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
595 if (bms_overlap(chgparam, rtfunc->funcparams))
597 if (node->funcstates[i].tstore != NULL)
599 tuplestore_end(node->funcstates[i].tstore);
600 node->funcstates[i].tstore = NULL;
602 node->funcstates[i].rowcount = -1;
608 /* Reset ordinality counter */
611 /* Make sure we rewind any remaining tuplestores */
612 for (i = 0; i < node->nfuncs; i++)
614 if (node->funcstates[i].tstore != NULL)
615 tuplestore_rescan(node->funcstates[i].tstore);