PostgreSQL Source Code  git master
execScan.c File Reference
#include "postgres.h"
#include "executor/executor.h"
#include "miscadmin.h"
#include "utils/memutils.h"
Include dependency graph for execScan.c:

Go to the source code of this file.

Functions

static TupleTableSlotExecScanFetch (ScanState *node, ExecScanAccessMtd accessMtd, ExecScanRecheckMtd recheckMtd)
 
TupleTableSlotExecScan (ScanState *node, ExecScanAccessMtd accessMtd, ExecScanRecheckMtd recheckMtd)
 
void ExecAssignScanProjectionInfo (ScanState *node)
 
void ExecAssignScanProjectionInfoWithVarno (ScanState *node, int varno)
 
void ExecScanReScan (ScanState *node)
 

Function Documentation

◆ ExecAssignScanProjectionInfo()

void ExecAssignScanProjectionInfo ( ScanState node)

◆ ExecAssignScanProjectionInfoWithVarno()

void ExecAssignScanProjectionInfoWithVarno ( ScanState node,
int  varno 
)

◆ ExecScan()

TupleTableSlot* ExecScan ( ScanState node,
ExecScanAccessMtd  accessMtd,
ExecScanRecheckMtd  recheckMtd 
)

Definition at line 157 of file execScan.c.

160 {
161  ExprContext *econtext;
162  ExprState *qual;
163  ProjectionInfo *projInfo;
164 
165  /*
166  * Fetch data from node
167  */
168  qual = node->ps.qual;
169  projInfo = node->ps.ps_ProjInfo;
170  econtext = node->ps.ps_ExprContext;
171 
172  /* interrupt checks are in ExecScanFetch */
173 
174  /*
175  * If we have neither a qual to check nor a projection to do, just skip
176  * all the overhead and return the raw scan tuple.
177  */
178  if (!qual && !projInfo)
179  {
180  ResetExprContext(econtext);
181  return ExecScanFetch(node, accessMtd, recheckMtd);
182  }
183 
184  /*
185  * Reset per-tuple memory context to free any expression evaluation
186  * storage allocated in the previous tuple cycle.
187  */
188  ResetExprContext(econtext);
189 
190  /*
191  * get a tuple from the access method. Loop until we obtain a tuple that
192  * passes the qualification.
193  */
194  for (;;)
195  {
196  TupleTableSlot *slot;
197 
198  slot = ExecScanFetch(node, accessMtd, recheckMtd);
199 
200  /*
201  * if the slot returned by the accessMtd contains NULL, then it means
202  * there is nothing more to scan so we just return an empty slot,
203  * being careful to use the projection result slot so it has correct
204  * tupleDesc.
205  */
206  if (TupIsNull(slot))
207  {
208  if (projInfo)
209  return ExecClearTuple(projInfo->pi_state.resultslot);
210  else
211  return slot;
212  }
213 
214  /*
215  * place the current tuple into the expr context
216  */
217  econtext->ecxt_scantuple = slot;
218 
219  /*
220  * check that the current tuple satisfies the qual-clause
221  *
222  * check for non-null qual here to avoid a function call to ExecQual()
223  * when the qual is null ... saves only a few cycles, but they add up
224  * ...
225  */
226  if (qual == NULL || ExecQual(qual, econtext))
227  {
228  /*
229  * Found a satisfactory scan tuple.
230  */
231  if (projInfo)
232  {
233  /*
234  * Form a projection tuple, store it in the result tuple slot
235  * and return it.
236  */
237  return ExecProject(projInfo);
238  }
239  else
240  {
241  /*
242  * Here, we aren't projecting, so just return scan tuple.
243  */
244  return slot;
245  }
246  }
247  else
248  InstrCountFiltered1(node, 1);
249 
250  /*
251  * Tuple fails qual, so free per-tuple memory and try again.
252  */
253  ResetExprContext(econtext);
254  }
255 }
static TupleTableSlot * ExecScanFetch(ScanState *node, ExecScanAccessMtd accessMtd, ExecScanRecheckMtd recheckMtd)
Definition: execScan.c:35
#define InstrCountFiltered1(node, delta)
Definition: execnodes.h:1141
static TupleTableSlot * ExecProject(ProjectionInfo *projInfo)
Definition: executor.h:375
#define ResetExprContext(econtext)
Definition: executor.h:543
static bool ExecQual(ExprState *state, ExprContext *econtext)
Definition: executor.h:412
TupleTableSlot * ecxt_scantuple
Definition: execnodes.h:249
TupleTableSlot * resultslot
Definition: execnodes.h:96
ExprState * qual
Definition: execnodes.h:1058
ExprContext * ps_ExprContext
Definition: execnodes.h:1076
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:1077
ExprState pi_state
Definition: execnodes.h:356
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:432
#define TupIsNull(slot)
Definition: tuptable.h:299

References ExprContext::ecxt_scantuple, ExecClearTuple(), ExecProject(), ExecQual(), ExecScanFetch(), InstrCountFiltered1, ProjectionInfo::pi_state, ScanState::ps, PlanState::ps_ExprContext, PlanState::ps_ProjInfo, PlanState::qual, ResetExprContext, ExprState::resultslot, and TupIsNull.

Referenced by ExecBitmapHeapScan(), ExecCteScan(), ExecForeignScan(), ExecFunctionScan(), ExecIndexOnlyScan(), ExecIndexScan(), ExecNamedTuplestoreScan(), ExecSampleScan(), ExecSeqScan(), ExecSubqueryScan(), ExecTableFuncScan(), ExecTidRangeScan(), ExecTidScan(), ExecValuesScan(), and ExecWorkTableScan().

◆ ExecScanFetch()

static TupleTableSlot* ExecScanFetch ( ScanState node,
ExecScanAccessMtd  accessMtd,
ExecScanRecheckMtd  recheckMtd 
)
inlinestatic

Definition at line 35 of file execScan.c.

38 {
39  EState *estate = node->ps.state;
40 
42 
43  if (estate->es_epq_active != NULL)
44  {
45  EPQState *epqstate = estate->es_epq_active;
46 
47  /*
48  * We are inside an EvalPlanQual recheck. Return the test tuple if
49  * one is available, after rechecking any access-method-specific
50  * conditions.
51  */
52  Index scanrelid = ((Scan *) node->ps.plan)->scanrelid;
53 
54  if (scanrelid == 0)
55  {
56  /*
57  * This is a ForeignScan or CustomScan which has pushed down a
58  * join to the remote side. The recheck method is responsible not
59  * only for rechecking the scan/join quals but also for storing
60  * the correct tuple in the slot.
61  */
62 
63  TupleTableSlot *slot = node->ss_ScanTupleSlot;
64 
65  if (!(*recheckMtd) (node, slot))
66  ExecClearTuple(slot); /* would not be returned by scan */
67  return slot;
68  }
69  else if (epqstate->relsubs_done[scanrelid - 1])
70  {
71  /*
72  * Return empty slot, as either there is no EPQ tuple for this rel
73  * or we already returned it.
74  */
75 
76  TupleTableSlot *slot = node->ss_ScanTupleSlot;
77 
78  return ExecClearTuple(slot);
79  }
80  else if (epqstate->relsubs_slot[scanrelid - 1] != NULL)
81  {
82  /*
83  * Return replacement tuple provided by the EPQ caller.
84  */
85 
86  TupleTableSlot *slot = epqstate->relsubs_slot[scanrelid - 1];
87 
88  Assert(epqstate->relsubs_rowmark[scanrelid - 1] == NULL);
89 
90  /* Mark to remember that we shouldn't return it again */
91  epqstate->relsubs_done[scanrelid - 1] = true;
92 
93  /* Return empty slot if we haven't got a test tuple */
94  if (TupIsNull(slot))
95  return NULL;
96 
97  /* Check if it meets the access-method conditions */
98  if (!(*recheckMtd) (node, slot))
99  return ExecClearTuple(slot); /* would not be returned by
100  * scan */
101  return slot;
102  }
103  else if (epqstate->relsubs_rowmark[scanrelid - 1] != NULL)
104  {
105  /*
106  * Fetch and return replacement tuple using a non-locking rowmark.
107  */
108 
109  TupleTableSlot *slot = node->ss_ScanTupleSlot;
110 
111  /* Mark to remember that we shouldn't return more */
112  epqstate->relsubs_done[scanrelid - 1] = true;
113 
114  if (!EvalPlanQualFetchRowMark(epqstate, scanrelid, slot))
115  return NULL;
116 
117  /* Return empty slot if we haven't got a test tuple */
118  if (TupIsNull(slot))
119  return NULL;
120 
121  /* Check if it meets the access-method conditions */
122  if (!(*recheckMtd) (node, slot))
123  return ExecClearTuple(slot); /* would not be returned by
124  * scan */
125  return slot;
126  }
127  }
128 
129  /*
130  * Run the node-type-specific access method function to get the next tuple
131  */
132  return (*accessMtd) (node);
133 }
unsigned int Index
Definition: c.h:603
bool EvalPlanQualFetchRowMark(EPQState *epqstate, Index rti, TupleTableSlot *slot)
Definition: execMain.c:2650
Assert(fmt[strlen(fmt) - 1] !='\n')
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121
ExecAuxRowMark ** relsubs_rowmark
Definition: execnodes.h:1212
TupleTableSlot ** relsubs_slot
Definition: execnodes.h:1184
bool * relsubs_done
Definition: execnodes.h:1219
struct EPQState * es_epq_active
Definition: execnodes.h:691
EState * state
Definition: execnodes.h:1039

References Assert(), CHECK_FOR_INTERRUPTS, EState::es_epq_active, EvalPlanQualFetchRowMark(), ExecClearTuple(), PlanState::plan, ScanState::ps, EPQState::relsubs_done, EPQState::relsubs_rowmark, EPQState::relsubs_slot, ScanState::ss_ScanTupleSlot, PlanState::state, and TupIsNull.

Referenced by ExecScan().

◆ ExecScanReScan()

void ExecScanReScan ( ScanState node)

Definition at line 298 of file execScan.c.

299 {
300  EState *estate = node->ps.state;
301 
302  /*
303  * We must clear the scan tuple so that observers (e.g., execCurrent.c)
304  * can tell that this plan node is not positioned on a tuple.
305  */
307 
308  /*
309  * Rescan EvalPlanQual tuple(s) if we're inside an EvalPlanQual recheck.
310  * But don't lose the "blocked" status of blocked target relations.
311  */
312  if (estate->es_epq_active != NULL)
313  {
314  EPQState *epqstate = estate->es_epq_active;
315  Index scanrelid = ((Scan *) node->ps.plan)->scanrelid;
316 
317  if (scanrelid > 0)
318  epqstate->relsubs_done[scanrelid - 1] =
319  epqstate->relsubs_blocked[scanrelid - 1];
320  else
321  {
322  Bitmapset *relids;
323  int rtindex = -1;
324 
325  /*
326  * If an FDW or custom scan provider has replaced the join with a
327  * scan, there are multiple RTIs; reset the epqScanDone flag for
328  * all of them.
329  */
330  if (IsA(node->ps.plan, ForeignScan))
331  relids = ((ForeignScan *) node->ps.plan)->fs_base_relids;
332  else if (IsA(node->ps.plan, CustomScan))
333  relids = ((CustomScan *) node->ps.plan)->custom_relids;
334  else
335  elog(ERROR, "unexpected scan node: %d",
336  (int) nodeTag(node->ps.plan));
337 
338  while ((rtindex = bms_next_member(relids, rtindex)) >= 0)
339  {
340  Assert(rtindex > 0);
341  epqstate->relsubs_done[rtindex - 1] =
342  epqstate->relsubs_blocked[rtindex - 1];
343  }
344  }
345  }
346 }
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1106
#define ERROR
Definition: elog.h:39
#define IsA(nodeptr, _type_)
Definition: nodes.h:179
#define nodeTag(nodeptr)
Definition: nodes.h:133
bool * relsubs_blocked
Definition: execnodes.h:1228

References Assert(), bms_next_member(), elog(), ERROR, EState::es_epq_active, ExecClearTuple(), IsA, nodeTag, PlanState::plan, ScanState::ps, EPQState::relsubs_blocked, EPQState::relsubs_done, ScanState::ss_ScanTupleSlot, and PlanState::state.

Referenced by ExecReScanBitmapHeapScan(), ExecReScanCteScan(), ExecReScanForeignScan(), ExecReScanFunctionScan(), ExecReScanIndexOnlyScan(), ExecReScanIndexScan(), ExecReScanNamedTuplestoreScan(), ExecReScanSampleScan(), ExecReScanSeqScan(), ExecReScanSubqueryScan(), ExecReScanTableFuncScan(), ExecReScanTidRangeScan(), ExecReScanTidScan(), ExecReScanValuesScan(), and ExecReScanWorkTableScan().