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, Index varno)
 
void ExecScanReScan (ScanState *node)
 

Function Documentation

◆ ExecAssignScanProjectionInfo()

void ExecAssignScanProjectionInfo ( ScanState node)

◆ ExecAssignScanProjectionInfoWithVarno()

void ExecAssignScanProjectionInfoWithVarno ( ScanState node,
Index  varno 
)

Definition at line 285 of file execScan.c.

References ExecConditionalAssignProjectionInfo(), ScanState::ps, ScanState::ss_ScanTupleSlot, and TupleTableSlot::tts_tupleDescriptor.

Referenced by ExecInitCustomScan(), ExecInitForeignScan(), and ExecInitIndexOnlyScan().

286 {
288 
289  ExecConditionalAssignProjectionInfo(&node->ps, tupdesc, varno);
290 }
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1334
void ExecConditionalAssignProjectionInfo(PlanState *planstate, TupleDesc inputDesc, Index varno)
Definition: execUtils.c:519
PlanState ps
Definition: execnodes.h:1331
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:124

◆ ExecScan()

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

Definition at line 158 of file execScan.c.

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(), ExecTidScan(), ExecValuesScan(), and ExecWorkTableScan().

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

◆ ExecScanFetch()

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

Definition at line 35 of file execScan.c.

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().

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 we already performed an EPQ substitution
73  * for this relation.
74  */
75 
76  TupleTableSlot *slot = node->ss_ScanTupleSlot;
77 
78  /* Return empty slot, as we already returned a tuple */
79  return ExecClearTuple(slot);
80  }
81  else if (epqstate->relsubs_slot[scanrelid - 1] != NULL)
82  {
83  /*
84  * Return replacement tuple provided by the EPQ caller.
85  */
86 
87  TupleTableSlot *slot = epqstate->relsubs_slot[scanrelid - 1];
88 
89  Assert(epqstate->relsubs_rowmark[scanrelid - 1] == NULL);
90 
91  /* Mark to remember that we shouldn't return more */
92  epqstate->relsubs_done[scanrelid - 1] = true;
93 
94  /* Return empty slot if we haven't got a test tuple */
95  if (TupIsNull(slot))
96  return NULL;
97 
98  /* Check if it meets the access-method conditions */
99  if (!(*recheckMtd) (node, slot))
100  return ExecClearTuple(slot); /* would not be returned by
101  * scan */
102  return slot;
103  }
104  else if (epqstate->relsubs_rowmark[scanrelid - 1] != NULL)
105  {
106  /*
107  * Fetch and return replacement tuple using a non-locking rowmark.
108  */
109 
110  TupleTableSlot *slot = node->ss_ScanTupleSlot;
111 
112  /* Mark to remember that we shouldn't return more */
113  epqstate->relsubs_done[scanrelid - 1] = true;
114 
115  if (!EvalPlanQualFetchRowMark(epqstate, scanrelid, slot))
116  return NULL;
117 
118  /* Return empty slot if we haven't got a test tuple */
119  if (TupIsNull(slot))
120  return NULL;
121 
122  /* Check if it meets the access-method conditions */
123  if (!(*recheckMtd) (node, slot))
124  return ExecClearTuple(slot); /* would not be returned by
125  * scan */
126  return slot;
127  }
128  }
129 
130  /*
131  * Run the node-type-specific access method function to get the next tuple
132  */
133  return (*accessMtd) (node);
134 }
TupleTableSlot ** relsubs_slot
Definition: execnodes.h:1085
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:426
bool EvalPlanQualFetchRowMark(EPQState *epqstate, Index rti, TupleTableSlot *slot)
Definition: execMain.c:2576
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1334
EState * state
Definition: execnodes.h:942
struct EPQState * es_epq_active
Definition: execnodes.h:579
PlanState ps
Definition: execnodes.h:1331
#define TupIsNull(slot)
Definition: tuptable.h:293
bool * relsubs_done
Definition: execnodes.h:1119
unsigned int Index
Definition: c.h:475
Plan * plan
Definition: execnodes.h:940
#define Assert(condition)
Definition: c.h:732
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
ExecAuxRowMark ** relsubs_rowmark
Definition: execnodes.h:1113

◆ ExecScanReScan()

void ExecScanReScan ( ScanState node)

Definition at line 299 of file execScan.c.

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

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

300 {
301  EState *estate = node->ps.state;
302 
303  /*
304  * We must clear the scan tuple so that observers (e.g., execCurrent.c)
305  * can tell that this plan node is not positioned on a tuple.
306  */
308 
309  /* Rescan EvalPlanQual tuple if we're inside an EvalPlanQual recheck */
310  if (estate->es_epq_active != NULL)
311  {
312  EPQState *epqstate = estate->es_epq_active;
313  Index scanrelid = ((Scan *) node->ps.plan)->scanrelid;
314 
315  if (scanrelid > 0)
316  epqstate->relsubs_done[scanrelid - 1] = false;
317  else
318  {
319  Bitmapset *relids;
320  int rtindex = -1;
321 
322  /*
323  * If an FDW or custom scan provider has replaced the join with a
324  * scan, there are multiple RTIs; reset the epqScanDone flag for
325  * all of them.
326  */
327  if (IsA(node->ps.plan, ForeignScan))
328  relids = ((ForeignScan *) node->ps.plan)->fs_relids;
329  else if (IsA(node->ps.plan, CustomScan))
330  relids = ((CustomScan *) node->ps.plan)->custom_relids;
331  else
332  elog(ERROR, "unexpected scan node: %d",
333  (int) nodeTag(node->ps.plan));
334 
335  while ((rtindex = bms_next_member(relids, rtindex)) >= 0)
336  {
337  Assert(rtindex > 0);
338  epqstate->relsubs_done[rtindex - 1] = false;
339  }
340  }
341  }
342 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:426
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1334
EState * state
Definition: execnodes.h:942
struct EPQState * es_epq_active
Definition: execnodes.h:579
PlanState ps
Definition: execnodes.h:1331
#define ERROR
Definition: elog.h:43
bool * relsubs_done
Definition: execnodes.h:1119
unsigned int Index
Definition: c.h:475
Plan * plan
Definition: execnodes.h:940
#define Assert(condition)
Definition: c.h:732
#define nodeTag(nodeptr)
Definition: nodes.h:530
#define elog(elevel,...)
Definition: elog.h:226