PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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

void ExecEndFunctionScan ( FunctionScanState node)

Definition at line 523 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().

524 {
525  int i;
526 
527  /*
528  * Free the exprcontext
529  */
530  ExecFreeExprContext(&node->ss.ps);
531 
532  /*
533  * clean out the tuple table
534  */
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 }
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1104
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:521
PlanState ps
Definition: execnodes.h:1101
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:880
Tuplestorestate * tstore
struct FunctionScanPerFuncState * funcstates
Definition: execnodes.h:1426
TupleTableSlot * func_slot
void tuplestore_end(Tuplestorestate *state)
Definition: tuplestore.c:453
int i
FunctionScanState* ExecInitFunctionScan ( FunctionScan node,
EState estate,
int  eflags 
)

Definition at line 280 of file nodeFunctionscan.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate(), FunctionScanState::argcontext, Assert, BlessTupleDesc(), BuildDescFromLists(), FunctionScanPerFuncState::colcount, CreateTemplateTupleDesc(), CreateTupleDescCopy(), CurrentMemoryContext, FunctionScanState::eflags, elog, ERROR, EXEC_FLAG_MARK, ExecAssignExprContext(), ExecAssignResultTypeFromTL(), ExecAssignScanProjectionInfo(), ExecAssignScanType(), ExecFunctionScan(), ExecInitExtraTupleSlot(), ExecInitQual(), ExecInitResultTupleSlot(), ExecInitScanTupleSlot(), ExecInitTableFunctionResult(), PlanState::ExecProcNode, ExecSetSlotDescriptor(), 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, INT8OID, lfirst, list_length(), makeNode, tupleDesc::natts, FunctionScanState::nfuncs, FunctionScanState::ordinal, FunctionScanState::ordinality, outerPlan, palloc(), Scan::plan, PlanState::plan, ScanState::ps, PlanState::ps_ExprContext, Plan::qual, PlanState::qual, RECORDOID, FunctionScanPerFuncState::rowcount, FunctionScan::scan, FunctionScanPerFuncState::setexpr, FunctionScanState::simple, FunctionScanState::ss, PlanState::state, tupleDesc::tdtypeid, tupleDesc::tdtypmod, FunctionScanPerFuncState::tstore, FunctionScanPerFuncState::tupdesc, TupleDescCopyEntry(), TupleDescInitEntry(), TupleDescInitEntryCollation(), TYPEFUNC_COMPOSITE, 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  /*
338  * tuple table initialization
339  */
340  ExecInitResultTupleSlot(estate, &scanstate->ss.ps);
341  ExecInitScanTupleSlot(estate, &scanstate->ss);
342 
343  /*
344  * initialize child expressions
345  */
346  scanstate->ss.ps.qual =
347  ExecInitQual(node->scan.plan.qual, (PlanState *) scanstate);
348 
349  scanstate->funcstates = palloc(nfuncs * sizeof(FunctionScanPerFuncState));
350 
351  natts = 0;
352  i = 0;
353  foreach(lc, node->functions)
354  {
355  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
356  Node *funcexpr = rtfunc->funcexpr;
357  int colcount = rtfunc->funccolcount;
358  FunctionScanPerFuncState *fs = &scanstate->funcstates[i];
359  TypeFuncClass functypclass;
360  Oid funcrettype;
361  TupleDesc tupdesc;
362 
363  fs->setexpr =
364  ExecInitTableFunctionResult((Expr *) funcexpr,
365  scanstate->ss.ps.ps_ExprContext,
366  &scanstate->ss.ps);
367 
368  /*
369  * Don't allocate the tuplestores; the actual calls to the functions
370  * do that. NULL means that we have not called the function yet (or
371  * need to call it again after a rescan).
372  */
373  fs->tstore = NULL;
374  fs->rowcount = -1;
375 
376  /*
377  * Now determine if the function returns a simple or composite type,
378  * and build an appropriate tupdesc. Note that in the composite case,
379  * the function may now return more columns than it did when the plan
380  * was made; we have to ignore any columns beyond "colcount".
381  */
382  functypclass = get_expr_result_type(funcexpr,
383  &funcrettype,
384  &tupdesc);
385 
386  if (functypclass == TYPEFUNC_COMPOSITE)
387  {
388  /* Composite data type, e.g. a table's row type */
389  Assert(tupdesc);
390  Assert(tupdesc->natts >= colcount);
391  /* Must copy it out of typcache for safety */
392  tupdesc = CreateTupleDescCopy(tupdesc);
393  }
394  else if (functypclass == TYPEFUNC_SCALAR)
395  {
396  /* Base data type, i.e. scalar */
397  tupdesc = CreateTemplateTupleDesc(1, false);
398  TupleDescInitEntry(tupdesc,
399  (AttrNumber) 1,
400  NULL, /* don't care about the name here */
401  funcrettype,
402  -1,
403  0);
405  (AttrNumber) 1,
406  exprCollation(funcexpr));
407  }
408  else if (functypclass == TYPEFUNC_RECORD)
409  {
410  tupdesc = BuildDescFromLists(rtfunc->funccolnames,
411  rtfunc->funccoltypes,
412  rtfunc->funccoltypmods,
413  rtfunc->funccolcollations);
414 
415  /*
416  * For RECORD results, make sure a typmod has been assigned. (The
417  * function should do this for itself, but let's cover things in
418  * case it doesn't.)
419  */
420  BlessTupleDesc(tupdesc);
421  }
422  else
423  {
424  /* crummy error message, but parser should have caught this */
425  elog(ERROR, "function in FROM has unsupported return type");
426  }
427 
428  fs->tupdesc = tupdesc;
429  fs->colcount = colcount;
430 
431  /*
432  * We only need separate slots for the function results if we are
433  * doing ordinality or multiple functions; otherwise, we'll fetch
434  * function results directly into the scan slot.
435  */
436  if (!scanstate->simple)
437  {
438  fs->func_slot = ExecInitExtraTupleSlot(estate);
440  }
441  else
442  fs->func_slot = NULL;
443 
444  natts += colcount;
445  i++;
446  }
447 
448  /*
449  * Create the combined TupleDesc
450  *
451  * If there is just one function without ordinality, the scan result
452  * tupdesc is the same as the function result tupdesc --- except that we
453  * may stuff new names into it below, so drop any rowtype label.
454  */
455  if (scanstate->simple)
456  {
457  scan_tupdesc = CreateTupleDescCopy(scanstate->funcstates[0].tupdesc);
458  scan_tupdesc->tdtypeid = RECORDOID;
459  scan_tupdesc->tdtypmod = -1;
460  }
461  else
462  {
463  AttrNumber attno = 0;
464 
465  if (node->funcordinality)
466  natts++;
467 
468  scan_tupdesc = CreateTemplateTupleDesc(natts, false);
469 
470  for (i = 0; i < nfuncs; i++)
471  {
472  TupleDesc tupdesc = scanstate->funcstates[i].tupdesc;
473  int colcount = scanstate->funcstates[i].colcount;
474  int j;
475 
476  for (j = 1; j <= colcount; j++)
477  TupleDescCopyEntry(scan_tupdesc, ++attno, tupdesc, j);
478  }
479 
480  /* If doing ordinality, add a column of type "bigint" at the end */
481  if (node->funcordinality)
482  {
483  TupleDescInitEntry(scan_tupdesc,
484  ++attno,
485  NULL, /* don't care about the name here */
486  INT8OID,
487  -1,
488  0);
489  }
490 
491  Assert(attno == natts);
492  }
493 
494  ExecAssignScanType(&scanstate->ss, scan_tupdesc);
495 
496  /*
497  * Initialize result tuple type and projection info.
498  */
499  ExecAssignResultTypeFromTL(&scanstate->ss.ps);
500  ExecAssignScanProjectionInfo(&scanstate->ss);
501 
502  /*
503  * Create a memory context that ExecMakeTableFunctionResult can use to
504  * evaluate function arguments in. We can't use the per-tuple context for
505  * this because it gets reset too often; but we don't want to leak
506  * evaluation results into the query-lifespan context either. We just
507  * need one context, because we evaluate each function separately.
508  */
510  "Table function arguments",
512 
513  return scanstate;
514 }
List * qual
Definition: plannodes.h:145
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:102
Plan plan
Definition: plannodes.h:328
static TupleTableSlot * ExecFunctionScan(PlanState *pstate)
TupleTableSlot * ExecInitExtraTupleSlot(EState *estate)
Definition: execTuples.c:852
Oid tdtypeid
Definition: tupdesc.h:74
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate)
Definition: execTuples.c:842
TupleDesc BuildDescFromLists(List *names, List *types, List *typmods, List *collations)
Definition: tupdesc.c:787
List * functions
Definition: plannodes.h:508
ExprContext * ps_ExprContext
Definition: execnodes.h:881
Definition: nodes.h:510
MemoryContext argcontext
Definition: execnodes.h:1427
EState * state
Definition: execnodes.h:849
unsigned int Oid
Definition: postgres_ext.h:31
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:227
int natts
Definition: tupdesc.h:73
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:160
void ExecAssignResultTypeFromTL(PlanState *planstate)
Definition: execUtils.c:445
int32 tdtypmod
Definition: tupdesc.h:75
SetExprState * ExecInitTableFunctionResult(Expr *expr, ExprContext *econtext, PlanState *parent)
Definition: execSRF.c:56
PlanState ps
Definition: execnodes.h:1101
#define ERROR
Definition: elog.h:43
void ExecAssignScanProjectionInfo(ScanState *node)
Definition: execScan.c:237
void ExecInitResultTupleSlot(EState *estate, PlanState *planstate)
Definition: execTuples.c:832
void TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno, TupleDesc src, AttrNumber srcAttno)
Definition: tupdesc.c:211
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition: tupdesc.c:664
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:165
#define innerPlan(node)
Definition: plannodes.h:173
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:1032
Tuplestorestate * tstore
#define RECORDOID
Definition: pg_type.h:680
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
TypeFuncClass
Definition: funcapi.h:150
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:505
#define outerPlan(node)
Definition: plannodes.h:174
struct FunctionScanPerFuncState * funcstates
Definition: execnodes.h:1426
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:853
void ExecSetSlotDescriptor(TupleTableSlot *slot, TupleDesc tupdesc)
Definition: execTuples.c:247
TupleTableSlot * func_slot
Plan * plan
Definition: execnodes.h:847
List * funccoltypmods
Definition: parsenodes.h:1086
#define INT8OID
Definition: pg_type.h:304
List * funccolcollations
Definition: parsenodes.h:1087
#define makeNode(_type_)
Definition: nodes.h:558
bool funcordinality
Definition: plannodes.h:509
#define Assert(condition)
Definition: c.h:681
#define lfirst(lc)
Definition: pg_list.h:106
#define EXEC_FLAG_MARK
Definition: executor.h:61
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:423
static int list_length(const List *l)
Definition: pg_list.h:89
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:720
TupleDesc CreateTemplateTupleDesc(int natts, bool hasoid)
Definition: tupdesc.c:43
ExprState * qual
Definition: execnodes.h:865
void * palloc(Size size)
Definition: mcxt.c:848
int i
void ExecAssignScanType(ScanState *scanstate, TupleDesc tupDesc)
Definition: execUtils.c:545
#define elog
Definition: elog.h:219
int16 AttrNumber
Definition: attnum.h:21
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 
570  for (i = 0; i < node->nfuncs; i++)
571  {
572  FunctionScanPerFuncState *fs = &node->funcstates[i];
573 
574  if (fs->func_slot)
576  }
577 
578  ExecScanReScan(&node->ss);
579 
580  /*
581  * Here we have a choice whether to drop the tuplestores (and recompute
582  * the function outputs) or just rescan them. We must recompute if an
583  * expression contains changed parameters, else we rescan.
584  *
585  * XXX maybe we should recompute if the function is volatile? But in
586  * general the executor doesn't conditionalize its actions on that.
587  */
588  if (chgparam)
589  {
590  ListCell *lc;
591 
592  i = 0;
593  foreach(lc, scan->functions)
594  {
595  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
596 
597  if (bms_overlap(chgparam, rtfunc->funcparams))
598  {
599  if (node->funcstates[i].tstore != NULL)
600  {
602  node->funcstates[i].tstore = NULL;
603  }
604  node->funcstates[i].rowcount = -1;
605  }
606  i++;
607  }
608  }
609 
610  /* Reset ordinality counter */
611  node->ordinal = 0;
612 
613  /* Make sure we rewind any remaining tuplestores */
614  for (i = 0; i < node->nfuncs; i++)
615  {
616  if (node->funcstates[i].tstore != NULL)
618  }
619 }
void tuplestore_rescan(Tuplestorestate *state)
Definition: tuplestore.c:1233
List * functions
Definition: plannodes.h:508
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
PlanState ps
Definition: execnodes.h:1101
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:880
Tuplestorestate * tstore
Bitmapset * chgParam
Definition: execnodes.h:875
struct FunctionScanPerFuncState * funcstates
Definition: execnodes.h:1426
TupleTableSlot * func_slot
Plan * plan
Definition: execnodes.h:847
#define lfirst(lc)
Definition: pg_list.h:106
void tuplestore_end(Tuplestorestate *state)
Definition: tuplestore.c:453
Bitmapset * funcparams
Definition: parsenodes.h:1089
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:443
int i
void ExecScanReScan(ScanState *node)
Definition: execScan.c:329