PostgreSQL Source Code  git master
nodeTableFuncscan.c File Reference
#include "postgres.h"
#include "nodes/execnodes.h"
#include "executor/executor.h"
#include "executor/nodeTableFuncscan.h"
#include "executor/tablefunc.h"
#include "miscadmin.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/xml.h"
Include dependency graph for nodeTableFuncscan.c:

Go to the source code of this file.

Functions

static TupleTableSlotTableFuncNext (TableFuncScanState *node)
 
static bool TableFuncRecheck (TableFuncScanState *node, TupleTableSlot *slot)
 
static void tfuncFetchRows (TableFuncScanState *tstate, ExprContext *econtext)
 
static void tfuncInitialize (TableFuncScanState *tstate, ExprContext *econtext, Datum doc)
 
static void tfuncLoadRows (TableFuncScanState *tstate, ExprContext *econtext)
 
static TupleTableSlotExecTableFuncScan (PlanState *pstate)
 
TableFuncScanStateExecInitTableFuncScan (TableFuncScan *node, EState *estate, int eflags)
 
void ExecEndTableFuncScan (TableFuncScanState *node)
 
void ExecReScanTableFuncScan (TableFuncScanState *node)
 

Function Documentation

◆ ExecEndTableFuncScan()

void ExecEndTableFuncScan ( TableFuncScanState node)

Definition at line 215 of file nodeTableFuncscan.c.

References ExecClearTuple(), ExecFreeExprContext(), ScanState::ps, PlanState::ps_ResultTupleSlot, TableFuncScanState::ss, ScanState::ss_ScanTupleSlot, tuplestore_end(), and TableFuncScanState::tupstore.

Referenced by ExecEndNode().

216 {
217  /*
218  * Free the exprcontext
219  */
220  ExecFreeExprContext(&node->ss.ps);
221 
222  /*
223  * clean out the tuple table
224  */
225  if (node->ss.ps.ps_ResultTupleSlot)
228 
229  /*
230  * Release tuplestore resources
231  */
232  if (node->tupstore != NULL)
233  tuplestore_end(node->tupstore);
234  node->tupstore = NULL;
235 }
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:426
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1283
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:616
PlanState ps
Definition: execnodes.h:1280
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:983
void tuplestore_end(Tuplestorestate *state)
Definition: tuplestore.c:453
Tuplestorestate * tupstore
Definition: execnodes.h:1668

◆ ExecInitTableFuncScan()

TableFuncScanState* ExecInitTableFuncScan ( TableFuncScan node,
EState estate,
int  eflags 
)

Definition at line 111 of file nodeTableFuncscan.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert, atttypid, BuildDescFromLists(), TableFunc::colcollations, TableFunc::coldefexprs, TableFunc::colexprs, TableFunc::colnames, TableFunc::coltypes, TableFunc::coltypmods, CurrentMemoryContext, TableFunc::docexpr, EXEC_FLAG_MARK, ExecAssignExprContext(), ExecAssignScanProjectionInfo(), ExecInitExpr(), ExecInitExprList(), ExecInitQual(), ExecInitResultTypeTL(), ExecInitScanTupleSlot(), PlanState::ExecProcNode, ExecTableFuncScan(), fmgr_info(), getTypeInputInfo(), i, innerPlan, makeNode, TupleDescData::natts, TableFunc::notnulls, TableFunc::ns_names, TableFuncScanState::ns_names, TableFunc::ns_uris, TableFuncScanState::ns_uris, TableFuncScanState::opaque, outerPlan, palloc(), TableFuncScanState::perTableCxt, Scan::plan, PlanState::plan, ScanState::ps, Plan::qual, PlanState::qual, TableFuncScanState::routine, TableFunc::rowexpr, TableFuncScan::scan, TableFuncScanState::ss, PlanState::state, TableFuncScan::tablefunc, TTSOpsMinimalTuple, TupleDescAttr, and XmlTableRoutine.

Referenced by ExecInitNode().

112 {
113  TableFuncScanState *scanstate;
114  TableFunc *tf = node->tablefunc;
115  TupleDesc tupdesc;
116  int i;
117 
118  /* check for unsupported flags */
119  Assert(!(eflags & EXEC_FLAG_MARK));
120 
121  /*
122  * TableFuncscan should not have any children.
123  */
124  Assert(outerPlan(node) == NULL);
125  Assert(innerPlan(node) == NULL);
126 
127  /*
128  * create new ScanState for node
129  */
130  scanstate = makeNode(TableFuncScanState);
131  scanstate->ss.ps.plan = (Plan *) node;
132  scanstate->ss.ps.state = estate;
133  scanstate->ss.ps.ExecProcNode = ExecTableFuncScan;
134 
135  /*
136  * Miscellaneous initialization
137  *
138  * create expression context for node
139  */
140  ExecAssignExprContext(estate, &scanstate->ss.ps);
141 
142  /*
143  * initialize source tuple type
144  */
145  tupdesc = BuildDescFromLists(tf->colnames,
146  tf->coltypes,
147  tf->coltypmods,
148  tf->colcollations);
149  /* and the corresponding scan slot */
150  ExecInitScanTupleSlot(estate, &scanstate->ss, tupdesc,
152 
153  /*
154  * Initialize result type and projection.
155  */
156  ExecInitResultTypeTL(&scanstate->ss.ps);
157  ExecAssignScanProjectionInfo(&scanstate->ss);
158 
159  /*
160  * initialize child expressions
161  */
162  scanstate->ss.ps.qual =
163  ExecInitQual(node->scan.plan.qual, &scanstate->ss.ps);
164 
165  /* Only XMLTABLE is supported currently */
166  scanstate->routine = &XmlTableRoutine;
167 
168  scanstate->perTableCxt =
170  "TableFunc per value context",
172  scanstate->opaque = NULL; /* initialized at runtime */
173 
174  scanstate->ns_names = tf->ns_names;
175 
176  scanstate->ns_uris =
177  ExecInitExprList(tf->ns_uris, (PlanState *) scanstate);
178  scanstate->docexpr =
179  ExecInitExpr((Expr *) tf->docexpr, (PlanState *) scanstate);
180  scanstate->rowexpr =
181  ExecInitExpr((Expr *) tf->rowexpr, (PlanState *) scanstate);
182  scanstate->colexprs =
183  ExecInitExprList(tf->colexprs, (PlanState *) scanstate);
184  scanstate->coldefexprs =
185  ExecInitExprList(tf->coldefexprs, (PlanState *) scanstate);
186 
187  scanstate->notnulls = tf->notnulls;
188 
189  /* these are allocated now and initialized later */
190  scanstate->in_functions = palloc(sizeof(FmgrInfo) * tupdesc->natts);
191  scanstate->typioparams = palloc(sizeof(Oid) * tupdesc->natts);
192 
193  /*
194  * Fill in the necessary fmgr infos.
195  */
196  for (i = 0; i < tupdesc->natts; i++)
197  {
198  Oid in_funcid;
199 
201  &in_funcid, &scanstate->typioparams[i]);
202  fmgr_info(in_funcid, &scanstate->in_functions[i]);
203  }
204 
205  return scanstate;
206 }
Definition: fmgr.h:56
List * qual
Definition: plannodes.h:141
Plan plan
Definition: plannodes.h:340
#define AllocSetContextCreate
Definition: memutils.h:169
Node * docexpr
Definition: primnodes.h:87
TupleDesc BuildDescFromLists(List *names, List *types, List *typmods, List *collations)
Definition: tupdesc.c:894
List * colnames
Definition: primnodes.h:89
List * coltypmods
Definition: primnodes.h:91
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
TableFunc * tablefunc
Definition: plannodes.h:542
EState * state
Definition: execnodes.h:947
unsigned int Oid
Definition: postgres_ext.h:31
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:207
List * colcollations
Definition: primnodes.h:92
PlanState ps
Definition: execnodes.h:1280
const TableFuncRoutine XmlTableRoutine
Definition: xml.c:207
Bitmapset * notnulls
Definition: execnodes.h:1661
void ExecAssignScanProjectionInfo(ScanState *node)
Definition: execScan.c:233
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:124
List * coldefexprs
Definition: primnodes.h:94
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:191
List * ns_names
Definition: primnodes.h:86
#define innerPlan(node)
Definition: plannodes.h:169
Node * rowexpr
Definition: primnodes.h:88
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1776
const struct TableFuncRoutine * routine
Definition: execnodes.h:1663
void ExecInitResultTypeTL(PlanState *planstate)
Definition: execTuples.c:1720
ExprState * docexpr
Definition: execnodes.h:1655
Bitmapset * notnulls
Definition: primnodes.h:95
List * ExecInitExprList(List *nodes, PlanState *parent)
Definition: execExpr.c:316
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
Oid atttypid
Definition: pg_attribute.h:49
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2641
#define outerPlan(node)
Definition: plannodes.h:170
MemoryContext perTableCxt
Definition: execnodes.h:1667
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:951
List * coltypes
Definition: primnodes.h:90
Plan * plan
Definition: execnodes.h:945
List * colexprs
Definition: primnodes.h:93
#define makeNode(_type_)
Definition: nodes.h:572
#define Assert(condition)
Definition: c.h:732
#define EXEC_FLAG_MARK
Definition: executor.h:59
List * ns_uris
Definition: primnodes.h:85
FmgrInfo * in_functions
Definition: execnodes.h:1664
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:446
ExprState * qual
Definition: execnodes.h:966
static TupleTableSlot * ExecTableFuncScan(PlanState *pstate)
void * palloc(Size size)
Definition: mcxt.c:924
int i
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execExpr.c:121
ExprState * rowexpr
Definition: execnodes.h:1656
const TupleTableSlotOps TTSOpsMinimalTuple
Definition: execTuples.c:86

◆ ExecReScanTableFuncScan()

void ExecReScanTableFuncScan ( TableFuncScanState node)

Definition at line 244 of file nodeTableFuncscan.c.

References PlanState::chgParam, ExecClearTuple(), ExecScanReScan(), ScanState::ps, PlanState::ps_ResultTupleSlot, TableFuncScanState::ss, tuplestore_end(), tuplestore_rescan(), and TableFuncScanState::tupstore.

Referenced by ExecReScan().

245 {
246  Bitmapset *chgparam = node->ss.ps.chgParam;
247 
248  if (node->ss.ps.ps_ResultTupleSlot)
250  ExecScanReScan(&node->ss);
251 
252  /*
253  * Recompute when parameters are changed.
254  */
255  if (chgparam)
256  {
257  if (node->tupstore != NULL)
258  {
259  tuplestore_end(node->tupstore);
260  node->tupstore = NULL;
261  }
262  }
263 
264  if (node->tupstore != NULL)
266 }
void tuplestore_rescan(Tuplestorestate *state)
Definition: tuplestore.c:1233
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:426
PlanState ps
Definition: execnodes.h:1280
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:983
Bitmapset * chgParam
Definition: execnodes.h:977
void tuplestore_end(Tuplestorestate *state)
Definition: tuplestore.c:453
void ExecScanReScan(ScanState *node)
Definition: execScan.c:260
Tuplestorestate * tupstore
Definition: execnodes.h:1668

◆ ExecTableFuncScan()

static TupleTableSlot* ExecTableFuncScan ( PlanState pstate)
static

Definition at line 97 of file nodeTableFuncscan.c.

References castNode, ExecScan(), TableFuncScanState::ss, TableFuncNext(), and TableFuncRecheck().

Referenced by ExecInitTableFuncScan().

98 {
100 
101  return ExecScan(&node->ss,
104 }
#define castNode(_type_, nodeptr)
Definition: nodes.h:593
TupleTableSlot * ExecScan(ScanState *node, ExecScanAccessMtd accessMtd, ExecScanRecheckMtd recheckMtd)
Definition: execScan.c:119
static TupleTableSlot * TableFuncNext(TableFuncScanState *node)
static bool TableFuncRecheck(TableFuncScanState *node, TupleTableSlot *slot)
bool(* ExecScanRecheckMtd)(ScanState *node, TupleTableSlot *slot)
Definition: executor.h:426
TupleTableSlot *(* ExecScanAccessMtd)(ScanState *node)
Definition: executor.h:425

◆ TableFuncNext()

static TupleTableSlot * TableFuncNext ( TableFuncScanState node)
static

Definition at line 54 of file nodeTableFuncscan.c.

References ScanState::ps, PlanState::ps_ExprContext, TableFuncScanState::ss, ScanState::ss_ScanTupleSlot, tfuncFetchRows(), tuplestore_gettupleslot(), and TableFuncScanState::tupstore.

Referenced by ExecTableFuncScan().

55 {
56  TupleTableSlot *scanslot;
57 
58  scanslot = node->ss.ss_ScanTupleSlot;
59 
60  /*
61  * If first time through, read all tuples from function and put them in a
62  * tuplestore. Subsequent calls just fetch tuples from tuplestore.
63  */
64  if (node->tupstore == NULL)
65  tfuncFetchRows(node, node->ss.ps.ps_ExprContext);
66 
67  /*
68  * Get the next tuple from tuplestore.
69  */
70  (void) tuplestore_gettupleslot(node->tupstore,
71  true,
72  false,
73  scanslot);
74  return scanslot;
75 }
ExprContext * ps_ExprContext
Definition: execnodes.h:984
static void tfuncFetchRows(TableFuncScanState *tstate, ExprContext *econtext)
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1283
PlanState ps
Definition: execnodes.h:1280
bool tuplestore_gettupleslot(Tuplestorestate *state, bool forward, bool copy, TupleTableSlot *slot)
Definition: tuplestore.c:1078
Tuplestorestate * tupstore
Definition: execnodes.h:1668

◆ TableFuncRecheck()

static bool TableFuncRecheck ( TableFuncScanState node,
TupleTableSlot slot 
)
static

Definition at line 81 of file nodeTableFuncscan.c.

Referenced by ExecTableFuncScan().

82 {
83  /* nothing to check */
84  return true;
85 }

◆ tfuncFetchRows()

static void tfuncFetchRows ( TableFuncScanState tstate,
ExprContext econtext 
)
static

Definition at line 275 of file nodeTableFuncscan.c.

References Assert, TableFuncRoutine::DestroyOpaque, TableFuncScanState::docexpr, ExprContext::ecxt_per_query_memory, ExecEvalExpr(), TableFuncRoutine::InitOpaque, MemoryContextReset(), MemoryContextSwitchTo(), TupleDescData::natts, TableFuncScanState::opaque, TableFuncScanState::ordinal, TableFuncScanState::perTableCxt, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, TableFuncScanState::routine, TableFuncScanState::ss, ScanState::ss_ScanTupleSlot, tfuncInitialize(), tfuncLoadRows(), TupleTableSlot::tts_tupleDescriptor, tuplestore_begin_heap(), TableFuncScanState::tupstore, value, and work_mem.

Referenced by TableFuncNext().

276 {
277  const TableFuncRoutine *routine = tstate->routine;
278  MemoryContext oldcxt;
279  Datum value;
280  bool isnull;
281 
282  Assert(tstate->opaque == NULL);
283 
284  /* build tuplestore for the result */
285  oldcxt = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
286  tstate->tupstore = tuplestore_begin_heap(false, false, work_mem);
287 
288  /*
289  * Each call to fetch a new set of rows - of which there may be very many
290  * if XMLTABLE is being used in a lateral join - will allocate a possibly
291  * substantial amount of memory, so we cannot use the per-query context
292  * here. perTableCxt now serves the same function as "argcontext" does in
293  * FunctionScan - a place to store per-one-call (i.e. one result table)
294  * lifetime data (as opposed to per-query or per-result-tuple).
295  */
297 
298  PG_TRY();
299  {
300  routine->InitOpaque(tstate,
302 
303  /*
304  * If evaluating the document expression returns NULL, the table
305  * expression is empty and we return immediately.
306  */
307  value = ExecEvalExpr(tstate->docexpr, econtext, &isnull);
308 
309  if (!isnull)
310  {
311  /* otherwise, pass the document value to the table builder */
312  tfuncInitialize(tstate, econtext, value);
313 
314  /* initialize ordinality counter */
315  tstate->ordinal = 1;
316 
317  /* Load all rows into the tuplestore, and we're done */
318  tfuncLoadRows(tstate, econtext);
319  }
320  }
321  PG_CATCH();
322  {
323  if (tstate->opaque != NULL)
324  routine->DestroyOpaque(tstate);
325  PG_RE_THROW();
326  }
327  PG_END_TRY();
328 
329  /* clean up and return to original memory context */
330 
331  if (tstate->opaque != NULL)
332  {
333  routine->DestroyOpaque(tstate);
334  tstate->opaque = NULL;
335  }
336 
337  MemoryContextSwitchTo(oldcxt);
339 
340  return;
341 }
void(* InitOpaque)(struct TableFuncScanState *state, int natts)
Definition: tablefunc.h:54
static struct @144 value
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void(* DestroyOpaque)(struct TableFuncScanState *state)
Definition: tablefunc.h:64
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1283
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
static void tfuncLoadRows(TableFuncScanState *tstate, ExprContext *econtext)
static void tfuncInitialize(TableFuncScanState *tstate, ExprContext *econtext, Datum doc)
const struct TableFuncRoutine * routine
Definition: execnodes.h:1663
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:285
ExprState * docexpr
Definition: execnodes.h:1655
MemoryContext perTableCxt
Definition: execnodes.h:1667
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:124
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
uintptr_t Datum
Definition: postgres.h:367
int work_mem
Definition: globals.c:121
#define PG_CATCH()
Definition: elog.h:310
#define Assert(condition)
Definition: c.h:732
#define PG_RE_THROW()
Definition: elog.h:331
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:231
#define PG_TRY()
Definition: elog.h:301
Tuplestorestate * tupstore
Definition: execnodes.h:1668
#define PG_END_TRY()
Definition: elog.h:317

◆ tfuncInitialize()

static void tfuncInitialize ( TableFuncScanState tstate,
ExprContext econtext,
Datum  doc 
)
static

Definition at line 348 of file nodeTableFuncscan.c.

References TableFuncScanState::colexprs, ereport, errcode(), errdetail(), errmsg(), ERROR, ExecEvalExpr(), forboth, lfirst, NameStr, TableFuncScanState::ns_names, TableFuncScanState::ns_uris, PlanState::plan, ScanState::ps, TableFuncScanState::routine, TableFuncScanState::rowexpr, TableFuncRoutine::SetColumnFilter, TableFuncRoutine::SetDocument, TableFuncRoutine::SetNamespace, TableFuncRoutine::SetRowFilter, TableFuncScanState::ss, ScanState::ss_ScanTupleSlot, strVal, TextDatumGetCString, TupleTableSlot::tts_tupleDescriptor, TupleDescAttr, and value.

Referenced by tfuncFetchRows().

349 {
350  const TableFuncRoutine *routine = tstate->routine;
351  TupleDesc tupdesc;
352  ListCell *lc1,
353  *lc2;
354  bool isnull;
355  int colno;
356  Datum value;
357  int ordinalitycol =
358  ((TableFuncScan *) (tstate->ss.ps.plan))->tablefunc->ordinalitycol;
359 
360  /*
361  * Install the document as a possibly-toasted Datum into the tablefunc
362  * context.
363  */
364  routine->SetDocument(tstate, doc);
365 
366  /* Evaluate namespace specifications */
367  forboth(lc1, tstate->ns_uris, lc2, tstate->ns_names)
368  {
369  ExprState *expr = (ExprState *) lfirst(lc1);
370  Value *ns_node = (Value *) lfirst(lc2);
371  char *ns_uri;
372  char *ns_name;
373 
374  value = ExecEvalExpr((ExprState *) expr, econtext, &isnull);
375  if (isnull)
376  ereport(ERROR,
377  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
378  errmsg("namespace URI must not be null")));
379  ns_uri = TextDatumGetCString(value);
380 
381  /* DEFAULT is passed down to SetNamespace as NULL */
382  ns_name = ns_node ? strVal(ns_node) : NULL;
383 
384  routine->SetNamespace(tstate, ns_name, ns_uri);
385  }
386 
387  /* Install the row filter expression into the table builder context */
388  value = ExecEvalExpr(tstate->rowexpr, econtext, &isnull);
389  if (isnull)
390  ereport(ERROR,
391  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
392  errmsg("row filter expression must not be null")));
393 
394  routine->SetRowFilter(tstate, TextDatumGetCString(value));
395 
396  /*
397  * Install the column filter expressions into the table builder context.
398  * If an expression is given, use that; otherwise the column name itself
399  * is the column filter.
400  */
401  colno = 0;
402  tupdesc = tstate->ss.ss_ScanTupleSlot->tts_tupleDescriptor;
403  foreach(lc1, tstate->colexprs)
404  {
405  char *colfilter;
406  Form_pg_attribute att = TupleDescAttr(tupdesc, colno);
407 
408  if (colno != ordinalitycol)
409  {
410  ExprState *colexpr = lfirst(lc1);
411 
412  if (colexpr != NULL)
413  {
414  value = ExecEvalExpr(colexpr, econtext, &isnull);
415  if (isnull)
416  ereport(ERROR,
417  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
418  errmsg("column filter expression must not be null"),
419  errdetail("Filter for column \"%s\" is null.",
420  NameStr(att->attname))));
421  colfilter = TextDatumGetCString(value);
422  }
423  else
424  colfilter = NameStr(att->attname);
425 
426  routine->SetColumnFilter(tstate, colfilter, colno);
427  }
428 
429  colno++;
430  }
431 }
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:419
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
void(* SetColumnFilter)(struct TableFuncScanState *state, const char *path, int colnum)
Definition: tablefunc.h:59
static struct @144 value
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:570
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1283
void(* SetDocument)(struct TableFuncScanState *state, Datum value)
Definition: tablefunc.h:55
PlanState ps
Definition: execnodes.h:1280
void(* SetNamespace)(struct TableFuncScanState *state, const char *name, const char *uri)
Definition: tablefunc.h:56
#define ERROR
Definition: elog.h:43
const struct TableFuncRoutine * routine
Definition: execnodes.h:1663
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:285
int errdetail(const char *fmt,...)
Definition: elog.c:860
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
#define ereport(elevel, rest)
Definition: elog.h:141
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:124
#define TextDatumGetCString(d)
Definition: builtins.h:84
uintptr_t Datum
Definition: postgres.h:367
Plan * plan
Definition: execnodes.h:945
#define lfirst(lc)
Definition: pg_list.h:190
Definition: value.h:42
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define NameStr(name)
Definition: c.h:609
void(* SetRowFilter)(struct TableFuncScanState *state, const char *path)
Definition: tablefunc.h:58
ExprState * rowexpr
Definition: execnodes.h:1656

◆ tfuncLoadRows()

static void tfuncLoadRows ( TableFuncScanState tstate,
ExprContext econtext 
)
static

Definition at line 437 of file nodeTableFuncscan.c.

References bms_is_member(), CHECK_FOR_INTERRUPTS, TableFuncScanState::coldefexprs, ExprContext::ecxt_per_tuple_memory, ereport, errcode(), errmsg(), ERROR, ExecClearTuple(), ExecEvalExpr(), TableFuncRoutine::FetchRow, TableFuncRoutine::GetValue, Int32GetDatum, lfirst, list_head(), lnext(), MemoryContextReset(), MemoryContextSwitchTo(), NameStr, TupleDescData::natts, TableFuncScanState::notnulls, TableFuncScanState::ordinal, PlanState::plan, ScanState::ps, TableFuncScanState::routine, TableFuncScanState::ss, ScanState::ss_ScanTupleSlot, TupleTableSlot::tts_isnull, TupleTableSlot::tts_tupleDescriptor, TupleTableSlot::tts_values, TupleDescAttr, tuplestore_putvalues(), TableFuncScanState::tupstore, and values.

Referenced by tfuncFetchRows().

438 {
439  const TableFuncRoutine *routine = tstate->routine;
440  TupleTableSlot *slot = tstate->ss.ss_ScanTupleSlot;
441  TupleDesc tupdesc = slot->tts_tupleDescriptor;
442  Datum *values = slot->tts_values;
443  bool *nulls = slot->tts_isnull;
444  int natts = tupdesc->natts;
445  MemoryContext oldcxt;
446  int ordinalitycol;
447 
448  ordinalitycol =
449  ((TableFuncScan *) (tstate->ss.ps.plan))->tablefunc->ordinalitycol;
450 
451  /*
452  * We need a short-lived memory context that we can clean up each time
453  * around the loop, to avoid wasting space. Our default per-tuple context
454  * is fine for the job, since we won't have used it for anything yet in
455  * this tuple cycle.
456  */
457  oldcxt = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
458 
459  /*
460  * Keep requesting rows from the table builder until there aren't any.
461  */
462  while (routine->FetchRow(tstate))
463  {
464  ListCell *cell = list_head(tstate->coldefexprs);
465  int colno;
466 
468 
470 
471  /*
472  * Obtain the value of each column for this row, installing them into
473  * the slot; then add the tuple to the tuplestore.
474  */
475  for (colno = 0; colno < natts; colno++)
476  {
477  Form_pg_attribute att = TupleDescAttr(tupdesc, colno);
478 
479  if (colno == ordinalitycol)
480  {
481  /* Fast path for ordinality column */
482  values[colno] = Int32GetDatum(tstate->ordinal++);
483  nulls[colno] = false;
484  }
485  else
486  {
487  bool isnull;
488 
489  values[colno] = routine->GetValue(tstate,
490  colno,
491  att->atttypid,
492  att->atttypmod,
493  &isnull);
494 
495  /* No value? Evaluate and apply the default, if any */
496  if (isnull && cell != NULL)
497  {
498  ExprState *coldefexpr = (ExprState *) lfirst(cell);
499 
500  if (coldefexpr != NULL)
501  values[colno] = ExecEvalExpr(coldefexpr, econtext,
502  &isnull);
503  }
504 
505  /* Verify a possible NOT NULL constraint */
506  if (isnull && bms_is_member(colno, tstate->notnulls))
507  ereport(ERROR,
508  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
509  errmsg("null is not allowed in column \"%s\"",
510  NameStr(att->attname))));
511 
512  nulls[colno] = isnull;
513  }
514 
515  /* advance list of default expressions */
516  if (cell != NULL)
517  cell = lnext(tstate->coldefexprs, cell);
518  }
519 
520  tuplestore_putvalues(tstate->tupstore, tupdesc, values, nulls);
521 
523  }
524 
525  MemoryContextSwitchTo(oldcxt);
526 }
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:426
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:321
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:232
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:570
Datum * tts_values
Definition: tuptable.h:126
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1283
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
PlanState ps
Definition: execnodes.h:1280
#define ERROR
Definition: elog.h:43
Bitmapset * notnulls
Definition: execnodes.h:1661
const struct TableFuncRoutine * routine
Definition: execnodes.h:1663
bool * tts_isnull
Definition: tuptable.h:128
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:285
static ListCell * list_head(const List *l)
Definition: pg_list.h:125
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
#define ereport(elevel, rest)
Definition: elog.h:141
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:124
uintptr_t Datum
Definition: postgres.h:367
Plan * plan
Definition: execnodes.h:945
#define lfirst(lc)
Definition: pg_list.h:190
Datum(* GetValue)(struct TableFuncScanState *state, int colnum, Oid typid, int32 typmod, bool *isnull)
Definition: tablefunc.h:62
bool(* FetchRow)(struct TableFuncScanState *state)
Definition: tablefunc.h:61
static Datum values[MAXATTR]
Definition: bootstrap.c:167
#define Int32GetDatum(X)
Definition: postgres.h:479
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define NameStr(name)
Definition: c.h:609
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
Tuplestorestate * tupstore
Definition: execnodes.h:1668
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427