PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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 bool tlist_matches_tupdesc (PlanState *ps, List *tlist, Index varno, TupleDesc tupdesc)
 
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

void ExecAssignScanProjectionInfo ( ScanState node)
void ExecAssignScanProjectionInfoWithVarno ( ScanState node,
Index  varno 
)

Definition at line 247 of file execScan.c.

References ExecAssignProjectionInfo(), NULL, Scan::plan, PlanState::plan, ScanState::ps, PlanState::ps_ProjInfo, ScanState::ss_ScanTupleSlot, Plan::targetlist, tlist_matches_tupdesc(), and TupleTableSlot::tts_tupleDescriptor.

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

248 {
249  Scan *scan = (Scan *) node->ps.plan;
250 
251  if (tlist_matches_tupdesc(&node->ps,
252  scan->plan.targetlist,
253  varno,
255  node->ps.ps_ProjInfo = NULL;
256  else
259 }
Plan plan
Definition: plannodes.h:328
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:844
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1061
PlanState ps
Definition: execnodes.h:1058
void ExecAssignProjectionInfo(PlanState *planstate, TupleDesc inputDesc)
Definition: execUtils.c:487
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:121
static bool tlist_matches_tupdesc(PlanState *ps, List *tlist, Index varno, TupleDesc tupdesc)
Definition: execScan.c:262
Plan * plan
Definition: execnodes.h:813
#define NULL
Definition: c.h:229
List * targetlist
Definition: plannodes.h:144
TupleTableSlot* ExecScan ( ScanState node,
ExecScanAccessMtd  accessMtd,
ExecScanRecheckMtd  recheckMtd 
)

Definition at line 121 of file execScan.c.

References CHECK_FOR_INTERRUPTS, ExprContext::ecxt_scantuple, ExecClearTuple(), ExecProject(), ExecQual(), ExecScanFetch(), InstrCountFiltered1, NULL, 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().

124 {
125  ExprContext *econtext;
126  ExprState *qual;
127  ProjectionInfo *projInfo;
128 
129  /*
130  * Fetch data from node
131  */
132  qual = node->ps.qual;
133  projInfo = node->ps.ps_ProjInfo;
134  econtext = node->ps.ps_ExprContext;
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 
161 
162  slot = ExecScanFetch(node, accessMtd, recheckMtd);
163 
164  /*
165  * if the slot returned by the accessMtd contains NULL, then it means
166  * there is nothing more to scan so we just return an empty slot,
167  * being careful to use the projection result slot so it has correct
168  * tupleDesc.
169  */
170  if (TupIsNull(slot))
171  {
172  if (projInfo)
173  return ExecClearTuple(projInfo->pi_state.resultslot);
174  else
175  return slot;
176  }
177 
178  /*
179  * place the current tuple into the expr context
180  */
181  econtext->ecxt_scantuple = slot;
182 
183  /*
184  * check that the current tuple satisfies the qual-clause
185  *
186  * check for non-null qual here to avoid a function call to ExecQual()
187  * when the qual is null ... saves only a few cycles, but they add up
188  * ...
189  */
190  if (qual == NULL || ExecQual(qual, econtext))
191  {
192  /*
193  * Found a satisfactory scan tuple.
194  */
195  if (projInfo)
196  {
197  /*
198  * Form a projection tuple, store it in the result tuple slot
199  * and return it.
200  */
201  return ExecProject(projInfo);
202  }
203  else
204  {
205  /*
206  * Here, we aren't projecting, so just return scan tuple.
207  */
208  return slot;
209  }
210  }
211  else
212  InstrCountFiltered1(node, 1);
213 
214  /*
215  * Tuple fails qual, so free per-tuple memory and try again.
216  */
217  ResetExprContext(econtext);
218  }
219 }
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:844
static TupleTableSlot * ExecScanFetch(ScanState *node, ExecScanAccessMtd accessMtd, ExecScanRecheckMtd recheckMtd)
Definition: execScan.c:37
ExprContext * ps_ExprContext
Definition: execnodes.h:843
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
TupleTableSlot * resultslot
Definition: execnodes.h:69
static bool ExecQual(ExprState *state, ExprContext *econtext)
Definition: executor.h:346
PlanState ps
Definition: execnodes.h:1058
#define TupIsNull(slot)
Definition: tuptable.h:138
#define InstrCountFiltered1(node, delta)
Definition: execnodes.h:858
#define NULL
Definition: c.h:229
TupleTableSlot * ecxt_scantuple
Definition: execnodes.h:197
ExprState * qual
Definition: execnodes.h:827
ExprState pi_state
Definition: execnodes.h:296
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
static TupleTableSlot * ExecProject(ProjectionInfo *projInfo)
Definition: executor.h:309
#define ResetExprContext(econtext)
Definition: executor.h:450
static TupleTableSlot* ExecScanFetch ( ScanState node,
ExecScanAccessMtd  accessMtd,
ExecScanRecheckMtd  recheckMtd 
)
inlinestatic

Definition at line 37 of file execScan.c.

References EState::es_epqScanDone, EState::es_epqTuple, EState::es_epqTupleSet, ExecClearTuple(), ExecStoreTuple(), InvalidBuffer, NULL, PlanState::plan, ScanState::ps, ScanState::ss_ScanTupleSlot, and PlanState::state.

Referenced by ExecScan().

40 {
41  EState *estate = node->ps.state;
42 
43  if (estate->es_epqTuple != 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_epqTupleSet[scanrelid - 1])
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  /* Return empty slot if we haven't got a test tuple */
77  if (estate->es_epqTuple[scanrelid - 1] == NULL)
78  return ExecClearTuple(slot);
79 
80  /* Store test tuple in the plan node's scan slot */
81  ExecStoreTuple(estate->es_epqTuple[scanrelid - 1],
82  slot, InvalidBuffer, false);
83 
84  /* Check if it meets the access-method conditions */
85  if (!(*recheckMtd) (node, slot))
86  ExecClearTuple(slot); /* would not be returned by scan */
87 
88  return slot;
89  }
90  }
91 
92  /*
93  * Run the node-type-specific access method function to get the next tuple
94  */
95  return (*accessMtd) (node);
96 }
TupleTableSlot * ExecStoreTuple(HeapTuple tuple, TupleTableSlot *slot, Buffer buffer, bool shouldFree)
Definition: execTuples.c:320
HeapTuple * es_epqTuple
Definition: execnodes.h:483
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
#define InvalidBuffer
Definition: buf.h:25
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1061
EState * state
Definition: execnodes.h:815
PlanState ps
Definition: execnodes.h:1058
unsigned int Index
Definition: c.h:365
Plan * plan
Definition: execnodes.h:813
#define NULL
Definition: c.h:229
bool * es_epqTupleSet
Definition: execnodes.h:484
bool * es_epqScanDone
Definition: execnodes.h:485
void ExecScanReScan ( ScanState node)

Definition at line 327 of file execScan.c.

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

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

328 {
329  EState *estate = node->ps.state;
330 
331  /* Rescan EvalPlanQual tuple if we're inside an EvalPlanQual recheck */
332  if (estate->es_epqScanDone != NULL)
333  {
334  Index scanrelid = ((Scan *) node->ps.plan)->scanrelid;
335 
336  if (scanrelid > 0)
337  estate->es_epqScanDone[scanrelid - 1] = false;
338  else
339  {
340  Bitmapset *relids;
341  int rtindex = -1;
342 
343  /*
344  * If an FDW or custom scan provider has replaced the join with a
345  * scan, there are multiple RTIs; reset the epqScanDone flag for
346  * all of them.
347  */
348  if (IsA(node->ps.plan, ForeignScan))
349  relids = ((ForeignScan *) node->ps.plan)->fs_relids;
350  else if (IsA(node->ps.plan, CustomScan))
351  relids = ((CustomScan *) node->ps.plan)->custom_relids;
352  else
353  elog(ERROR, "unexpected scan node: %d",
354  (int) nodeTag(node->ps.plan));
355 
356  while ((rtindex = bms_next_member(relids, rtindex)) >= 0)
357  {
358  Assert(rtindex > 0);
359  estate->es_epqScanDone[rtindex - 1] = false;
360  }
361  }
362  }
363 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:937
EState * state
Definition: execnodes.h:815
PlanState ps
Definition: execnodes.h:1058
#define ERROR
Definition: elog.h:43
unsigned int Index
Definition: c.h:365
Plan * plan
Definition: execnodes.h:813
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define nodeTag(nodeptr)
Definition: nodes.h:514
bool * es_epqScanDone
Definition: execnodes.h:485
#define elog
Definition: elog.h:219
static bool tlist_matches_tupdesc ( PlanState ps,
List tlist,
Index  varno,
TupleDesc  tupdesc 
)
static

Definition at line 262 of file execScan.c.

References Assert, tupleDesc::attrs, ExecContextForcesOids(), IsA, lfirst, list_head(), lnext, tupleDesc::natts, NULL, tupleDesc::tdhasoid, Var::varattno, Var::varlevelsup, Var::varno, Var::vartype, and Var::vartypmod.

Referenced by ExecAssignScanProjectionInfoWithVarno().

263 {
264  int numattrs = tupdesc->natts;
265  int attrno;
266  bool hasoid;
267  ListCell *tlist_item = list_head(tlist);
268 
269  /* Check the tlist attributes */
270  for (attrno = 1; attrno <= numattrs; attrno++)
271  {
272  Form_pg_attribute att_tup = tupdesc->attrs[attrno - 1];
273  Var *var;
274 
275  if (tlist_item == NULL)
276  return false; /* tlist too short */
277  var = (Var *) ((TargetEntry *) lfirst(tlist_item))->expr;
278  if (!var || !IsA(var, Var))
279  return false; /* tlist item not a Var */
280  /* if these Asserts fail, planner messed up */
281  Assert(var->varno == varno);
282  Assert(var->varlevelsup == 0);
283  if (var->varattno != attrno)
284  return false; /* out of order */
285  if (att_tup->attisdropped)
286  return false; /* table contains dropped columns */
287 
288  /*
289  * Note: usually the Var's type should match the tupdesc exactly, but
290  * in situations involving unions of columns that have different
291  * typmods, the Var may have come from above the union and hence have
292  * typmod -1. This is a legitimate situation since the Var still
293  * describes the column, just not as exactly as the tupdesc does. We
294  * could change the planner to prevent it, but it'd then insert
295  * projection steps just to convert from specific typmod to typmod -1,
296  * which is pretty silly.
297  */
298  if (var->vartype != att_tup->atttypid ||
299  (var->vartypmod != att_tup->atttypmod &&
300  var->vartypmod != -1))
301  return false; /* type mismatch */
302 
303  tlist_item = lnext(tlist_item);
304  }
305 
306  if (tlist_item)
307  return false; /* tlist too long */
308 
309  /*
310  * If the plan context requires a particular hasoid setting, then that has
311  * to match, too.
312  */
313  if (ExecContextForcesOids(ps, &hasoid) &&
314  hasoid != tupdesc->tdhasoid)
315  return false;
316 
317  return true;
318 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
Index varlevelsup
Definition: primnodes.h:173
bool tdhasoid
Definition: tupdesc.h:79
Form_pg_attribute * attrs
Definition: tupdesc.h:74
AttrNumber varattno
Definition: primnodes.h:168
Definition: primnodes.h:163
int natts
Definition: tupdesc.h:73
Oid vartype
Definition: primnodes.h:170
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
#define lnext(lc)
Definition: pg_list.h:105
Index varno
Definition: primnodes.h:166
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
bool ExecContextForcesOids(PlanState *planstate, bool *hasoids)
Definition: execMain.c:1500
int32 vartypmod
Definition: primnodes.h:171