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 246 of file execScan.c.

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

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

247 {
249 
250  ExecConditionalAssignProjectionInfo(&node->ps, tupdesc, varno);
251 }
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1283
void ExecConditionalAssignProjectionInfo(PlanState *planstate, TupleDesc inputDesc, Index varno)
Definition: execUtils.c:521
PlanState ps
Definition: execnodes.h:1280
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:124

◆ ExecScan()

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

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

122 {
123  ExprContext *econtext;
124  ExprState *qual;
125  ProjectionInfo *projInfo;
126 
127  /*
128  * Fetch data from node
129  */
130  qual = node->ps.qual;
131  projInfo = node->ps.ps_ProjInfo;
132  econtext = node->ps.ps_ExprContext;
133 
134  /* interrupt checks are in ExecScanFetch */
135 
136  /*
137  * If we have neither a qual to check nor a projection to do, just skip
138  * all the overhead and return the raw scan tuple.
139  */
140  if (!qual && !projInfo)
141  {
142  ResetExprContext(econtext);
143  return ExecScanFetch(node, accessMtd, recheckMtd);
144  }
145 
146  /*
147  * Reset per-tuple memory context to free any expression evaluation
148  * storage allocated in the previous tuple cycle.
149  */
150  ResetExprContext(econtext);
151 
152  /*
153  * get a tuple from the access method. Loop until we obtain a tuple that
154  * passes the qualification.
155  */
156  for (;;)
157  {
158  TupleTableSlot *slot;
159 
160  slot = ExecScanFetch(node, accessMtd, recheckMtd);
161 
162  /*
163  * if the slot returned by the accessMtd contains NULL, then it means
164  * there is nothing more to scan so we just return an empty slot,
165  * being careful to use the projection result slot so it has correct
166  * tupleDesc.
167  */
168  if (TupIsNull(slot))
169  {
170  if (projInfo)
171  return ExecClearTuple(projInfo->pi_state.resultslot);
172  else
173  return slot;
174  }
175 
176  /*
177  * place the current tuple into the expr context
178  */
179  econtext->ecxt_scantuple = slot;
180 
181  /*
182  * check that the current tuple satisfies the qual-clause
183  *
184  * check for non-null qual here to avoid a function call to ExecQual()
185  * when the qual is null ... saves only a few cycles, but they add up
186  * ...
187  */
188  if (qual == NULL || ExecQual(qual, econtext))
189  {
190  /*
191  * Found a satisfactory scan tuple.
192  */
193  if (projInfo)
194  {
195  /*
196  * Form a projection tuple, store it in the result tuple slot
197  * and return it.
198  */
199  return ExecProject(projInfo);
200  }
201  else
202  {
203  /*
204  * Here, we aren't projecting, so just return scan tuple.
205  */
206  return slot;
207  }
208  }
209  else
210  InstrCountFiltered1(node, 1);
211 
212  /*
213  * Tuple fails qual, so free per-tuple memory and try again.
214  */
215  ResetExprContext(econtext);
216  }
217 }
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:985
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:984
TupleTableSlot * resultslot
Definition: execnodes.h:81
static bool ExecQual(ExprState *state, ExprContext *econtext)
Definition: executor.h:365
PlanState ps
Definition: execnodes.h:1280
#define TupIsNull(slot)
Definition: tuptable.h:293
#define InstrCountFiltered1(node, delta)
Definition: execnodes.h:1047
TupleTableSlot * ecxt_scantuple
Definition: execnodes.h:224
ExprState * qual
Definition: execnodes.h:966
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 CHECK_FOR_INTERRUPTS, EState::es_epqScanDone, EState::es_epqTupleSlot, ExecClearTuple(), PlanState::plan, ScanState::ps, ScanState::ss_ScanTupleSlot, PlanState::state, and TupIsNull.

Referenced by ExecScan().

38 {
39  EState *estate = node->ps.state;
40 
42 
43  if (estate->es_epqTupleSlot != NULL)
44  {
45  /*
46  * We are inside an EvalPlanQual recheck. Return the test tuple if
47  * one is available, after rechecking any access-method-specific
48  * conditions.
49  */
50  Index scanrelid = ((Scan *) node->ps.plan)->scanrelid;
51 
52  if (scanrelid == 0)
53  {
54  TupleTableSlot *slot = node->ss_ScanTupleSlot;
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  if (!(*recheckMtd) (node, slot))
63  ExecClearTuple(slot); /* would not be returned by scan */
64  return slot;
65  }
66  else if (estate->es_epqTupleSlot[scanrelid - 1] != NULL)
67  {
68  TupleTableSlot *slot = node->ss_ScanTupleSlot;
69 
70  /* Return empty slot if we already returned a tuple */
71  if (estate->es_epqScanDone[scanrelid - 1])
72  return ExecClearTuple(slot);
73  /* Else mark to remember that we shouldn't return more */
74  estate->es_epqScanDone[scanrelid - 1] = true;
75 
76  slot = estate->es_epqTupleSlot[scanrelid - 1];
77 
78  /* Return empty slot if we haven't got a test tuple */
79  if (TupIsNull(slot))
80  return NULL;
81 
82  /* Check if it meets the access-method conditions */
83  if (!(*recheckMtd) (node, slot))
84  return ExecClearTuple(slot); /* would not be returned by
85  * scan */
86 
87  return slot;
88  }
89  }
90 
91  /*
92  * Run the node-type-specific access method function to get the next tuple
93  */
94  return (*accessMtd) (node);
95 }
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:426
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1283
EState * state
Definition: execnodes.h:947
PlanState ps
Definition: execnodes.h:1280
#define TupIsNull(slot)
Definition: tuptable.h:293
unsigned int Index
Definition: c.h:475
Plan * plan
Definition: execnodes.h:945
TupleTableSlot ** es_epqTupleSlot
Definition: execnodes.h:583
bool * es_epqScanDone
Definition: execnodes.h:584
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99

◆ ExecScanReScan()

void ExecScanReScan ( ScanState node)

Definition at line 260 of file execScan.c.

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

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

261 {
262  EState *estate = node->ps.state;
263 
264  /*
265  * We must clear the scan tuple so that observers (e.g., execCurrent.c)
266  * can tell that this plan node is not positioned on a tuple.
267  */
269 
270  /* Rescan EvalPlanQual tuple if we're inside an EvalPlanQual recheck */
271  if (estate->es_epqScanDone != NULL)
272  {
273  Index scanrelid = ((Scan *) node->ps.plan)->scanrelid;
274 
275  if (scanrelid > 0)
276  estate->es_epqScanDone[scanrelid - 1] = false;
277  else
278  {
279  Bitmapset *relids;
280  int rtindex = -1;
281 
282  /*
283  * If an FDW or custom scan provider has replaced the join with a
284  * scan, there are multiple RTIs; reset the epqScanDone flag for
285  * all of them.
286  */
287  if (IsA(node->ps.plan, ForeignScan))
288  relids = ((ForeignScan *) node->ps.plan)->fs_relids;
289  else if (IsA(node->ps.plan, CustomScan))
290  relids = ((CustomScan *) node->ps.plan)->custom_relids;
291  else
292  elog(ERROR, "unexpected scan node: %d",
293  (int) nodeTag(node->ps.plan));
294 
295  while ((rtindex = bms_next_member(relids, rtindex)) >= 0)
296  {
297  Assert(rtindex > 0);
298  estate->es_epqScanDone[rtindex - 1] = false;
299  }
300  }
301  }
302 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:575
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:1283
EState * state
Definition: execnodes.h:947
PlanState ps
Definition: execnodes.h:1280
#define ERROR
Definition: elog.h:43
unsigned int Index
Definition: c.h:475
Plan * plan
Definition: execnodes.h:945
#define Assert(condition)
Definition: c.h:732
#define nodeTag(nodeptr)
Definition: nodes.h:529
#define elog(elevel,...)
Definition: elog.h:226
bool * es_epqScanDone
Definition: execnodes.h:584