PostgreSQL Source Code  git master
nodeValuesscan.c File Reference
Include dependency graph for nodeValuesscan.c:

Go to the source code of this file.

Functions

static TupleTableSlotValuesNext (ValuesScanState *node)
 
static bool ValuesRecheck (ValuesScanState *node, TupleTableSlot *slot)
 
static TupleTableSlotExecValuesScan (PlanState *pstate)
 
ValuesScanStateExecInitValuesScan (ValuesScan *node, EState *estate, int eflags)
 
void ExecEndValuesScan (ValuesScanState *node)
 
void ExecReScanValuesScan (ValuesScanState *node)
 

Function Documentation

◆ ExecEndValuesScan()

void ExecEndValuesScan ( ValuesScanState node)

Definition at line 293 of file nodeValuesscan.c.

References ExecClearTuple(), ExecFreeExprContext(), ScanState::ps, PlanState::ps_ExprContext, PlanState::ps_ResultTupleSlot, ValuesScanState::rowcontext, ValuesScanState::ss, and ScanState::ss_ScanTupleSlot.

Referenced by ExecEndNode().

294 {
295  /*
296  * Free both exprcontexts
297  */
298  ExecFreeExprContext(&node->ss.ps);
299  node->ss.ps.ps_ExprContext = node->rowcontext;
300  ExecFreeExprContext(&node->ss.ps);
301 
302  /*
303  * clean out the tuple table
304  */
307 }
ExprContext * ps_ExprContext
Definition: execnodes.h:902
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:475
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1127
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:561
ScanState ss
Definition: execnodes.h:1480
PlanState ps
Definition: execnodes.h:1124
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:901
ExprContext * rowcontext
Definition: execnodes.h:1481

◆ ExecInitValuesScan()

ValuesScanState* ExecInitValuesScan ( ValuesScan node,
EState estate,
int  eflags 
)

Definition at line 214 of file nodeValuesscan.c.

References Assert, ExecAssignExprContext(), ExecAssignScanProjectionInfo(), ExecInitQual(), ExecInitResultTupleSlotTL(), ExecInitScanTupleSlot(), PlanState::ExecProcNode, ExecTypeFromExprList(), ExecValuesScan(), i, innerPlan, lfirst, linitial, list_length(), makeNode, outerPlan, palloc(), Scan::plan, PlanState::plan, ScanState::ps, PlanState::ps_ExprContext, Plan::qual, PlanState::qual, ValuesScanState::rowcontext, ValuesScan::scan, ValuesScanState::ss, PlanState::state, and ValuesScan::values_lists.

Referenced by ExecInitNode().

215 {
216  ValuesScanState *scanstate;
217  TupleDesc tupdesc;
218  ListCell *vtl;
219  int i;
220  PlanState *planstate;
221 
222  /*
223  * ValuesScan should not have any children.
224  */
225  Assert(outerPlan(node) == NULL);
226  Assert(innerPlan(node) == NULL);
227 
228  /*
229  * create new ScanState for node
230  */
231  scanstate = makeNode(ValuesScanState);
232  scanstate->ss.ps.plan = (Plan *) node;
233  scanstate->ss.ps.state = estate;
234  scanstate->ss.ps.ExecProcNode = ExecValuesScan;
235 
236  /*
237  * Miscellaneous initialization
238  */
239  planstate = &scanstate->ss.ps;
240 
241  /*
242  * Create expression contexts. We need two, one for per-sublist
243  * processing and one for execScan.c to use for quals and projections. We
244  * cheat a little by using ExecAssignExprContext() to build both.
245  */
246  ExecAssignExprContext(estate, planstate);
247  scanstate->rowcontext = planstate->ps_ExprContext;
248  ExecAssignExprContext(estate, planstate);
249 
250  /*
251  * Get info about values list, initialize scan slot with it.
252  */
253  tupdesc = ExecTypeFromExprList((List *) linitial(node->values_lists));
254  ExecInitScanTupleSlot(estate, &scanstate->ss, tupdesc);
255 
256  /*
257  * Initialize result slot, type and projection.
258  */
259  ExecInitResultTupleSlotTL(estate, &scanstate->ss.ps);
260  ExecAssignScanProjectionInfo(&scanstate->ss);
261 
262  /*
263  * initialize child expressions
264  */
265  scanstate->ss.ps.qual =
266  ExecInitQual(node->scan.plan.qual, (PlanState *) scanstate);
267 
268  /*
269  * Other node-specific setup
270  */
271  scanstate->curr_idx = -1;
272  scanstate->array_len = list_length(node->values_lists);
273 
274  /* convert list of sublists into array of sublists for easy addressing */
275  scanstate->exprlists = (List **)
276  palloc(scanstate->array_len * sizeof(List *));
277  i = 0;
278  foreach(vtl, node->values_lists)
279  {
280  scanstate->exprlists[i++] = (List *) lfirst(vtl);
281  }
282 
283  return scanstate;
284 }
List * qual
Definition: plannodes.h:145
Plan plan
Definition: plannodes.h:330
static TupleTableSlot * ExecValuesScan(PlanState *pstate)
ExprContext * ps_ExprContext
Definition: execnodes.h:902
List * values_lists
Definition: plannodes.h:521
EState * state
Definition: execnodes.h:870
Scan scan
Definition: plannodes.h:520
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:204
ScanState ss
Definition: execnodes.h:1480
PlanState ps
Definition: execnodes.h:1124
#define linitial(l)
Definition: pg_list.h:111
void ExecAssignScanProjectionInfo(ScanState *node)
Definition: execScan.c:235
#define innerPlan(node)
Definition: plannodes.h:173
#define outerPlan(node)
Definition: plannodes.h:174
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:874
ExprContext * rowcontext
Definition: execnodes.h:1481
void ExecInitResultTupleSlotTL(EState *estate, PlanState *planstate)
Definition: execTuples.c:870
Plan * plan
Definition: execnodes.h:868
#define makeNode(_type_)
Definition: nodes.h:561
#define Assert(condition)
Definition: c.h:688
#define lfirst(lc)
Definition: pg_list.h:106
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:425
static int list_length(const List *l)
Definition: pg_list.h:89
ExprState * qual
Definition: execnodes.h:886
TupleDesc ExecTypeFromExprList(List *exprList)
Definition: execTuples.c:1003
void * palloc(Size size)
Definition: mcxt.c:835
int i
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate, TupleDesc tupledesc)
Definition: execTuples.c:895
List ** exprlists
Definition: execnodes.h:1482
Definition: pg_list.h:45

◆ ExecReScanValuesScan()

void ExecReScanValuesScan ( ValuesScanState node)

Definition at line 316 of file nodeValuesscan.c.

References ValuesScanState::curr_idx, ExecClearTuple(), ExecScanReScan(), ScanState::ps, PlanState::ps_ResultTupleSlot, and ValuesScanState::ss.

Referenced by ExecReScan().

317 {
319 
320  ExecScanReScan(&node->ss);
321 
322  node->curr_idx = -1;
323 }
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:475
ScanState ss
Definition: execnodes.h:1480
PlanState ps
Definition: execnodes.h:1124
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:901
void ExecScanReScan(ScanState *node)
Definition: execScan.c:262

◆ ExecValuesScan()

static TupleTableSlot* ExecValuesScan ( PlanState pstate)
static

Definition at line 200 of file nodeValuesscan.c.

References castNode, ExecScan(), ValuesScanState::ss, ValuesNext(), and ValuesRecheck().

Referenced by ExecInitValuesScan().

201 {
202  ValuesScanState *node = castNode(ValuesScanState, pstate);
203 
204  return ExecScan(&node->ss,
207 }
#define castNode(_type_, nodeptr)
Definition: nodes.h:582
TupleTableSlot * ExecScan(ScanState *node, ExecScanAccessMtd accessMtd, ExecScanRecheckMtd recheckMtd)
Definition: execScan.c:121
ScanState ss
Definition: execnodes.h:1480
static TupleTableSlot * ValuesNext(ValuesScanState *node)
static bool ValuesRecheck(ValuesScanState *node, TupleTableSlot *slot)
bool(* ExecScanRecheckMtd)(ScanState *node, TupleTableSlot *slot)
Definition: executor.h:423
TupleTableSlot *(* ExecScanAccessMtd)(ScanState *node)
Definition: executor.h:422

◆ ValuesNext()

static TupleTableSlot * ValuesNext ( ValuesScanState node)
static

Definition at line 46 of file nodeValuesscan.c.

References ValuesScanState::array_len, Assert, ValuesScanState::curr_idx, ExprContext::ecxt_per_tuple_memory, EState::es_direction, ExecClearTuple(), ExecEvalExpr(), ExecInitExprList(), ExecStoreVirtualTuple(), ValuesScanState::exprlists, lfirst, list_length(), MakeExpandedObjectReadOnly, MemoryContextSwitchTo(), tupleDesc::natts, NIL, ScanState::ps, ReScanExprContext(), ValuesScanState::rowcontext, ScanDirectionIsForward, ValuesScanState::ss, ScanState::ss_ScanTupleSlot, PlanState::state, PlanState::subPlan, TupleTableSlot::tts_isnull, TupleTableSlot::tts_tupleDescriptor, TupleTableSlot::tts_values, TupleDescAttr, and values.

Referenced by ExecValuesScan().

47 {
48  TupleTableSlot *slot;
49  EState *estate;
50  ExprContext *econtext;
51  ScanDirection direction;
52  List *exprlist;
53 
54  /*
55  * get information from the estate and scan state
56  */
57  estate = node->ss.ps.state;
58  direction = estate->es_direction;
59  slot = node->ss.ss_ScanTupleSlot;
60  econtext = node->rowcontext;
61 
62  /*
63  * Get the next tuple. Return NULL if no more tuples.
64  */
65  if (ScanDirectionIsForward(direction))
66  {
67  if (node->curr_idx < node->array_len)
68  node->curr_idx++;
69  if (node->curr_idx < node->array_len)
70  exprlist = node->exprlists[node->curr_idx];
71  else
72  exprlist = NIL;
73  }
74  else
75  {
76  if (node->curr_idx >= 0)
77  node->curr_idx--;
78  if (node->curr_idx >= 0)
79  exprlist = node->exprlists[node->curr_idx];
80  else
81  exprlist = NIL;
82  }
83 
84  /*
85  * Always clear the result slot; this is appropriate if we are at the end
86  * of the data, and if we're not, we still need it as the first step of
87  * the store-virtual-tuple protocol. It seems wise to clear the slot
88  * before we reset the context it might have pointers into.
89  */
90  ExecClearTuple(slot);
91 
92  if (exprlist)
93  {
94  MemoryContext oldContext;
95  List *oldsubplans;
96  List *exprstatelist;
97  Datum *values;
98  bool *isnull;
99  ListCell *lc;
100  int resind;
101 
102  /*
103  * Get rid of any prior cycle's leftovers. We use ReScanExprContext
104  * not just ResetExprContext because we want any registered shutdown
105  * callbacks to be called.
106  */
107  ReScanExprContext(econtext);
108 
109  /*
110  * Build the expression eval state in the econtext's per-tuple memory.
111  * This is a tad unusual, but we want to delete the eval state again
112  * when we move to the next row, to avoid growth of memory
113  * requirements over a long values list.
114  */
115  oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
116 
117  /*
118  * The expressions might contain SubPlans (this is currently only
119  * possible if there's a sub-select containing a LATERAL reference,
120  * otherwise sub-selects in a VALUES list should be InitPlans). Those
121  * subplans will want to hook themselves into our subPlan list, which
122  * would result in a corrupted list after we delete the eval state. We
123  * can work around this by saving and restoring the subPlan list.
124  * (There's no need for the functionality that would be enabled by
125  * having the list entries, since the SubPlans aren't going to be
126  * re-executed anyway.)
127  */
128  oldsubplans = node->ss.ps.subPlan;
129  node->ss.ps.subPlan = NIL;
130 
131  exprstatelist = ExecInitExprList(exprlist, &node->ss.ps);
132 
133  node->ss.ps.subPlan = oldsubplans;
134 
135  /* parser should have checked all sublists are the same length */
136  Assert(list_length(exprstatelist) == slot->tts_tupleDescriptor->natts);
137 
138  /*
139  * Compute the expressions and build a virtual result tuple. We
140  * already did ExecClearTuple(slot).
141  */
142  values = slot->tts_values;
143  isnull = slot->tts_isnull;
144 
145  resind = 0;
146  foreach(lc, exprstatelist)
147  {
148  ExprState *estate = (ExprState *) lfirst(lc);
150  resind);
151 
152  values[resind] = ExecEvalExpr(estate,
153  econtext,
154  &isnull[resind]);
155 
156  /*
157  * We must force any R/W expanded datums to read-only state, in
158  * case they are multiply referenced in the plan node's output
159  * expressions, or in case we skip the output projection and the
160  * output column is multiply referenced in higher plan nodes.
161  */
162  values[resind] = MakeExpandedObjectReadOnly(values[resind],
163  isnull[resind],
164  attr->attlen);
165 
166  resind++;
167  }
168 
169  MemoryContextSwitchTo(oldContext);
170 
171  /*
172  * And return the virtual tuple.
173  */
174  ExecStoreVirtualTuple(slot);
175  }
176 
177  return slot;
178 }
#define NIL
Definition: pg_list.h:69
#define ScanDirectionIsForward(direction)
Definition: sdir.h:55
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:90
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:217
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:475
List * subPlan
Definition: execnodes.h:891
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Datum * tts_values
Definition: tuptable.h:125
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1127
EState * state
Definition: execnodes.h:870
int natts
Definition: tupdesc.h:79
ScanDirection es_direction
Definition: execnodes.h:442
ScanState ss
Definition: execnodes.h:1480
PlanState ps
Definition: execnodes.h:1124
bool * tts_isnull
Definition: tuptable.h:126
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:282
ScanDirection
Definition: sdir.h:22
List * ExecInitExprList(List *nodes, PlanState *parent)
Definition: execExpr.c:313
#define MakeExpandedObjectReadOnly(d, isnull, typlen)
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:121
uintptr_t Datum
Definition: postgres.h:365
ExprContext * rowcontext
Definition: execnodes.h:1481
#define Assert(condition)
Definition: c.h:688
#define lfirst(lc)
Definition: pg_list.h:106
static int list_length(const List *l)
Definition: pg_list.h:89
void ReScanExprContext(ExprContext *econtext)
Definition: execUtils.c:383
static Datum values[MAXATTR]
Definition: bootstrap.c:164
List ** exprlists
Definition: execnodes.h:1482
Definition: pg_list.h:45
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:524

◆ ValuesRecheck()

static bool ValuesRecheck ( ValuesScanState node,
TupleTableSlot slot 
)
static

Definition at line 184 of file nodeValuesscan.c.

Referenced by ExecValuesScan().

185 {
186  /* nothing to check */
187  return true;
188 }