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)
 
TupleTableSlotExecFunctionScan (FunctionScanState *node)
 
void ExecEndFunctionScan (FunctionScanState *node)
 
void ExecReScanFunctionScan (FunctionScanState *node)
 

Function Documentation

void ExecEndFunctionScan ( FunctionScanState node)

Definition at line 521 of file nodeFunctionscan.c.

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

Referenced by ExecEndNode().

522 {
523  int i;
524 
525  /*
526  * Free the exprcontext
527  */
528  ExecFreeExprContext(&node->ss.ps);
529 
530  /*
531  * clean out the tuple table
532  */
535 
536  /*
537  * Release slots and tuplestore resources
538  */
539  for (i = 0; i < node->nfuncs; i++)
540  {
541  FunctionScanPerFuncState *fs = &node->funcstates[i];
542 
543  if (fs->func_slot)
545 
546  if (fs->tstore != NULL)
547  {
548  tuplestore_end(node->funcstates[i].tstore);
549  fs->tstore = NULL;
550  }
551  }
552 }
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1290
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:684
PlanState ps
Definition: execnodes.h:1287
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:1076
Tuplestorestate * tstore
struct FunctionScanPerFuncState * funcstates
Definition: execnodes.h:1555
TupleTableSlot * func_slot
#define NULL
Definition: c.h:226
void tuplestore_end(Tuplestorestate *state)
Definition: tuplestore.c:450
int i
TupleTableSlot* ExecFunctionScan ( FunctionScanState node)

Definition at line 266 of file nodeFunctionscan.c.

References ExecScan(), FunctionNext(), FunctionRecheck(), and FunctionScanState::ss.

Referenced by ExecProcNode().

267 {
268  return ExecScan(&node->ss,
271 }
TupleTableSlot * ExecScan(ScanState *node, ExecScanAccessMtd accessMtd, ExecScanRecheckMtd recheckMtd)
Definition: execScan.c:121
TupleTableSlot *(* ExecScanAccessMtd)(ScanState *node)
Definition: executor.h:271
bool(* ExecScanRecheckMtd)(ScanState *node, TupleTableSlot *slot)
Definition: executor.h:272
static TupleTableSlot * FunctionNext(FunctionScanState *node)
static bool FunctionRecheck(FunctionScanState *node, TupleTableSlot *slot)
FunctionScanState* ExecInitFunctionScan ( FunctionScan node,
EState estate,
int  eflags 
)

Definition at line 278 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(), ExecInitExpr(), ExecInitExtraTupleSlot(), ExecInitResultTupleSlot(), ExecInitScanTupleSlot(), ExecSetSlotDescriptor(), exprCollation(), FunctionScanPerFuncState::func_slot, RangeTblFunction::funccolcollations, RangeTblFunction::funccolcount, RangeTblFunction::funccolnames, RangeTblFunction::funccoltypes, RangeTblFunction::funccoltypmods, FunctionScanPerFuncState::funcexpr, RangeTblFunction::funcexpr, FunctionScan::funcordinality, FunctionScanState::funcstates, FunctionScan::functions, get_expr_result_type(), i, innerPlan, INT8OID, lfirst, list_length(), makeNode, tupleDesc::natts, FunctionScanState::nfuncs, NULL, FunctionScanState::ordinal, FunctionScanState::ordinality, outerPlan, palloc(), Scan::plan, PlanState::plan, ScanState::ps, Plan::qual, PlanState::qual, RECORDOID, FunctionScanPerFuncState::rowcount, FunctionScan::scan, FunctionScanState::simple, FunctionScanState::ss, PlanState::state, Plan::targetlist, PlanState::targetlist, tupleDesc::tdtypeid, tupleDesc::tdtypmod, FunctionScanPerFuncState::tstore, FunctionScanPerFuncState::tupdesc, TupleDescCopyEntry(), TupleDescInitEntry(), TupleDescInitEntryCollation(), TYPEFUNC_COMPOSITE, TYPEFUNC_RECORD, and TYPEFUNC_SCALAR.

Referenced by ExecInitNode().

279 {
280  FunctionScanState *scanstate;
281  int nfuncs = list_length(node->functions);
282  TupleDesc scan_tupdesc;
283  int i,
284  natts;
285  ListCell *lc;
286 
287  /* check for unsupported flags */
288  Assert(!(eflags & EXEC_FLAG_MARK));
289 
290  /*
291  * FunctionScan should not have any children.
292  */
293  Assert(outerPlan(node) == NULL);
294  Assert(innerPlan(node) == NULL);
295 
296  /*
297  * create new ScanState for node
298  */
299  scanstate = makeNode(FunctionScanState);
300  scanstate->ss.ps.plan = (Plan *) node;
301  scanstate->ss.ps.state = estate;
302  scanstate->eflags = eflags;
303 
304  /*
305  * are we adding an ordinality column?
306  */
307  scanstate->ordinality = node->funcordinality;
308 
309  scanstate->nfuncs = nfuncs;
310  if (nfuncs == 1 && !node->funcordinality)
311  scanstate->simple = true;
312  else
313  scanstate->simple = false;
314 
315  /*
316  * Ordinal 0 represents the "before the first row" position.
317  *
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.)
324  */
325  scanstate->ordinal = 0;
326 
327  /*
328  * Miscellaneous initialization
329  *
330  * create expression context for node
331  */
332  ExecAssignExprContext(estate, &scanstate->ss.ps);
333 
334  /*
335  * tuple table initialization
336  */
337  ExecInitResultTupleSlot(estate, &scanstate->ss.ps);
338  ExecInitScanTupleSlot(estate, &scanstate->ss);
339 
340  /*
341  * initialize child expressions
342  */
343  scanstate->ss.ps.targetlist = (List *)
345  (PlanState *) scanstate);
346  scanstate->ss.ps.qual = (List *)
347  ExecInitExpr((Expr *) node->scan.plan.qual,
348  (PlanState *) scanstate);
349 
350  scanstate->funcstates = palloc(nfuncs * sizeof(FunctionScanPerFuncState));
351 
352  natts = 0;
353  i = 0;
354  foreach(lc, node->functions)
355  {
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;
361  Oid funcrettype;
362  TupleDesc tupdesc;
363 
364  fs->funcexpr = ExecInitExpr((Expr *) funcexpr, (PlanState *) scanstate);
365 
366  /*
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).
370  */
371  fs->tstore = NULL;
372  fs->rowcount = -1;
373 
374  /*
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".
379  */
380  functypclass = get_expr_result_type(funcexpr,
381  &funcrettype,
382  &tupdesc);
383 
384  if (functypclass == TYPEFUNC_COMPOSITE)
385  {
386  /* Composite data type, e.g. a table's row type */
387  Assert(tupdesc);
388  Assert(tupdesc->natts >= colcount);
389  /* Must copy it out of typcache for safety */
390  tupdesc = CreateTupleDescCopy(tupdesc);
391  }
392  else if (functypclass == TYPEFUNC_SCALAR)
393  {
394  /* Base data type, i.e. scalar */
395  tupdesc = CreateTemplateTupleDesc(1, false);
396  TupleDescInitEntry(tupdesc,
397  (AttrNumber) 1,
398  NULL, /* don't care about the name here */
399  funcrettype,
400  -1,
401  0);
403  (AttrNumber) 1,
404  exprCollation(funcexpr));
405  }
406  else if (functypclass == TYPEFUNC_RECORD)
407  {
408  tupdesc = BuildDescFromLists(rtfunc->funccolnames,
409  rtfunc->funccoltypes,
410  rtfunc->funccoltypmods,
411  rtfunc->funccolcollations);
412 
413  /*
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
416  * case it doesn't.)
417  */
418  BlessTupleDesc(tupdesc);
419  }
420  else
421  {
422  /* crummy error message, but parser should have caught this */
423  elog(ERROR, "function in FROM has unsupported return type");
424  }
425 
426  fs->tupdesc = tupdesc;
427  fs->colcount = colcount;
428 
429  /*
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.
433  */
434  if (!scanstate->simple)
435  {
436  fs->func_slot = ExecInitExtraTupleSlot(estate);
438  }
439  else
440  fs->func_slot = NULL;
441 
442  natts += colcount;
443  i++;
444  }
445 
446  /*
447  * Create the combined TupleDesc
448  *
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.
452  */
453  if (scanstate->simple)
454  {
455  scan_tupdesc = CreateTupleDescCopy(scanstate->funcstates[0].tupdesc);
456  scan_tupdesc->tdtypeid = RECORDOID;
457  scan_tupdesc->tdtypmod = -1;
458  }
459  else
460  {
461  AttrNumber attno = 0;
462 
463  if (node->funcordinality)
464  natts++;
465 
466  scan_tupdesc = CreateTemplateTupleDesc(natts, false);
467 
468  for (i = 0; i < nfuncs; i++)
469  {
470  TupleDesc tupdesc = scanstate->funcstates[i].tupdesc;
471  int colcount = scanstate->funcstates[i].colcount;
472  int j;
473 
474  for (j = 1; j <= colcount; j++)
475  TupleDescCopyEntry(scan_tupdesc, ++attno, tupdesc, j);
476  }
477 
478  /* If doing ordinality, add a column of type "bigint" at the end */
479  if (node->funcordinality)
480  {
481  TupleDescInitEntry(scan_tupdesc,
482  ++attno,
483  NULL, /* don't care about the name here */
484  INT8OID,
485  -1,
486  0);
487  }
488 
489  Assert(attno == natts);
490  }
491 
492  ExecAssignScanType(&scanstate->ss, scan_tupdesc);
493 
494  /*
495  * Initialize result tuple type and projection info.
496  */
497  ExecAssignResultTypeFromTL(&scanstate->ss.ps);
498  ExecAssignScanProjectionInfo(&scanstate->ss);
499 
500  /*
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.
506  */
508  "Table function arguments",
510 
511  return scanstate;
512 }
List * qual
Definition: plannodes.h:130
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:141
Plan plan
Definition: plannodes.h:305
TupleTableSlot * ExecInitExtraTupleSlot(EState *estate)
Definition: execTuples.c:852
Oid tdtypeid
Definition: tupdesc.h:77
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate)
Definition: execTuples.c:842
TupleDesc BuildDescFromLists(List *names, List *types, List *typmods, List *collations)
Definition: tupdesc.c:771
List * functions
Definition: plannodes.h:484
List * qual
Definition: execnodes.h:1061
Definition: nodes.h:508
List * targetlist
Definition: execnodes.h:1060
MemoryContext argcontext
Definition: execnodes.h:1557
List * funccolnames
Definition: parsenodes.h:996
EState * state
Definition: execnodes.h:1048
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
void ExecAssignResultTypeFromTL(PlanState *planstate)
Definition: execUtils.c:429
int32 tdtypmod
Definition: tupdesc.h:78
PlanState ps
Definition: execnodes.h:1287
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execQual.c:4266
#define ERROR
Definition: elog.h:43
void ExecAssignScanProjectionInfo(ScanState *node)
Definition: execScan.c:235
void ExecInitResultTupleSlot(EState *estate, PlanState *planstate)
Definition: execTuples.c:832
void TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno, TupleDesc src, AttrNumber srcAttno)
Definition: tupdesc.c:229
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition: tupdesc.c:650
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:145
#define innerPlan(node)
Definition: plannodes.h:158
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:1031
Tuplestorestate * tstore
#define RECORDOID
Definition: pg_type.h:668
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:493
#define outerPlan(node)
Definition: plannodes.h:159
struct FunctionScanPerFuncState * funcstates
Definition: execnodes.h:1555
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:440
void ExecSetSlotDescriptor(TupleTableSlot *slot, TupleDesc tupdesc)
Definition: execTuples.c:247
TupleTableSlot * func_slot
Plan * plan
Definition: execnodes.h:1046
List * funccoltypmods
Definition: parsenodes.h:998
#define INT8OID
Definition: pg_type.h:304
List * funccolcollations
Definition: parsenodes.h:999
#define makeNode(_type_)
Definition: nodes.h:556
bool funcordinality
Definition: plannodes.h:485
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:670
#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:407
static int list_length(const List *l)
Definition: pg_list.h:89
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:748
TupleDesc CreateTemplateTupleDesc(int natts, bool hasoid)
Definition: tupdesc.c:41
List * targetlist
Definition: plannodes.h:129
void * palloc(Size size)
Definition: mcxt.c:891
int i
List * funccoltypes
Definition: parsenodes.h:997
void ExecAssignScanType(ScanState *scanstate, TupleDesc tupDesc)
Definition: execUtils.c:708
#define elog
Definition: elog.h:219
Definition: pg_list.h:45
int16 AttrNumber
Definition: attnum.h:21
void ExecReScanFunctionScan ( FunctionScanState node)

Definition at line 561 of file nodeFunctionscan.c.

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

Referenced by ExecReScan().

562 {
563  FunctionScan *scan = (FunctionScan *) node->ss.ps.plan;
564  int i;
565  Bitmapset *chgparam = node->ss.ps.chgParam;
566 
568  for (i = 0; i < node->nfuncs; i++)
569  {
570  FunctionScanPerFuncState *fs = &node->funcstates[i];
571 
572  if (fs->func_slot)
574  }
575 
576  ExecScanReScan(&node->ss);
577 
578  /*
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.
582  *
583  * XXX maybe we should recompute if the function is volatile? But in
584  * general the executor doesn't conditionalize its actions on that.
585  */
586  if (chgparam)
587  {
588  ListCell *lc;
589 
590  i = 0;
591  foreach(lc, scan->functions)
592  {
593  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
594 
595  if (bms_overlap(chgparam, rtfunc->funcparams))
596  {
597  if (node->funcstates[i].tstore != NULL)
598  {
600  node->funcstates[i].tstore = NULL;
601  }
602  node->funcstates[i].rowcount = -1;
603  }
604  i++;
605  }
606  }
607 
608  /* Reset ordinality counter */
609  node->ordinal = 0;
610 
611  /* Make sure we rewind any remaining tuplestores */
612  for (i = 0; i < node->nfuncs; i++)
613  {
614  if (node->funcstates[i].tstore != NULL)
616  }
617 }
void tuplestore_rescan(Tuplestorestate *state)
Definition: tuplestore.c:1216
List * functions
Definition: plannodes.h:484
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
PlanState ps
Definition: execnodes.h:1287
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:1076
Tuplestorestate * tstore
Bitmapset * chgParam
Definition: execnodes.h:1071
struct FunctionScanPerFuncState * funcstates
Definition: execnodes.h:1555
TupleTableSlot * func_slot
Plan * plan
Definition: execnodes.h:1046
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
void tuplestore_end(Tuplestorestate *state)
Definition: tuplestore.c:450
Bitmapset * funcparams
Definition: parsenodes.h:1001
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:442
int i
void ExecScanReScan(ScanState *node)
Definition: execScan.c:327