PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
nodeFunctionscan.c File Reference
#include "postgres.h"
#include "catalog/pg_type.h"
#include "executor/nodeFunctionscan.h"
#include "funcapi.h"
#include "nodes/nodeFuncs.h"
#include "utils/builtins.h"
#include "utils/memutils.h"
Include dependency graph for nodeFunctionscan.c:

Go to the source code of this file.

Data Structures

struct  FunctionScanPerFuncState
 

Typedefs

typedef struct
FunctionScanPerFuncState 
FunctionScanPerFuncState
 

Functions

static TupleTableSlotFunctionNext (FunctionScanState *node)
 
static bool FunctionRecheck (FunctionScanState *node, TupleTableSlot *slot)
 
TupleTableSlotExecFunctionScan (FunctionScanState *node)
 
FunctionScanStateExecInitFunctionScan (FunctionScan *node, EState *estate, int eflags)
 
void ExecEndFunctionScan (FunctionScanState *node)
 
void ExecReScanFunctionScan (FunctionScanState *node)
 

Typedef Documentation

Function Documentation

void ExecEndFunctionScan ( FunctionScanState node)

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

521 {
522  int i;
523 
524  /*
525  * Free the exprcontext
526  */
527  ExecFreeExprContext(&node->ss.ps);
528 
529  /*
530  * clean out the tuple table
531  */
534 
535  /*
536  * Release slots and tuplestore resources
537  */
538  for (i = 0; i < node->nfuncs; i++)
539  {
540  FunctionScanPerFuncState *fs = &node->funcstates[i];
541 
542  if (fs->func_slot)
544 
545  if (fs->tstore != NULL)
546  {
547  tuplestore_end(node->funcstates[i].tstore);
548  fs->tstore = NULL;
549  }
550  }
551 }
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1047
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:511
PlanState ps
Definition: execnodes.h:1044
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:829
Tuplestorestate * tstore
struct FunctionScanPerFuncState * funcstates
Definition: execnodes.h:1369
TupleTableSlot * func_slot
#define NULL
Definition: c.h:229
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:389
bool(* ExecScanRecheckMtd)(ScanState *node, TupleTableSlot *slot)
Definition: executor.h:390
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(), ExecInitExtraTupleSlot(), ExecInitQual(), ExecInitResultTupleSlot(), ExecInitScanTupleSlot(), ExecInitTableFunctionResult(), 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, NULL, 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().

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.qual =
344  ExecInitQual(node->scan.plan.qual, (PlanState *) scanstate);
345 
346  scanstate->funcstates = palloc(nfuncs * sizeof(FunctionScanPerFuncState));
347 
348  natts = 0;
349  i = 0;
350  foreach(lc, node->functions)
351  {
352  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
353  Node *funcexpr = rtfunc->funcexpr;
354  int colcount = rtfunc->funccolcount;
355  FunctionScanPerFuncState *fs = &scanstate->funcstates[i];
356  TypeFuncClass functypclass;
357  Oid funcrettype;
358  TupleDesc tupdesc;
359 
360  fs->setexpr =
361  ExecInitTableFunctionResult((Expr *) funcexpr,
362  scanstate->ss.ps.ps_ExprContext,
363  &scanstate->ss.ps);
364 
365  /*
366  * Don't allocate the tuplestores; the actual calls to the functions
367  * do that. NULL means that we have not called the function yet (or
368  * need to call it again after a rescan).
369  */
370  fs->tstore = NULL;
371  fs->rowcount = -1;
372 
373  /*
374  * Now determine if the function returns a simple or composite type,
375  * and build an appropriate tupdesc. Note that in the composite case,
376  * the function may now return more columns than it did when the plan
377  * was made; we have to ignore any columns beyond "colcount".
378  */
379  functypclass = get_expr_result_type(funcexpr,
380  &funcrettype,
381  &tupdesc);
382 
383  if (functypclass == TYPEFUNC_COMPOSITE)
384  {
385  /* Composite data type, e.g. a table's row type */
386  Assert(tupdesc);
387  Assert(tupdesc->natts >= colcount);
388  /* Must copy it out of typcache for safety */
389  tupdesc = CreateTupleDescCopy(tupdesc);
390  }
391  else if (functypclass == TYPEFUNC_SCALAR)
392  {
393  /* Base data type, i.e. scalar */
394  tupdesc = CreateTemplateTupleDesc(1, false);
395  TupleDescInitEntry(tupdesc,
396  (AttrNumber) 1,
397  NULL, /* don't care about the name here */
398  funcrettype,
399  -1,
400  0);
402  (AttrNumber) 1,
403  exprCollation(funcexpr));
404  }
405  else if (functypclass == TYPEFUNC_RECORD)
406  {
407  tupdesc = BuildDescFromLists(rtfunc->funccolnames,
408  rtfunc->funccoltypes,
409  rtfunc->funccoltypmods,
410  rtfunc->funccolcollations);
411 
412  /*
413  * For RECORD results, make sure a typmod has been assigned. (The
414  * function should do this for itself, but let's cover things in
415  * case it doesn't.)
416  */
417  BlessTupleDesc(tupdesc);
418  }
419  else
420  {
421  /* crummy error message, but parser should have caught this */
422  elog(ERROR, "function in FROM has unsupported return type");
423  }
424 
425  fs->tupdesc = tupdesc;
426  fs->colcount = colcount;
427 
428  /*
429  * We only need separate slots for the function results if we are
430  * doing ordinality or multiple functions; otherwise, we'll fetch
431  * function results directly into the scan slot.
432  */
433  if (!scanstate->simple)
434  {
435  fs->func_slot = ExecInitExtraTupleSlot(estate);
437  }
438  else
439  fs->func_slot = NULL;
440 
441  natts += colcount;
442  i++;
443  }
444 
445  /*
446  * Create the combined TupleDesc
447  *
448  * If there is just one function without ordinality, the scan result
449  * tupdesc is the same as the function result tupdesc --- except that we
450  * may stuff new names into it below, so drop any rowtype label.
451  */
452  if (scanstate->simple)
453  {
454  scan_tupdesc = CreateTupleDescCopy(scanstate->funcstates[0].tupdesc);
455  scan_tupdesc->tdtypeid = RECORDOID;
456  scan_tupdesc->tdtypmod = -1;
457  }
458  else
459  {
460  AttrNumber attno = 0;
461 
462  if (node->funcordinality)
463  natts++;
464 
465  scan_tupdesc = CreateTemplateTupleDesc(natts, false);
466 
467  for (i = 0; i < nfuncs; i++)
468  {
469  TupleDesc tupdesc = scanstate->funcstates[i].tupdesc;
470  int colcount = scanstate->funcstates[i].colcount;
471  int j;
472 
473  for (j = 1; j <= colcount; j++)
474  TupleDescCopyEntry(scan_tupdesc, ++attno, tupdesc, j);
475  }
476 
477  /* If doing ordinality, add a column of type "bigint" at the end */
478  if (node->funcordinality)
479  {
480  TupleDescInitEntry(scan_tupdesc,
481  ++attno,
482  NULL, /* don't care about the name here */
483  INT8OID,
484  -1,
485  0);
486  }
487 
488  Assert(attno == natts);
489  }
490 
491  ExecAssignScanType(&scanstate->ss, scan_tupdesc);
492 
493  /*
494  * Initialize result tuple type and projection info.
495  */
496  ExecAssignResultTypeFromTL(&scanstate->ss.ps);
497  ExecAssignScanProjectionInfo(&scanstate->ss);
498 
499  /*
500  * Create a memory context that ExecMakeTableFunctionResult can use to
501  * evaluate function arguments in. We can't use the per-tuple context for
502  * this because it gets reset too often; but we don't want to leak
503  * evaluation results into the query-lifespan context either. We just
504  * need one context, because we evaluate each function separately.
505  */
507  "Table function arguments",
509 
510  return scanstate;
511 }
List * qual
Definition: plannodes.h:133
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:141
Plan plan
Definition: plannodes.h:315
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:495
ExprContext * ps_ExprContext
Definition: execnodes.h:830
Definition: nodes.h:506
MemoryContext argcontext
Definition: execnodes.h:1371
EState * state
Definition: execnodes.h:802
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:435
int32 tdtypmod
Definition: tupdesc.h:78
SetExprState * ExecInitTableFunctionResult(Expr *expr, ExprContext *econtext, PlanState *parent)
Definition: execSRF.c:55
PlanState ps
Definition: execnodes.h:1044
#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:165
#define innerPlan(node)
Definition: plannodes.h:161
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:1031
Tuplestorestate * tstore
#define RECORDOID
Definition: pg_type.h:676
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:162
struct FunctionScanPerFuncState * funcstates
Definition: execnodes.h:1369
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
void ExecSetSlotDescriptor(TupleTableSlot *slot, TupleDesc tupdesc)
Definition: execTuples.c:247
TupleTableSlot * func_slot
Plan * plan
Definition: execnodes.h:800
List * funccoltypmods
Definition: parsenodes.h:1037
#define INT8OID
Definition: pg_type.h:304
List * funccolcollations
Definition: parsenodes.h:1038
#define makeNode(_type_)
Definition: nodes.h:554
bool funcordinality
Definition: plannodes.h:496
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#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:413
static int list_length(const List *l)
Definition: pg_list.h:89
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:745
TupleDesc CreateTemplateTupleDesc(int natts, bool hasoid)
Definition: tupdesc.c:41
ExprState * qual
Definition: execnodes.h:814
void * palloc(Size size)
Definition: mcxt.c:849
int i
void ExecAssignScanType(ScanState *scanstate, TupleDesc tupDesc)
Definition: execUtils.c:535
#define elog
Definition: elog.h:219
int16 AttrNumber
Definition: attnum.h:21
void ExecReScanFunctionScan ( FunctionScanState node)

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

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

Definition at line 60 of file nodeFunctionscan.c.

References FunctionScanState::argcontext, FunctionScanPerFuncState::colcount, FunctionScanState::eflags, EState::es_direction, EXEC_FLAG_BACKWARD, ExecClearTuple(), ExecMakeTableFunctionResult(), ExecStoreVirtualTuple(), FunctionScanPerFuncState::func_slot, FunctionScanState::funcstates, i, Int64GetDatumFast, FunctionScanState::nfuncs, NULL, FunctionScanState::ordinal, FunctionScanState::ordinality, ScanState::ps, PlanState::ps_ExprContext, FunctionScanPerFuncState::rowcount, ScanDirectionIsForward, FunctionScanPerFuncState::setexpr, FunctionScanState::simple, slot_getallattrs(), FunctionScanState::ss, ScanState::ss_ScanTupleSlot, PlanState::state, FunctionScanPerFuncState::tstore, TupleTableSlot::tts_isnull, TupleTableSlot::tts_values, FunctionScanPerFuncState::tupdesc, TupIsNull, tuplestore_gettupleslot(), and tuplestore_rescan().

Referenced by ExecFunctionScan().

61 {
62  EState *estate;
63  ScanDirection direction;
64  TupleTableSlot *scanslot;
65  bool alldone;
66  int64 oldpos;
67  int funcno;
68  int att;
69 
70  /*
71  * get information from the estate and scan state
72  */
73  estate = node->ss.ps.state;
74  direction = estate->es_direction;
75  scanslot = node->ss.ss_ScanTupleSlot;
76 
77  if (node->simple)
78  {
79  /*
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
83  * rowcounts either.
84  */
85  Tuplestorestate *tstore = node->funcstates[0].tstore;
86 
87  /*
88  * If first time through, read all tuples from function and put them
89  * in a tuplestore. Subsequent calls just fetch tuples from
90  * tuplestore.
91  */
92  if (tstore == NULL)
93  {
94  node->funcstates[0].tstore = tstore =
96  node->ss.ps.ps_ExprContext,
97  node->argcontext,
98  node->funcstates[0].tupdesc,
99  node->eflags & EXEC_FLAG_BACKWARD);
100 
101  /*
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.
105  */
106  tuplestore_rescan(tstore);
107  }
108 
109  /*
110  * Get the next tuple from tuplestore.
111  */
112  (void) tuplestore_gettupleslot(tstore,
113  ScanDirectionIsForward(direction),
114  false,
115  scanslot);
116  return scanslot;
117  }
118 
119  /*
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.
124  */
125  oldpos = node->ordinal;
126  if (ScanDirectionIsForward(direction))
127  node->ordinal++;
128  else
129  node->ordinal--;
130 
131  /*
132  * Main loop over functions.
133  *
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.
137  */
138  ExecClearTuple(scanslot);
139  att = 0;
140  alldone = true;
141  for (funcno = 0; funcno < node->nfuncs; funcno++)
142  {
143  FunctionScanPerFuncState *fs = &node->funcstates[funcno];
144  int i;
145 
146  /*
147  * If first time through, read all tuples from function and put them
148  * in a tuplestore. Subsequent calls just fetch tuples from
149  * tuplestore.
150  */
151  if (fs->tstore == NULL)
152  {
153  fs->tstore =
155  node->ss.ps.ps_ExprContext,
156  node->argcontext,
157  fs->tupdesc,
158  node->eflags & EXEC_FLAG_BACKWARD);
159 
160  /*
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.
164  */
166  }
167 
168  /*
169  * Get the next tuple from tuplestore.
170  *
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.
174  */
175  if (fs->rowcount != -1 && fs->rowcount < oldpos)
177  else
178  (void) tuplestore_gettupleslot(fs->tstore,
179  ScanDirectionIsForward(direction),
180  false,
181  fs->func_slot);
182 
183  if (TupIsNull(fs->func_slot))
184  {
185  /*
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.
191  */
192  if (ScanDirectionIsForward(direction) && fs->rowcount == -1)
193  fs->rowcount = node->ordinal;
194 
195  /*
196  * populate the result cols with nulls
197  */
198  for (i = 0; i < fs->colcount; i++)
199  {
200  scanslot->tts_values[att] = (Datum) 0;
201  scanslot->tts_isnull[att] = true;
202  att++;
203  }
204  }
205  else
206  {
207  /*
208  * we have a result, so just copy it to the result cols.
209  */
211 
212  for (i = 0; i < fs->colcount; i++)
213  {
214  scanslot->tts_values[att] = fs->func_slot->tts_values[i];
215  scanslot->tts_isnull[att] = fs->func_slot->tts_isnull[i];
216  att++;
217  }
218 
219  /*
220  * We're not done until every function result is exhausted; we pad
221  * the shorter results with nulls until then.
222  */
223  alldone = false;
224  }
225  }
226 
227  /*
228  * ordinal col is always last, per spec.
229  */
230  if (node->ordinality)
231  {
232  scanslot->tts_values[att] = Int64GetDatumFast(node->ordinal);
233  scanslot->tts_isnull[att] = false;
234  }
235 
236  /*
237  * If alldone, we just return the previously-cleared scanslot. Otherwise,
238  * finish creating the virtual tuple.
239  */
240  if (!alldone)
241  ExecStoreVirtualTuple(scanslot);
242 
243  return scanslot;
244 }
void tuplestore_rescan(Tuplestorestate *state)
Definition: tuplestore.c:1216
#define ScanDirectionIsForward(direction)
Definition: sdir.h:55
ExprContext * ps_ExprContext
Definition: execnodes.h:830
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
Datum * tts_values
Definition: tuptable.h:125
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1047
MemoryContext argcontext
Definition: execnodes.h:1371
EState * state
Definition: execnodes.h:802
ScanDirection es_direction
Definition: execnodes.h:407
PlanState ps
Definition: execnodes.h:1044
#define EXEC_FLAG_BACKWARD
Definition: executor.h:60
bool * tts_isnull
Definition: tuptable.h:126
Tuplestorestate * ExecMakeTableFunctionResult(SetExprState *setexpr, ExprContext *econtext, MemoryContext argContext, TupleDesc expectedDesc, bool randomAccess)
Definition: execSRF.c:100
ScanDirection
Definition: sdir.h:22
Tuplestorestate * tstore
#define TupIsNull(slot)
Definition: tuptable.h:138
void slot_getallattrs(TupleTableSlot *slot)
Definition: heaptuple.c:1239
struct FunctionScanPerFuncState * funcstates
Definition: execnodes.h:1369
uintptr_t Datum
Definition: postgres.h:372
#define Int64GetDatumFast(X)
Definition: postgres.h:781
TupleTableSlot * func_slot
bool tuplestore_gettupleslot(Tuplestorestate *state, bool forward, bool copy, TupleTableSlot *slot)
Definition: tuplestore.c:1061
#define NULL
Definition: c.h:229
int i
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:488
static bool FunctionRecheck ( FunctionScanState node,
TupleTableSlot slot 
)
static

Definition at line 250 of file nodeFunctionscan.c.

Referenced by ExecFunctionScan().

251 {
252  /* nothing to check */
253  return true;
254 }