PostgreSQL Source Code  git master
nodeFunctionscan.h File Reference
#include "nodes/execnodes.h"
Include dependency graph for nodeFunctionscan.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

FunctionScanStateExecInitFunctionScan (FunctionScan *node, EState *estate, int eflags)
 
void ExecEndFunctionScan (FunctionScanState *node)
 
void ExecReScanFunctionScan (FunctionScanState *node)
 

Function Documentation

◆ ExecEndFunctionScan()

void ExecEndFunctionScan ( FunctionScanState node)

Definition at line 522 of file nodeFunctionscan.c.

References ExecClearTuple(), ExecFreeExprContext(), FunctionScanPerFuncState::func_slot, FunctionScanState::funcstates, i, FunctionScanState::nfuncs, ScanState::ps, PlanState::ps_ResultTupleSlot, FunctionScanState::ss, ScanState::ss_ScanTupleSlot, FunctionScanPerFuncState::tstore, and tuplestore_end().

Referenced by ExecEndNode().

523 {
524  int i;
525 
526  /*
527  * Free the exprcontext
528  */
529  ExecFreeExprContext(&node->ss.ps);
530 
531  /*
532  * clean out the tuple table
533  */
534  if (node->ss.ps.ps_ResultTupleSlot)
537 
538  /*
539  * Release slots and tuplestore resources
540  */
541  for (i = 0; i < node->nfuncs; i++)
542  {
543  FunctionScanPerFuncState *fs = &node->funcstates[i];
544 
545  if (fs->func_slot)
547 
548  if (fs->tstore != NULL)
549  {
550  tuplestore_end(node->funcstates[i].tstore);
551  fs->tstore = NULL;
552  }
553  }
554 }
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:426
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1334
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:614
PlanState ps
Definition: execnodes.h:1331
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:978
Tuplestorestate * tstore
struct FunctionScanPerFuncState * funcstates
Definition: execnodes.h:1667
TupleTableSlot * func_slot
void tuplestore_end(Tuplestorestate *state)
Definition: tuplestore.c:453
int i

◆ ExecInitFunctionScan()

FunctionScanState* ExecInitFunctionScan ( FunctionScan node,
EState estate,
int  eflags 
)

Definition at line 280 of file nodeFunctionscan.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert, BlessTupleDesc(), BuildDescFromLists(), FunctionScanPerFuncState::colcount, CreateTemplateTupleDesc(), CreateTupleDescCopy(), CurrentMemoryContext, FunctionScanState::eflags, elog, ERROR, EXEC_FLAG_MARK, ExecAssignExprContext(), ExecAssignScanProjectionInfo(), ExecFunctionScan(), ExecInitExtraTupleSlot(), ExecInitQual(), ExecInitResultTypeTL(), ExecInitScanTupleSlot(), ExecInitTableFunctionResult(), PlanState::ExecProcNode, exprCollation(), FunctionScanPerFuncState::func_slot, RangeTblFunction::funccolcollations, RangeTblFunction::funccolcount, RangeTblFunction::funccolnames, RangeTblFunction::funccoltypes, RangeTblFunction::funccoltypmods, RangeTblFunction::funcexpr, FunctionScan::funcordinality, FunctionScanState::funcstates, FunctionScan::functions, get_expr_result_type(), i, innerPlan, lfirst, list_length(), makeNode, TupleDescData::natts, FunctionScanState::nfuncs, FunctionScanState::ordinal, FunctionScanState::ordinality, outerPlan, palloc(), Scan::plan, PlanState::plan, ScanState::ps, PlanState::ps_ExprContext, Plan::qual, PlanState::qual, FunctionScanPerFuncState::rowcount, FunctionScan::scan, FunctionScanPerFuncState::setexpr, FunctionScanState::simple, FunctionScanState::ss, PlanState::state, TupleDescData::tdtypeid, TupleDescData::tdtypmod, FunctionScanPerFuncState::tstore, TTSOpsMinimalTuple, FunctionScanPerFuncState::tupdesc, TupleDescCopyEntry(), TupleDescInitEntry(), TupleDescInitEntryCollation(), TYPEFUNC_COMPOSITE, TYPEFUNC_COMPOSITE_DOMAIN, TYPEFUNC_RECORD, and TYPEFUNC_SCALAR.

Referenced by ExecInitNode().

281 {
282  FunctionScanState *scanstate;
283  int nfuncs = list_length(node->functions);
284  TupleDesc scan_tupdesc;
285  int i,
286  natts;
287  ListCell *lc;
288 
289  /* check for unsupported flags */
290  Assert(!(eflags & EXEC_FLAG_MARK));
291 
292  /*
293  * FunctionScan should not have any children.
294  */
295  Assert(outerPlan(node) == NULL);
296  Assert(innerPlan(node) == NULL);
297 
298  /*
299  * create new ScanState for node
300  */
301  scanstate = makeNode(FunctionScanState);
302  scanstate->ss.ps.plan = (Plan *) node;
303  scanstate->ss.ps.state = estate;
304  scanstate->ss.ps.ExecProcNode = ExecFunctionScan;
305  scanstate->eflags = eflags;
306 
307  /*
308  * are we adding an ordinality column?
309  */
310  scanstate->ordinality = node->funcordinality;
311 
312  scanstate->nfuncs = nfuncs;
313  if (nfuncs == 1 && !node->funcordinality)
314  scanstate->simple = true;
315  else
316  scanstate->simple = false;
317 
318  /*
319  * Ordinal 0 represents the "before the first row" position.
320  *
321  * We need to track ordinal position even when not adding an ordinality
322  * column to the result, in order to handle backwards scanning properly
323  * with multiple functions with different result sizes. (We can't position
324  * any individual function's tuplestore any more than 1 place beyond its
325  * end, so when scanning backwards, we need to know when to start
326  * including the function in the scan again.)
327  */
328  scanstate->ordinal = 0;
329 
330  /*
331  * Miscellaneous initialization
332  *
333  * create expression context for node
334  */
335  ExecAssignExprContext(estate, &scanstate->ss.ps);
336 
337  scanstate->funcstates = palloc(nfuncs * sizeof(FunctionScanPerFuncState));
338 
339  natts = 0;
340  i = 0;
341  foreach(lc, node->functions)
342  {
343  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
344  Node *funcexpr = rtfunc->funcexpr;
345  int colcount = rtfunc->funccolcount;
346  FunctionScanPerFuncState *fs = &scanstate->funcstates[i];
347  TypeFuncClass functypclass;
348  Oid funcrettype;
349  TupleDesc tupdesc;
350 
351  fs->setexpr =
352  ExecInitTableFunctionResult((Expr *) funcexpr,
353  scanstate->ss.ps.ps_ExprContext,
354  &scanstate->ss.ps);
355 
356  /*
357  * Don't allocate the tuplestores; the actual calls to the functions
358  * do that. NULL means that we have not called the function yet (or
359  * need to call it again after a rescan).
360  */
361  fs->tstore = NULL;
362  fs->rowcount = -1;
363 
364  /*
365  * Now determine if the function returns a simple or composite type,
366  * and build an appropriate tupdesc. Note that in the composite case,
367  * the function may now return more columns than it did when the plan
368  * was made; we have to ignore any columns beyond "colcount".
369  */
370  functypclass = get_expr_result_type(funcexpr,
371  &funcrettype,
372  &tupdesc);
373 
374  if (functypclass == TYPEFUNC_COMPOSITE ||
375  functypclass == TYPEFUNC_COMPOSITE_DOMAIN)
376  {
377  /* Composite data type, e.g. a table's row type */
378  Assert(tupdesc);
379  Assert(tupdesc->natts >= colcount);
380  /* Must copy it out of typcache for safety */
381  tupdesc = CreateTupleDescCopy(tupdesc);
382  }
383  else if (functypclass == TYPEFUNC_SCALAR)
384  {
385  /* Base data type, i.e. scalar */
386  tupdesc = CreateTemplateTupleDesc(1);
387  TupleDescInitEntry(tupdesc,
388  (AttrNumber) 1,
389  NULL, /* don't care about the name here */
390  funcrettype,
391  -1,
392  0);
394  (AttrNumber) 1,
395  exprCollation(funcexpr));
396  }
397  else if (functypclass == TYPEFUNC_RECORD)
398  {
399  tupdesc = BuildDescFromLists(rtfunc->funccolnames,
400  rtfunc->funccoltypes,
401  rtfunc->funccoltypmods,
402  rtfunc->funccolcollations);
403 
404  /*
405  * For RECORD results, make sure a typmod has been assigned. (The
406  * function should do this for itself, but let's cover things in
407  * case it doesn't.)
408  */
409  BlessTupleDesc(tupdesc);
410  }
411  else
412  {
413  /* crummy error message, but parser should have caught this */
414  elog(ERROR, "function in FROM has unsupported return type");
415  }
416 
417  fs->tupdesc = tupdesc;
418  fs->colcount = colcount;
419 
420  /*
421  * We only need separate slots for the function results if we are
422  * doing ordinality or multiple functions; otherwise, we'll fetch
423  * function results directly into the scan slot.
424  */
425  if (!scanstate->simple)
426  {
427  fs->func_slot = ExecInitExtraTupleSlot(estate, fs->tupdesc,
429  }
430  else
431  fs->func_slot = NULL;
432 
433  natts += colcount;
434  i++;
435  }
436 
437  /*
438  * Create the combined TupleDesc
439  *
440  * If there is just one function without ordinality, the scan result
441  * tupdesc is the same as the function result tupdesc --- except that we
442  * may stuff new names into it below, so drop any rowtype label.
443  */
444  if (scanstate->simple)
445  {
446  scan_tupdesc = CreateTupleDescCopy(scanstate->funcstates[0].tupdesc);
447  scan_tupdesc->tdtypeid = RECORDOID;
448  scan_tupdesc->tdtypmod = -1;
449  }
450  else
451  {
452  AttrNumber attno = 0;
453 
454  if (node->funcordinality)
455  natts++;
456 
457  scan_tupdesc = CreateTemplateTupleDesc(natts);
458 
459  for (i = 0; i < nfuncs; i++)
460  {
461  TupleDesc tupdesc = scanstate->funcstates[i].tupdesc;
462  int colcount = scanstate->funcstates[i].colcount;
463  int j;
464 
465  for (j = 1; j <= colcount; j++)
466  TupleDescCopyEntry(scan_tupdesc, ++attno, tupdesc, j);
467  }
468 
469  /* If doing ordinality, add a column of type "bigint" at the end */
470  if (node->funcordinality)
471  {
472  TupleDescInitEntry(scan_tupdesc,
473  ++attno,
474  NULL, /* don't care about the name here */
475  INT8OID,
476  -1,
477  0);
478  }
479 
480  Assert(attno == natts);
481  }
482 
483  /*
484  * Initialize scan slot and type.
485  */
486  ExecInitScanTupleSlot(estate, &scanstate->ss, scan_tupdesc,
488 
489  /*
490  * Initialize result slot, type and projection.
491  */
492  ExecInitResultTypeTL(&scanstate->ss.ps);
493  ExecAssignScanProjectionInfo(&scanstate->ss);
494 
495  /*
496  * initialize child expressions
497  */
498  scanstate->ss.ps.qual =
499  ExecInitQual(node->scan.plan.qual, (PlanState *) scanstate);
500 
501  /*
502  * Create a memory context that ExecMakeTableFunctionResult can use to
503  * evaluate function arguments in. We can't use the per-tuple context for
504  * this because it gets reset too often; but we don't want to leak
505  * evaluation results into the query-lifespan context either. We just
506  * need one context, because we evaluate each function separately.
507  */
509  "Table function arguments",
511 
512  return scanstate;
513 }
List * qual
Definition: plannodes.h:141
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:110
Plan plan
Definition: plannodes.h:340
static TupleTableSlot * ExecFunctionScan(PlanState *pstate)
#define AllocSetContextCreate
Definition: memutils.h:170
TupleTableSlot * ExecInitExtraTupleSlot(EState *estate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1796
TupleDesc BuildDescFromLists(List *names, List *types, List *typmods, List *collations)
Definition: tupdesc.c:894
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:44
List * functions
Definition: plannodes.h:521
ExprContext * ps_ExprContext
Definition: execnodes.h:979
Definition: nodes.h:525
MemoryContext argcontext
Definition: execnodes.h:1668
EState * state
Definition: execnodes.h:942
unsigned int Oid
Definition: postgres_ext.h:31
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:212
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:207
SetExprState * ExecInitTableFunctionResult(Expr *expr, ExprContext *econtext, PlanState *parent)
Definition: execSRF.c:56
PlanState ps
Definition: execnodes.h:1331
#define ERROR
Definition: elog.h:43
void ExecAssignScanProjectionInfo(ScanState *node)
Definition: execScan.c:272
void TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno, TupleDesc src, AttrNumber srcAttno)
Definition: tupdesc.c:271
int32 tdtypmod
Definition: tupdesc.h:83
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition: tupdesc.c:769
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
#define innerPlan(node)
Definition: plannodes.h:169
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2048
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1776
void ExecInitResultTypeTL(PlanState *planstate)
Definition: execTuples.c:1720
Tuplestorestate * tstore
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
TypeFuncClass
Definition: funcapi.h:147
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:603
#define outerPlan(node)
Definition: plannodes.h:170
struct FunctionScanPerFuncState * funcstates
Definition: execnodes.h:1667
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:946
TupleTableSlot * func_slot
Plan * plan
Definition: execnodes.h:940
List * funccoltypmods
Definition: parsenodes.h:1130
List * funccolcollations
Definition: parsenodes.h:1131
#define makeNode(_type_)
Definition: nodes.h:573
bool funcordinality
Definition: plannodes.h:522
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
#define EXEC_FLAG_MARK
Definition: executor.h:59
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:444
static int list_length(const List *l)
Definition: pg_list.h:169
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:720
ExprState * qual
Definition: execnodes.h:961
Oid tdtypeid
Definition: tupdesc.h:82
void * palloc(Size size)
Definition: mcxt.c:949
#define elog(elevel,...)
Definition: elog.h:226
int i
int16 AttrNumber
Definition: attnum.h:21
const TupleTableSlotOps TTSOpsMinimalTuple
Definition: execTuples.c:86

◆ ExecReScanFunctionScan()

void ExecReScanFunctionScan ( FunctionScanState node)

Definition at line 563 of file nodeFunctionscan.c.

References bms_overlap(), PlanState::chgParam, ExecClearTuple(), ExecScanReScan(), FunctionScanPerFuncState::func_slot, RangeTblFunction::funcparams, FunctionScanState::funcstates, FunctionScan::functions, i, lfirst, FunctionScanState::nfuncs, FunctionScanState::ordinal, PlanState::plan, ScanState::ps, PlanState::ps_ResultTupleSlot, FunctionScanPerFuncState::rowcount, FunctionScanState::ss, FunctionScanPerFuncState::tstore, tuplestore_end(), and tuplestore_rescan().

Referenced by ExecReScan().

564 {
565  FunctionScan *scan = (FunctionScan *) node->ss.ps.plan;
566  int i;
567  Bitmapset *chgparam = node->ss.ps.chgParam;
568 
569  if (node->ss.ps.ps_ResultTupleSlot)
571  for (i = 0; i < node->nfuncs; i++)
572  {
573  FunctionScanPerFuncState *fs = &node->funcstates[i];
574 
575  if (fs->func_slot)
577  }
578 
579  ExecScanReScan(&node->ss);
580 
581  /*
582  * Here we have a choice whether to drop the tuplestores (and recompute
583  * the function outputs) or just rescan them. We must recompute if an
584  * expression contains changed parameters, else we rescan.
585  *
586  * XXX maybe we should recompute if the function is volatile? But in
587  * general the executor doesn't conditionalize its actions on that.
588  */
589  if (chgparam)
590  {
591  ListCell *lc;
592 
593  i = 0;
594  foreach(lc, scan->functions)
595  {
596  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
597 
598  if (bms_overlap(chgparam, rtfunc->funcparams))
599  {
600  if (node->funcstates[i].tstore != NULL)
601  {
603  node->funcstates[i].tstore = NULL;
604  }
605  node->funcstates[i].rowcount = -1;
606  }
607  i++;
608  }
609  }
610 
611  /* Reset ordinality counter */
612  node->ordinal = 0;
613 
614  /* Make sure we rewind any remaining tuplestores */
615  for (i = 0; i < node->nfuncs; i++)
616  {
617  if (node->funcstates[i].tstore != NULL)
619  }
620 }
void tuplestore_rescan(Tuplestorestate *state)
Definition: tuplestore.c:1233
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:426
List * functions
Definition: plannodes.h:521
PlanState ps
Definition: execnodes.h:1331
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:978
Tuplestorestate * tstore
Bitmapset * chgParam
Definition: execnodes.h:972
struct FunctionScanPerFuncState * funcstates
Definition: execnodes.h:1667
TupleTableSlot * func_slot
Plan * plan
Definition: execnodes.h:940
#define lfirst(lc)
Definition: pg_list.h:190
void tuplestore_end(Tuplestorestate *state)
Definition: tuplestore.c:453
Bitmapset * funcparams
Definition: parsenodes.h:1133
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
int i
void ExecScanReScan(ScanState *node)
Definition: execScan.c:299